Question: Python please Problem 1: Replacing words, once. A kid is playing a game of replacing words. There is a dictionary d, which specifies for some
Python please









Problem 1: Replacing words, once. A kid is playing a game of replacing words. There is a dictionary d, which specifies for some words a replacement. The kid takes a piece of text, like "I like to eat bananas covered in chocolate", and if the replacement dictionary d is: d = {"bananas": "apples", "chocolate": "syrup" } the result would be: "I like to eat apples covered in syrup Your task is to implement a function replace_once(t, d), that takes a text t and a replacement dictionary d, and returns the result of replacing words according to the dictionary: if a word appears as a key, replace it with the dictionary value, and if a word does not appear in the dictionary, leave it alone. To write your code, note that .split() splits a string according to whitespace: [3] print("I love eating bananas". split() ['I', 'love', 'eating', 'bananas'] Given a list of words, you can concatenate them together with spaces in between as follows: [4] word_list = "I love eating bananas".split() print("Word list:", word_list) back_together ".join(word_list) print("Back together:", back_together) = this homework. # You can also join them in other print("You can also put commas:", ways, btw. Just DON'T do it for ".join(word_list)) Word list: ['I', 'love', 'eating', 'bananas'] Back together: I love eating bananas You can also put commas: I, love, eating, bananas Here is where you can write your replace_once function. Here is where you can write your replace_once function. [ ] def replace_once(t, d): a @param t: @param d: Creturns: a string dictionary, mapping words to their replacements string, where words in d have been replaced according to a the dictionary mapping. # YOUR CODE HERE raise NotImplementedError() # This is a place where you can write additional tests # your code, or debugging code, if you need. You can to help you test also leave it blank. # YOUR CODE HERE raise NotImplementedError() Here are some tests. 0 0 # 10 points. Tests. d = 1] "I love cats" check_equal (replace_once( td), I love cats) t 1 d = {"cats": "dogs"} check_equal (replace_once(t, d), "I love dogs") NameError Traceback (most recent call last) Kipython-input-6-5f8ed487b359) in 0 3 d = 0 4 t = "I love cats" 5 check_equal (replace_once(t, d), "I love cats") 6 7 d = {"cats" : "dogs"} NameError: name 'replace_once' is not defined Problem 2: Replacing words, irrespective of case. The problem with our previous code is that we require an exact case match: if we have the string: t = "Dogs are nice" and a replacement dictionary d = {"dogs": "cats"} the result of replace_once(t, d) is still "Dogs are nice". Write a function replace_case_insensitive(t, d) that applies the replacement irrespective of whether the original word is capitalized or not, and that preserves the capitalization: a capitalized word is replaced with another capitalized word. For example, if d = {"dogs": "cats"} then replace_case_insensitive ("Dogs are nice", d) is: "Cats are nice" and replace_case_insensitive("I like dogs", d) is: "I like cats" When writing your solution, you can assume that both keys and values in the replacement dictionary appear in lowercase. Note also that you only need to concern yourselves with words that are either all in lowercase, like "dog, or capitalized, like "Dog"; you don't have to worry about words like "DOG" or "DOG. To write your solution, note that you can capitalize a word w via: w.capitalize() and you can test whether a word w is capitalized via: and you can test whether a word w is capitalized via: w.capitalize() Also, given a word w, you can turn it into lowercase via: w.lower() You can write your solution below. [] def replace_case_insensitive(t, d): @param t string @param d: a dictionary mapping words to their replacements Creturns: a string, where words in d have been replaced according in case insensitive way, and preserving capitalization. to the dictionary mapping, # YOUR CODE HERE raise NotImplementedError() # This is a place where you can write additional tests # your code, or debugging code, if you need. You can to help you test also leave it blank. # YOUR CODE HERE raise NotImplementedError() Here are some tests. [ ] # 10 points t = "I love dogs" d = {"dogs" : "cats"} check_equal (replace_case_insensitive(t, d), "I love cats") t = "Dogs are nice d = {"dogs" : "cats"} check_equal (replace_case_insensitive(t, d), "Cats are nice") Here are some tests. [] # 10 points = t "I love dogs" d = {"dogs": "cats"} check_equal (replace_case_insensitive(t, d), "I love cats") t d = "Dogs are nice" = {"dogs": "cats"} check_equal (replace_case_insensitive(t, d), "Cats are nice") t = "Cats cats nothing else than cats d {"dogs": "cats", "cats" : "dogs"} check_equal (replace_case_insensitive(t, d), "Dogs dogs nothing else = than dogs) - Problem 3: Repeated replacements Let's go back now to the simpler version of the problem, where we replace in case-sensitive way. Our original version of the code applies the replacement only once. That is, if the dictionary d maps: d = {"cats": "dogs", "dogs": "pigs"} and our text is t = "I love cats", the function replace_once(t, d) applies the replacement only once, and we obtain "I love dogs" Now, you should write a function replace_chain, that keeps applying replacements until no more replacements can be applied, so that replace_chain(t, d) yields: "I love pigs" and our text is t = "I love cats", the function replace_once(t, d) applies the replacement only once, and we obtain "I love dogs" Now, you should write a function replace_chain, that keeps applying replacements until no more replacements can be applied, so that replace_chain(t, d) yields: "I love pigs" def replace_chain(t, d): @param t: a string @param d: a dictionary, mapping words to their replacements Preturns: string, where words in d have been replaced according to repeating the replacements until no replacement can be applied. a the dictionary mapping, # YOUR CODE HERE raise NotImplementedError() [ ] # This is a # your code, place where you can write additional tests debugging code, if you need. You can to help you test also leave it blank. or # YOUR CODE HERE raise NotImplementedError() Here are some tests. [] # 10 points t = "I like dogs" d = docs"; "cato "cats"; "nies "hirds: "butterflies"} Here are some tests. [] # 10 points t = "I like dogs" d = {"dogs": "cats", "cats": "pigs", "birds": "butterflies"} check_equal (replace_chain(t, d), "I like pigs") A question for you to ponder. If one has a loop in the dictionary, as in: d = {"cats": "dogs", "dogs": "cats"} what would your code do? Would it terminate? How would you be able to detect problem replacements like this? You will learn this later in the class, in the section on graph and graph algorithms. Problem 4: Triangles In this problem, you have to create a class representing a triangle. A triangle is created specifying the lengths of its 3 sides; for instance, Triangle (3, 4, 5) creates a triangle with sides 3,4,5. The class has to implement the following methods. is_well_formed: Returns True if the triangle is well-formed, and false otherwise. A triangle is well formed if, for edges of length x, y, z, you have: *