Я пытаюсь определить пространственно-временную сложность функции, которую я написал, чтобы я мог обсудить эту функцию в терминах асимптотических границ в моем отчете.
Учитывая список списков, гдекаждый подсписок представляет два элемента в "конфликте", эта функция возвращает словарь уникальных парных конфликтов.В этом контексте [1,2]
и [2,1]
являются дубликатами друг друга, поэтому список [[1,2], [1,2], [2,1], [3,4]]
приведет к появлению словаря {1: [2], 3: [4]}
def calculateDistinctConflicts():
conflicts = [[1,2], [1,2], [2,1], [3,4]]
distinctConflicts = {}
for pair in conflicts:
element0 = pair[0]
element1 = pair[1]
conflictKeys = distinctConflicts.keys()
if element0 not in conflictKeys and element1 not in conflictKeys:
distinctConflicts[element0] = []
targetList = distinctConflicts[element0]
targetList.append(element1)
elif element0 in conflictKeys and element1 not in distinctConflicts[element0]:
targetList = distinctConflicts[element0]
targetList.append(element1)
elif element1 in conflictKeys and element0 not in distinctConflicts[element1]:
targetList = distinctConflicts[element1]
targetList.append(element0)
elif element0 in conflictKeys and element1 in distinctConflicts[element0] or element1 in conflictKeys and element0 in distinctConflicts[element1]:
continue
print (distinctConflicts)
# {1: [2], 3: [4]}