This little script I wrote should do the trick. I made sure not to use any external libraries:
if __name__ == '__main__':
data = [3,4,5,1,0,3,4,6,7,2,3,0,6,7,4,3,2,1,0,2,3,5,4,5,6,5,7,0,8,2,1,4,0]
G = {i:[0,0] for i in data} # {i:(status, n_cycles) ... }
for i in range(len(data)):
nid, S, C = data[i], *G[data[i]]
if nid == 0: G.update({i:[2,c] for i,(s,c) in G.items() if s==1}); continue
elif G[nid][0] == 0: G.update({nid:[1,C]})
elif G[nid][0] == 2: G.update({nid:[0,C+1]})
[print(f'[{i}] Number of Cycles: {c}') for i,(s,c) in sorted(list(G.items()),key=lambda x:x[0]) if i>0]
When I run this, I get the following output:
[1] Number of Cycles: 2
[2] Number of Cycles: 3
[3] Number of Cycles: 3
[4] Number of Cycles: 4
[5] Number of Cycles: 1
[6] Number of Cycles: 2
[7] Number of Cycles: 2
[8] Number of Cycles: 0
For each number in your list, the number of cycles is the number of times that a number x appeared at position i, then some m numbers later a 0 appeared, then another x appeared n positions after the zero at position (i+m+n). After each cycle is counted, this process restarts for x, meaning that now the next x cycle could potentially start from position (i+m+n). Lmk if you want me to explain it in more detail!