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...
* Python * * Python Programming * Part 1: Product Class Write a class named Product...
* Python * * Python Programming * Part 1: Product Class Write a class named Product that holds data about an item in a retail store.   The class should store the following data in attributes: product id, item description, units in inventory, and price. Write the __init__ method to require all four attributes. Also write a __str__ method for debugging output.    Once you have written the class, write a main() function that creates three Product objects and stores the following...
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 Question Using lists, write the function non_unique(list) that takes a list list as argument. It...
Python Question Using lists, write the function non_unique(list) that takes a list list as argument. It returns a list which duplicated elements remains and each duplicated element is followed by a number which shows how many times it appears. All elements in return list should be in the same order as their appearance in the original list. For example, given the input [‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘d’, ‘a’,‘e’], the function would return [‘a’, 3, ‘b’, 2]. Another example, ['abc',...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT