Question

In: Computer Science

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 examples:

  • a = COTtupleRange(2, 3.51, 0.5) should produce

                  a = (2, 2.5, 3., 3.5)

  • a = COTtupleRange(2, 0.01, -0.5) should produce

                  a = (2, 1.5, 1., 0.5)

  • a = COTtupleRange(2, 1.51, 0.5) should produce

                  a = ()   

               Your code should also have a function mainProg() for testing the COTtupleRange(xstart,xend,xstep) function and the associated if __name__ == statement. Write the mainProg() code so that it does the three examples shown above.

Solutions

Expert Solution

Hi,

Hope you are doing fine. I have coded the question in python keeping all the requirements in mind. The code has been clearly explained using comments ehich have been highlighted in bold. Have a look at the snippets of the code for correct indentation.

Program:

#function COTtupleRange
def COTtupleRange(xstart,xend,xstep):
#result is an empty string
result=()
#if for positive xstep, xstart>xend or if for negative xstep, xstart < xend the function returns an empty string.
#if you need an empty tuple simply return result

if (xstep>0 and xstart>xend) or (xstep<0 and xstart<xend):
return ""
else:
#let s be the first term
s=xstart
#when xstep is positive, the loop must go on till s is greater than xend
#when xstep is negative, the loop must on of till s is lesser than xend

while((s<=xend and xstep>0) or (s>=xend and xstep<0)):
#tuples are immutable, so append() cannot be used.
#instead we use concatenate and add another tuple (s,) in each iteration

result=result+(s,)
#updating the value of s in each iteration
s=s+xstep
#return result
return result

def mainProg():
#testing the output using three tuples: a,b,c
print("Testing COTtupleRange")
a = COTtupleRange(2, 3.51, 0.5)
b = COTtupleRange(2, 0.01, -0.5)
c = COTtupleRange(2, 1.51, 0.5)
#print results
print("a = COTtupleRange(2, 3.51, 0.5) : ",a)
print("b = COTtupleRange(2, 0.01, -0.5) : ",b)
#if you need the result of third tuple as '()' as shown in the question, then instead of returning empty string in COTtupleRange(), return an empty tuple (return result)
print("c = COTtupleRange(2, 1.51, 0.5) : ",c)

#calling mainProg() is name is main:
if __name__ == "__main__":
mainProg()

Executable code snippet:

Output: Note that, for c, we return an empty string, hence nothing is visible. If you want '()' to be printed then replace the line return "" with return result in COTtupleRange function


Related Solutions

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...
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
Python pls Create a function party_freq(dicto:dict): this function returns a list inside tuple that how many...
Python pls Create a function party_freq(dicto:dict): this function returns a list inside tuple that how many times each person party in the day. For example def party_freq(dicto:dict) -> [(str,{(int,int,int): int})]: #code here input dict1 ={'fire1': {(2000,5,20,480) : ('Aaron', 25, 300, ( 0, 300)), (2000,5,20,720) : ('Baily', 45, 1500, (1500,500)), (2000,5,21,490) : ('Aaron', 35, 500, (1300,500)) }, 'fire2': {(2000,5,20,810) : ('Baily', 45, 1400, (600,1600)), (2000,5,20,930) : ('Baily', 43, 1800, ( 0, 0)) }} output [('Aaron', {(2000,5,20): 1, (2000,5,21): 1}), ('Baily', {(2000,5,20):...
create a function that sorted tuple by the second values of each tuple. If the values...
create a function that sorted tuple by the second values of each tuple. If the values are the same, sorted by increasing order. Finally returns a list of the first element of each tuple that sorted. def sort_tup(tup): #Code here input : tup1 = [(1, 15), (2, 8), (3, 22), (4, 30), (5, 15)] output: [4,3,1,5,2]
Write a Java method that returns the index of the largest element in an array of...
Write a Java method that returns the index of the largest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header: 
 public static int indexOfLargestElement(double[] array)
 Write a test program that prompts the user to enter ten numbers, invokes this
method to return the index of the largest element, and displays the index.
#Write a function called find_max_sales. find_max_sales will #have one parameter: a list of tuples. Each tuple...
#Write a function called find_max_sales. find_max_sales will #have one parameter: a list of tuples. Each tuple in the #list will have two items: a string and an integer. The #string will represent the name of a movie, and the integer #will represent that movie's total ticket sales (in millions #of dollars). # #The function should return the movie from the list that #had the most sales. Return only the movie name, not the #full tuple. #Below are some lines of...
Using Java Write a method that returns the index of the smallest element in an array...
Using Java Write a method that returns the index of the smallest element in an array of integers. If the number of such elements is greater than 1, return the smallest index. Use the following header:   public static int indexOfSmallestElement (double[] array)
Write a method in JAVA, called binarySearch, that returns the index of the key element if...
Write a method in JAVA, called binarySearch, that returns the index of the key element if found in the array, otherwise it will return the value of minus(insertion point +1). In main, use an initializer list create an array of ints called nums holding the following values: 1, 4, 13, 43, -25, 17, 22, -37, 29. Test your binarySearch method on nums with a key value number in it (e.g. 4) and one that is not (e.g. 100). Ex. Given...
Define a function named posterize. This function expects an image and a tuple of RGB values...
Define a function named posterize. This function expects an image and a tuple of RGB values as arguments. The function modifies the image like the blackAndWhite function developed in Chapter 7 and shown below, but it uses passed in RGB values instead of black. def blackAndWhite(image): """Converts the argument image to black and white.""" blackPixel = (0, 0, 0) whitePixel = (255, 255, 255) for y in range(image.getHeight()): for x in range(image.getWidth()): (r, g, b) = image.getPixel(x, y) average =...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT