modulus - Using Python Modulo Operator to Sort List -


i've been working on project euler problems try , learn python , wrote solution second problem (find sum of even-valued terms in fibonacci sequence not exceed 4 million). code gives me correct solution, requires me use modulus division twice in order remove odd-numbered values list of fibonacci numbers generated. here solution wrote:

term_1 = 1 term_2 = 2 fibonacci_list = [1] while term_2 < 4000000:     fibonacci_list.append(term_2)     term_1, term_2 = term_2, term_1 + term_2 num in fibonacci_list:     if num % 2 != 0         fibonacci_list.remove(num) num in fibonacci_list:     if num % 2 != 0         fibonacci_list.remove(num) return sum(fibonacci_list) 

if put in 1 for-loop, list fibonacci_list becomes following:

[2, 5, 8, 21, 34, 89, 144, 377, 610, 1597, 2584, 6765, 10946, 28657, 46368, 121393, 196418, 514229, 832040, 2178309, 3524578] 

shouldn't odd numbered terms fail modulus division test , removed? why need run loop twice remove odd numbered terms?

i imagine problem facing attempting remove items list while iterating on list.

see here, here , here previous questions on same topic.

for sake of discussion, let's assume in fact problem, , let's assume forbidden remove items list while iterating on it.

what differently result in not needing remove items list while iterating on it? i'm not sure if want given answer outright or not since doing project euler, refrain giving out obvious answers.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -