Question

In: Computer Science

Write a python program using the following requirements: Create a class called Sentence which has a...

Write a python program using the following requirements:

Create a class called Sentence which

  • has a constructor that takes a sentence string as input.
    • The default value for the constructor should be an empty string
    • The sentence must be a private attribute in the class
  • contains the following class methods:
    • get_all_words — Returns all the words in the sentence as a list
    • get_word — Returns only the word at a particular index in the sentence
      • Arguments: index
    • set_word — Changes the word at a given index in sentence to a new word
      • Arguments: index, new_word
      • Does not return anything
    • a str method — Returns the sentence as a string using the built in method

Include a unit test in an if __name__ block to test the class. Use assert to check for errors.

Unit Test Example:

  • Set a sentence variable with a sentence of your choice
  • Instantiate the Sentence object using the variable
  • Test that get_all_words() correctly returns that sentence as a list
  • Test that get_word(#) correctly returns the # word in the sentence
  • Test that replace() actually changes a word in the sentence object
  • Print the sentence before and after the word change using the built in str method

Solutions

Expert Solution

class Sentence:
        def __init__(self, sentence):
                self.__sentence = sentence

        def get_all_words(self):
                return self.__sentence.split()

        def get_word(self, index):
                return self.__sentence.split()[index]

        def replace(self, index, new_word):
                l = self.__sentence.split()
                l[index] = new_word
                self.__sentence = ' '.join(l)

        def __str__(self):
                return str(self.__sentence)


if __name__ == "__main__":
        s = "Hello, this is the sample test case."
        sentence = Sentence(s)

        # Testing get_all_words
        assert ["Hello,", "this", "is", "the", "sample", "test", "case."] == sentence.get_all_words()

        # Testing get_word(index)
        assert "Hello," == sentence.get_word(0) and "is" == sentence.get_word(2) and "test" == sentence.get_word(5)

        # Testing replace(index, new_word) along with print
        print("Before Replace:", sentence)
        sentence.replace(4, "new")
        assert str(sentence) == "Hello, this is the new test case."
        print("After Replace:", sentence)

Related Solutions

Using Python, write a program named hw3a.py that meets the following requirements: Create a dictionary called...
Using Python, write a program named hw3a.py that meets the following requirements: Create a dictionary called glossary. Have the glossary include a list of five (5) programing words you have learned in chapters 4-6. Use these words as keys to your dictionary. Find the definition of these words and use them as the values to the keys. Using a loop, print each word and its meaning as neatly formatted output. Create three dictionaries called person. Have each of the person...
Using Python, write a program that meets the following requirements: Make a list called sandwich_orders and...
Using Python, write a program that meets the following requirements: Make a list called sandwich_orders and fill it with the names of various sandwiches. Make an empty list called finished_sandwiches. Loop through the list of sandwich orders and spring a message for each order such as "I am working on your tuna sandwich" As each sandwich is made, move it to the list of finished sandwiches. After all the sandwiches have been made, print a message listing each sandwich that...
Using Python, write a program named hw3b.py that meets the following requirements: Welcome the user to...
Using Python, write a program named hw3b.py that meets the following requirements: Welcome the user to Zion's Pizza Restaurant and ask the user how many people are in their dinner group. If the answer is more than eight (8), print a message saying they'll have to wait for a table. Otherwise, report that their table is ready and take their order. Assume the client orders one pizza, and ask what he/she would like on their pizza, include a loop that...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
Write a program called Teen that takes a sentence and returns a new sentence based on...
Write a program called Teen that takes a sentence and returns a new sentence based on how a teenager might say that. The method that creates the new string should be called teenTalk and should have the signature shown in the starter code. The way to do that is to put the word "like" in between each of the words in the original sentence. For example, teenTalk("That is so funny!") would return "That like is like so like funny!". Sample...
Using Python 3, Write a class called GolfScore that keeps track of the score for a...
Using Python 3, Write a class called GolfScore that keeps track of the score for a person playing a round of golf. We will limit the round to 9 holes, just to make testing easier. As a reminder, the holes on the golf course are numbered 1 – 9. Create a class level variable named NUM_OF_HOLES set to 9. Methods to be written: The constructor – sets the score for all holes to -1 addScore (hole, score) – this method...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate...
Write code to create a Python dictionary called class. Add two entries to the dictionary: Associate the key 'class_name' with the value 'MAT123', and associate the key 'sect_num' with '211_145'
By only importing multiprocessing Write a python program which do the following : 1- Create one...
By only importing multiprocessing Write a python program which do the following : 1- Create one process this process must-read the content of a file “read any file content exist in your machine” and after that create another process in the first process function which will get the file content as a parameter for the second process function. 2- The second process function will get the file content and check out if there are any special characters or numbers in...
Write a Python program, in a file called StickFigure.py, which, given a value for the total...
Write a Python program, in a file called StickFigure.py, which, given a value for the total height of a stick figure, uses a recursive function to generate the stick figure pattern as shown below. The recursive function is used to print the entire figure. Note that the head, arms and legs stay the same, but the body can be different lengths, depending on what the user enters as the height of the figure. The "body" length is always 3 lines...
Define a class called Goals that has the following requirements in c++: a function called set...
Define a class called Goals that has the following requirements in c++: a function called set that takes 3 int parameters that are goals for "fame", "happiness" and "money". The function returns true and saves the values if they add up to exactly 60, and returns false otherwise (you may assume the parameters are not negative) a functions satisfies that takes 3 int parameters and returns true if each parameter is at least as large as the saved goal, false...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT