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:
- reads parameter
xs
- keep looping long condition
-len(xs) < i< len(xs)
true
, meaning, longi
more-len(xs)
(negative of length ofxs
list passf
) and lesslen(xs)
well. inside
while
loop:a - reads 2 values each element of list
xs
(presuming a
tuple give example of it's use) , assigni
,v
b - add each
v
listr
(r.append(v)
)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
Post a Comment