Runtime Error on #26 - Python 2.7
This code results in a runtime error at test #26:
from sys import stdout
d = int(float(raw_input().strip()))
n = [1,5,10,20,50,100,250,500,1000,2000]
l = ['few','few','several','pack','lots','horde','throng','swarm','zounds','legion','legion']
a = min(map(lambda x: x+d,filter(lambda x: x > 0, map(lambda x:x - d,n))))
stdout.write(l[n.index(a)])
This code was accepted without error:
from sys import stdout
d = int(raw_input())
if d > 0 and d < 5:
stdout.write('few')
elif d < 10:
stdout.write('several')
elif d < 20:
stdout.write('pack')
elif d < 50:
stdout.write('lots')
elif d < 100:
stdout.write('horde')
elif d < 250:
stdout.write('throng')
elif d < 500:
stdout.write('swarm')
elif d < 1000:
stdout.write('zounds')
else:
stdout.write('legion')
Can anyone explain why the first code fails but the second doesn't?