python - While loop function explanation and general input explanation -


i have function goes follows

def f(xs):     = 0     r=[]     while -len(xs) < i< len(xs):         i,v = xs[i]         r.append(v)     return r 

i wondering if run down on how function working entirely have been trying head around while loop , append function i'm having complete brain fart.

also please explanation why input below returns non-terminating execution

 zs = f([(1, 'a'), (-2, 'b'), (4,'c'), (-2, 'd')]) 

this function does:

  1. reads parameter xs
  2. keep looping long condition -len(xs) < i< len(xs) true, meaning, long i more -len(xs) (negative of length of xs list pass f) and less len(xs) well.
  3. inside while loop:

    a - reads 2 values each element of list xs (presuming a
    tuple give example of it's use) , assign i , v

    b - add each v list r (r.append(v))

  4. return list r

now, concerning example , based on above explanation, don't see how it's non-terminating execution the condition of while loop false after reaches tuple(4,'c') when -4 < 4 < 4 false condition.

so, @ end, f function return list: ['a','b','c'].


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -