K
K
Korifa2018-09-20 14:37:50
Python
Korifa, 2018-09-20 14:37:50

Why does the program skip an element in an iteration?

Why is [10, 20] skipped in iteration ?

def sum_of_intervals(intervals):
  length = 0
  while intervals:
    inter = intervals.pop()
    print(inter)
    print('outer')
    for x in intervals:
      print(x)
      if (min(x) <= sum(set(inter)) <= max(x)) or (min(inter) <= sum(x) <= max(inter)):
        inter = inter + x
        intervals.remove(x)
        print(intervals)
        print(inter)
        print('inner')
          
    length = length + (max(inter) - min(inter))
  return intervals, length

print(sum_of_intervals())

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Kitaev, 2018-09-20
@Korifa

Because you can't modify the sequence you're iterating over for-loop

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question