I keep getting a TypeError 'Range' object does not support item assignment. I tried to change the code a little as in add iter(...) before the range, aswell as list(...) before range. However, it didn't help and the Error continues. Here's the code:
def findAnchor(anchors, node):
start = node
while node != anchors[node]:
node = anchors[node]
anchors[start] = node
return node
def unionFindConnectedComponents(graph):
anchors = range(len(graph))
for node in iter(range(len(graph))):
for neighbor in graph[node]:
if neighbor < node:
continue
a1 = findAnchor(anchors, node)
a2 = findAnchor(anchors, neighbor)
if a1 < a2:
anchors[a2] = a1
elif a2 < a1:
anchors[a1] = a2
labels = [None]*len(graph)
current_label = 0
for node in range(len(graph)):
a = findAnchor(anchors, node)
if a == node:
labels[a] = current_label
current_label += 1
else:
labels[node] = labels[a]
return anchors, labels
Now the TypeError is in the beginning at anchors[start] = node. And node is a given argument from the second function where it says for node in iter(range(len(graph))). I tried it with iter and with list, neither worked. What to do?