In: Computer Science
1a Write a function COTlistRange(xstart,xend,xstep) that returns a list that has:
- as index 0 item, the value of xstart
- as index 1 item, the value xstart+xstep
- as index 2 item, the value (xstart+xstep)+xstep,
and so on as long as the value does equal or pass the value of xend.
However, the function should return an empty string if for positive xstep, xstart>xend or if for negative xstep, xstart < xend. Here are three examples:
a = COTlistRange(2, 3.51, 0.5) should produce
a = [2, 2.5, 3., 3.5]
a = COTlistRange(2, 0.01, -0.5) should produce
a = [2, 1.5, 1., 0.5]
a = COTlistRange(2, 1.51, 0.5) should produce
a = []
Your code should also have a function mainProg() for testing the COTlistRange(xstart,xend,xstep) function and the associated if __name__ == statement. Write the mainProg() code so that it does the three examples shown above.
b "Walk" your code of part a for the three examples, i.e. create a column for each variable showing in the column the evolution of changing object values.
c Repeat part a with a function COTtupleRange(xstart,xend,xstep). You are not allowed to use the tuple command to convert your result from part a to a tuple.
The above code has been given in python :-
def COTlistRange(xstart,xend,xstep):
a=[]
if(xstep>0):
if( xstart>xend):
return a
while(xstart<xend):
a.append(xstart)
xstart=xstart+xstep
elif(xstep<0):
if(xstart<xend):
return a
while(xstart>xend):
a.append(xstart)
xstart=xstart+xstep
elif(xstep==0):
a.append(xstart)
return a
def mainProg():
a = COTlistRange(2, 3.51, 0.5)
print(a)
a = COTlistRange(2, 0.01, -0.5)
print(a)
a = COTlistRange(2, 1.51, 0.5)
print(a)
# a = COTlistRange(2, 1.51, 0.0) one more case if xstep == 0 then only the xstart is added to the list
#print(a)
if __name__=="__main__":
mainProg()
The above function works fine and gives the desired output.
for part c). Tuple is immutable in python which means the elements cannot be added into it. The only way is to add elements into the list and then convert the list into the tuple once again.
I hope the answer helps you. Feel free to ask for any doubts in the comments section. Please give a positive rating if the answer helps you. Happy Studying & Happy Coding