Define and test a function myRange. This function should behave like Python’s standard range function, with the required and optional arguments, but it should return a list. Do not use the range function in your implementation!
Study Python’s help on range to determine the names, positions, and what to do with your function’s parameters. Use a default value of None for the two optional parameters. If these parameters both equal None, then the only provided argument should be considered the stop value, and the start value should default to 0. If just the third parameter equals None, then the function has been called with a start and stop value. Thus, the first part of the function’s code establishes what the values of the parameters are or should be. The rest of the code uses those values to build a list by counting up or down.
An example of the myRange function with only one argument provided is shown below:
print(myRange(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
def myRange(start, stop = None, step = None): if(step == None and stop == None): stop = start start = 0 step = 1 elif(step == None): step = 1 result = [] if (step < 0): while stop < start: result.append(start) start += step else: while start < stop: result.append(start) start += step return result def main(): print([x for x in myRange(10)]) print([x for x in myRange(1,10)]) print([x for x in myRange(1, 10,2)]) print([x for x in myRange(10, 1, -1)]) main()
Get Answers For Free
Most questions answered within 1 hours.