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...
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).
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
(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...
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT