Wednesday, 15 August 2012

python - First element out of order on a list -



python - First element out of order on a list -

for list [1,4,6,8,2,10] should homecoming 2

for list [1,6,7,9,3,10] should homecoming 3

it should homecoming first number smaller last, first in wrong order.

how should it? wrote, in 2 versions. i've been working on whole day no success.

#1 version

def out_of_order(lst): in range(0,len(lst)): b in range(a+1,len(lst)): if(b<a): print(b) break else: print("none")

#2 version

def out_of_orders(lst): a,b in zip(lst,lst[1:]): if(b<a): print(b) break else: print("none")

simply maintain lastly checked element in list , check if current element in list smaller or not.

def out_of_order(lst): before = 0 y in lst: if y < before: homecoming y before = y print(out_of_order([1,4,6,8,2,10])) print(out_of_order([1,6,7,9,3,10]))

python

No comments:

Post a Comment