-1

Essentially trying to add numbers to list L based on a pattern.

starting at index point 0 you print the element of that index point. The next index point then becomes becomes the int of the element you just printed and the pattern continues until done.

The catch is if that index point has already been used you move one to the left until you reach an index point that has not been used.

Below is my solution to the problem. Everything seems to work i.e. it moves along the number as it is suppose to however when it encounters an index point that it has already used it still prints that element.

Any ideas why it doesn't appear to be skipping to the left properly?

    Output is: 
    If L is: L = [1, 4, 4, 1, 2, 4, 3]
    N is:  1, 4, 2, 4, 2, 4, 2] when it should
    [1, 4, 2, 4, 1, 4, 3]

    # other way around

L = [1, 4, 4, 1, 2, 4, 3]
for index, s in enumerate(L):
    print(index, s)

print('  ', L)

M = []
N = []
T = [] 


i = L[0]
index_position = 0

while len(T) != len(L):
    if index_position in T:
        index_position = index_position + 1
    else:
        N.append(i)
        T.append(index_position)
        index_position = i
        i = L[index_position]


print('N is: ', N)
print('T is: ', T)
jpazzajpeg
  • 133
  • 2
  • 9

1 Answers1

0
seen_that_before = []
N = []

for number in L
  if number in seen_that_before: continue
  seen_that_before.append(number)
  N.append(number)

Something like that?

Please use significant variable names. N, T, L or i are confusing.

Pobe
  • 2,693
  • 1
  • 17
  • 24