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...
Question 12 PYTHON: Write a function named first_last that takes a single parameter, string_list (a list...
Question 12 PYTHON: Write a function named first_last that takes a single parameter, string_list (a list of strings). The function first_last should return a list of the strings in string_list that are not empty and that begin and end with the same letter. For example, the following would be correct input and output for the function first_last. response = ['to', 'that', 'I', 'say', '', 'hurrah'] print(first_last(response)) ['that', 'I', 'hurrah'] Question 13 (20 points) Write a function named number_luck. The function...
* 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 class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a...
Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call: isPalindrome(q) should...
Python 3.7: Give the following sample code, write the following programs: • An iterator class called...
Python 3.7: Give the following sample code, write the following programs: • An iterator class called Iteratefirstn that implements __iter__() and __next__() methods where next method should raise an exception for the cur index if it is passed the last element otherwise set cur index to the next index and advance the index by one, i.e. cur, num = num, num+1. Instantiate the class to calculate the sum of 1000000 elements similar to the example. • Instead of implementing the...
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.
PointList Class Write a class named PointList that keeps a list of Point objects in an...
PointList Class Write a class named PointList that keeps a list of Point objects in an ArrayList. The PointList class should accept any object that is an instance of the Point class,, or a subclass of Point. Demonstrate the class in an application. This is My point class…. public class Point<T> {    private T xCoordinate; private T yCoordinate;    public Point(T x, T y)    {    xCoordinate = x;    yCoordinate = y;    } public void setX(T ){        xCoordinate = x;    }    public...
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',...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT