Python 2 compiler doesn't read the correct values after the 31st inputted value -
solving smoothing weather problem on codeabbey. prints correct output first 32 values after doesn't read inputted values correctly. inputted test values on 150. here code:
from __future__ import division num=int(raw_input()); inp=((raw_input()).split(" ")); lists=[]; in inp: if inp.index(i)==0 or inp.index(i)==len(inp)-1: lists.append(inp[inp.index(i)]) else: a,b,c=0.0,0.0,0.0; a=float(inp[(inp.index(i))+1]) b=float(inp[inp.index(i)]) c=float(inp[(inp.index(i))-1]) x=(a+b+c)/3 x = ("%.9f" % x).rstrip('0') lists.append(x) in lists: print i,
the index in following code return first occurrence of in inp. so, if there duplicate values in inp, whole logic fails.
if inp.index(i)==0 or inp.index(i)==len(inp)-1: lists.append(inp[inp.index(i)])
the correct approach enumerate , correct indices:
from __future__ import division num = int(raw_input()) inp = ((raw_input()).split(" ")) lists = [] i, item in enumerate(inp): # loop through inp, while filling next item in item , keep on incrementing each time starting 0 if == 0 or == len(inp)-1: lists.append(inp[i]) else: = float(inp[i+1]) b = float(inp[i]) c = float(inp[i-1]) x = (a+b+c) / 3.0 x = ("%.9f" % x).rstrip('0') lists.append(x) in lists: print i,
hope helps.
Comments
Post a Comment