Function for Range for floats in python -
i have define own floating-point version of range() here. works same built-in range() function can accept float values.start, stop, , step integer or float numbers , step non-zero value , can negative.
i have tried looking answers on stackoverflow of them work positive step. keep getting stuck on test tests value (0,0.25,-1) i'm hundred percent sure there quicker , faster implement because program takes long time test values. can me create function , can't user imported tools.
def float(start,stop,step): x = start my_list = [] if start < stop: while x<stop: my_list.append(x) if x < 0: x-=step elif x > 0: x+=step else: x+=step return my_list if start > stop: while x<=start , x>stop: my_list.append(x) if x < 0: x=step elif x > 0: x+=step else: x+=step return my_list
the main reason you're getting answers wrong top level if
statements wrong. don't want @ boundaries when deciding way move. it's legal ask range has nothing in (with e.g. stop
less start
positive step
).
here's how i'd minimally adjust code work properly:
def float_range(start,stop,step): x = start my_list = [] if step > 0: while x < stop: my_list.append(x) x += step else: # should if step < 0 check step == 0 while x > stop: my_list.append(x) x += step return my_list
Comments
Post a Comment