Question

In: Computer Science

1a           Write a function COTlistRange(xstart,xend,xstep) that returns a list that has:                - as index 0...

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.

Solutions

Expert Solution

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


Related Solutions

Write a function COTtupleRange(xstart,xend,xstep) that returns a tuple that has:                - as index 0 item,...
Write a function COTtupleRange(xstart,xend,xstep) that returns a tuple 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...
Write a function parent_index_3_heap(child_index) that returns the index of the parent of the node at the...
Write a function parent_index_3_heap(child_index) that returns the index of the parent of the node at the given child_index. A None value should be returned if the child has no parent. Notes: This function is obviously for a 3-heap! The values in the heap list start at index 1 - that is the root of the heap is at index 1 in the heap list. This is for Python
Write a function that takes a valid stringlist and returns the index of the smallest element...
Write a function that takes a valid stringlist and returns the index of the smallest element in the list represented by the stringlist. You may not use split(). Examples: >>> stringlist min index('[123,53,1,8]') # 1 is smallest2 >>> stringlist min index('[1,2,345,0]') # 0 is smallest3 >>> stringlist min index('[5] ') # 5 is smallest0
We were asked this: Write a function that takes a list, and returns a list representing...
We were asked this: Write a function that takes a list, and returns a list representing each word whose reverse is also in the list. def find_reversals(lst: List[str]) -> List[str]: Each pair, such as 'abut', 'tuba', should be represented by the first element encountered. Don't report the same pairs twice. Don't list palindromes. I wrote this, which might not be optimal but passes the unit test! def find_reversals(lst): #Place to save to found_reversals=[]    #Make each string lowercase for i...
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Use Scheme Language Write a Scheme function that takes a list and returns a list identical...
Use Scheme Language Write a Scheme function that takes a list and returns a list identical to the parameter except the third element has been deleted. For example, (deleteitem '(a b c d e)) returns ‘(a b d e) ; (deleteitem '(a b (c d) e)) returns ‘(a b e).
Write a function child_indices(parent, branch_factor) that returns a list containing the list of indices where the...
Write a function child_indices(parent, branch_factor) that returns a list containing the list of indices where the children of the node at index parent will be stored for a heap list with the given branch_factor (ie, a branch_factor-heap). Python language Notes: The function should return all possible indices and the indices should be in order from smallest to biggest. The values in the heap list start at index 1 - that is the root of the heap is at index 1...
1.Write a function div7(lst) which takes in a list of integers, and returns a list of...
1.Write a function div7(lst) which takes in a list of integers, and returns a list of booleans of the same length, such that for each integer in the original list, the boolean in the output list is True if that integer was divisible by 7, or False if not. Use list comprehensions in python, the function only could be at most two lines long. Here is some examples: >>> div7([14, 5, 7, 3, 29, 28, 10]) [True, False, True, False,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT