In: Computer Science
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 the 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 range function with only one argument provided is shown below:
print(myRange(10)) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
def myRange(stop,start=0,step=0):
lt=[]
if start!=None: #start is not a none value
i=start
else:
i=0 #else assign i=0
if step==None or step>=0:
#for count up
while i<stop: #range upto stop value
lt.append(i) #add numbers o the list
if step!=None and step!=0: # if step value is specified any value
without None or 0 then add it to i variable.
i+=step
else:
i+=1 #default value for step value is one, so add 1 to each
iteration
else:
#count down
while i>=stop:
lt.append(i)
if step!=None and step!=0:
i=i+step
return lt
print(myRange(12,0,-1)) #print the list of values with specified
range
output:
for print(myRange(0,12,-1))
for print(myRange(12,0))
for print(myRange(12,None,None))
for print(myRange(12))