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.
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...
Write a Python function that returns a list of keys in aDict with the value target....
Write a Python function that returns a list of keys in aDict with the value target. The list of keys you return should be sorted in increasing order. The keys and values in aDict are both integers. (If aDict does not contain the value target, you should return an empty list.) This function takes in a dictionary and an integer and returns a list. def keysWithValue(aDict, target): ''' aDict: a dictionary target: an integer ''' # Your code here
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
*LISP PROGRAM* 2. Write a function that, given a list of lists, returns the total length...
*LISP PROGRAM* 2. Write a function that, given a list of lists, returns the total length of all the lists. This problem can be solved two different ways. 3. Write a program that prompts the user to enter two numbers and then outputs the sum of the two numbers. 4.Write ALLODDP, a recursive function that returns T if all the numbers in a list are odd.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT