2

I have to define my own floating-point version of range() here. It works the same as built-in range() function but can accept float values.start, stop, and step are integer or float numbers and step is a non-zero value and it also can be negative.

I have tried looking for answers on stackoverflow but all of them only work for positive step. I keep getting stuck on a test where it tests the value (0,0.25,-1) I'm a hundred percent sure there is a quicker and faster to implement this because my program takes a long time to test all the values. can someone help me create a function to do this and I 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 and x>stop:
        my_list.append(x)
        if x < 0:
            x=step
        elif x > 0:
            x+=step
        else:
            x+=step
    return my_list  
2
  • If it is supposed to work like the builtin range function, then you need to put the greater number first if you are going backwards Commented Mar 1, 2016 at 5:39
  • Found this answer (and others) stackoverflow.com/a/32536460/2308683 Commented Mar 1, 2016 at 5:44

2 Answers 2

3

Convert the floating steps into integer steps. You'll get less drift from error due to rounding of floats:

def float_range(start,stop,step):
    istop = int((stop-start) // step)
    for i in range(int(istop)):
        yield start + i * step
Sign up to request clarification or add additional context in comments.

Comments

2

The main reason you're getting answers wrong is the top level if statements are wrong. You don't want to look at the boundaries when deciding which way to move. It's legal to ask for a range that has nothing in it (with e.g. stop less than start with a positive step).

Here's how I'd minimally adjust your code to 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 really be if step < 0 with an extra check for step == 0 
        while x > stop:
            my_list.append(x)
            x += step
    return my_list 

3 Comments

That should happen automatically if your other check is either removed or changed to if step < 0. (You might also want to raise an exception is step is zero.)
Oh, actually I hadn't read all the details of your code. There are some other issues, but the initial bits were right. I'll edit with more fixes.
Thanks so much! it worked. I had everything misplaced. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.