Question

In: Computer Science

LANGUAGE PYTHON 3.7 Write a collection class named "Jumbler". Jumbler takes in an optional list of...

LANGUAGE PYTHON 3.7

Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a parameter to the constuctor with various strings. Jumbler stores random strings and we access the items based on the methods listed below.

Jumbler supports the following methods:

add() : Add a string to Jumbler
get() : return a random string from Jumbler
max() : return the largest string in the Jumbler based on the length of the strings in the Jumbler.
iterator: returns an iterator to be able to iterate through all the strings in the jumbler.

Solutions

Expert Solution

(*Note: Please up-vote. If any doubt, please let me know in the comments)

The class code and driver program code for testing are given below:

Class Code:

import random

class jumbler():

    def __init__(self,string_list=[]):

        self.mystrings = string_list   #will assign supplied list if given by user or an empty list otherwise

    

    #add method takes new string to be added as an argument

    def add(self,new_string=""):

        if new_string == "":

            new_string = input("Please enter the string to be added: ") #Ask for string if not supplied while calling the method

        self.mystrings.append(new_string)

    def get(self):

        random_index = random.randrange(0,len(self.mystrings)) #generating a random integer within range to use as index

        return self.mystrings[random_index]

    

    def max(self):

        max_str = self.mystrings[0]   #initialize max to be the first string in the list

        for i in range(0,len(self.mystrings)):

            if len(self.mystrings[i])>len(max_str):

                max_str = self.mystrings[i]

        return max_str

    

    def __iter__(self):

        self.n = 0

        return self

    def __next__(self):

        if self.n < len(self.mystrings):

            result = self.mystrings[self.n] #return string at nth position

            self.n += 1

            return result

        else:

            raise StopIteration

Main driver program code for testing the class:

#main testing

J1 = jumbler(["This","is it"]) #Creating an object

print(J1.max())

J1.add("foolish") #testing add method

J1.add("pseudo turtle truth")

J1.add()

print(J1.get())  #testing get method

#testing the iterator using a for loop

for i in J1:

    print(i)

Code screenshot (for indentation reference):

Test Output Screenshot:


Related Solutions

Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a...
Write a collection class named "Jumbler". Jumbler takes in an optional list of strings as a parameter to the constuctor with various strings. Jumbler stores random strings and we access the items based on the methods listed below. Jumbler supports the following methods: add() : Add a string to Jumbler get() : return a random string from Jumbler max() : return the largest string in the Jumbler based on the length of the strings in the Jumbler. iterator: returns an...
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).
(Python) a) Using the the code below write a function that takes the list xs as...
(Python) a) Using the the code below write a function that takes the list xs as input, divides it into nss = ns/nrs chunks (where nrs is an integer input parameter), computes the mean and standard deviation s (square root of the variance) of the numbers in each chunk and saves them in two lists of length nss and return these upon finishing. Hint: list slicing capabilities can be useful in implementing this function. from random import random as rnd...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
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.
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
(In python) 4. Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as...
(In python) 4. Write a function that involves two arguments, named changeTheCase(myFile, case), that takes, as arguments, the name of a file, myFile, and the case, which will either be “upper” or “lower”. If case is equal to “upper” the function will open the file, convert all characters on each line to upper case, write each line to a new file, named “upperCase.txt”, and return the string “Converted file to upper case.” If case is equal to “lower” the function...
Write a Python class to represent a LibraryMember that takes books from a library. A member...
Write a Python class to represent a LibraryMember that takes books from a library. A member has four private attributes, a name, an id, a fine amount, and the number of books borrowed. Your program should have two functions to add to the number of books and reduce the number of books with a member. Both functions, to add and return books, must return the final number of books in hand. Your program should print an error message whenever the...
PYTHON: Write a recursive function named linear_search that searches a list to find a given element....
PYTHON: Write a recursive function named linear_search that searches a list to find a given element. If the element is in the list, the function returns the index of the first instance of the element, otherwise it returns -1000. Sample Output >> linear_search(72, [10, 32, 83, 2, 72, 100, 32]) 4 >> linear_search(32, [10, 32, 83, 2, 72, 100, 32]) 1 >> linear_search(0, [10, 32, 83, 2, 72, 100, 32]) -1000 >> linear_search('a', ['c', 'a', 'l', 'i', 'f', 'o', 'r',...
Write Python class that takes a string and returns with a valid phone number. Number format...
Write Python class that takes a string and returns with a valid phone number. Number format is ten-digit numbers consisting of a three-digit area code and a seven-digit number. Clean up different telephone numbers by removing punctuation, and removing incorrect format and the country code (1). You should throw a ValueError with a string if there are too many or too few digits, or the wrong digits. For example, the strings: +1 (617) 111-0000, 617-111-0000, 1 617 111 0000, 617.111.0000...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT