should be your goal: write code that is simple and clear. Description Write a function word_compare that takes two argum
Posted: Sun May 15, 2022 2:08 pm
should be your goal: write code that is simple and clear. Description Write a function word_compare that takes two arguments (two strings). There are three possible return cases: • Return the string Anagram if the two inputs are anagrams • Return a two-tuple (a tuple with two items) if the two inputs are words but not anagrams • Return the string Those aren't strings! if either input is not a string. >>> word_compare ("rat", "tar") Anagram' >>> word_compare("hello", "goodbye") ('hello', 'goodbye') >>> word_compare(22, "hello") "Those aren't strings!" We'll work through this problem in three parts: 1) Write a function in a python file 2) Modify that funtion to use default parameters 3) Import that function in a different python file After getting this code to work, you will add a custom test case to verify future changes you might make do not break functionality, I Part 1 WordCompare.py Write a program WordCompare.py that will contain a single method called word_compare that functions as above. Hint - you can use the method is instance() to check the type of an object
Part 2 - Default parameters Python allows you to set default values for parameters. For example, suppose we have a function called biggen that takes a number and doubles it. We might write def biggen (n): return n.2 If we want to make it possible to multiply the input by a different number, we could add a parameter: def biggen (n, factor): return nefactor In some programming languages, this defines two different functions: one that takes two arguments and one that takes only one. In python, the second one would overwrite the first. If we still want to write biggen (n) 1 to get n2, we can set the factor parameter default to be 2. This way, if the user doesn't give a value, then it will default to 2: i def biggen (n, factor-2): return • factor Modify word_compare to use a default parameter Modify your word_compare method so that the second word uses a default value of "steal". The program should work as before, but should compare the first word to "steal" if no second word is specified.
Part 3 - A standalone program A python program can be run all by itself, or it may be used by other programs. The word_compare.py program is not really meant to be run on its own it is meant to be imported for use by other programs Write a python program called FindAnagrams.py that: • imports the function word_compare • defines a function find_anagrams that: uses word_compare to scan each word in a set against every other word returns a string representing all anagrams in the list, as below For instance, if your words were: words ["tar", "rat", "art","face", "cafe", "hello") Then your function should return the following string : tar: ['rat'art) rat: ['tar'. 'art) art: ['tar'. rat] face: ['cate') cafe: ['face hello: Note that find_anagrans should return a string, not print it. Part 4 - test it Testing your code is a vital portion of this course. Create a Ist of 12 new words (words which have not been used in examples in this assignment), 6 of which have anagrams and 6 which do not. For your 6 words with anagrams, you can use 6 words which are all anagrams of each other, or 3 pairs of anagrams, or 2 groups of 3-word anagrams Construct the expected return string, and use an assert statement to verify that you get the value you expect: words - CYOUR LIST OF WORDS] expected result - Construct the expected result assert find anagrams (vords) -- expected result W test your function with your own words
Part 2 - Default parameters Python allows you to set default values for parameters. For example, suppose we have a function called biggen that takes a number and doubles it. We might write def biggen (n): return n.2 If we want to make it possible to multiply the input by a different number, we could add a parameter: def biggen (n, factor): return nefactor In some programming languages, this defines two different functions: one that takes two arguments and one that takes only one. In python, the second one would overwrite the first. If we still want to write biggen (n) 1 to get n2, we can set the factor parameter default to be 2. This way, if the user doesn't give a value, then it will default to 2: i def biggen (n, factor-2): return • factor Modify word_compare to use a default parameter Modify your word_compare method so that the second word uses a default value of "steal". The program should work as before, but should compare the first word to "steal" if no second word is specified.
Part 3 - A standalone program A python program can be run all by itself, or it may be used by other programs. The word_compare.py program is not really meant to be run on its own it is meant to be imported for use by other programs Write a python program called FindAnagrams.py that: • imports the function word_compare • defines a function find_anagrams that: uses word_compare to scan each word in a set against every other word returns a string representing all anagrams in the list, as below For instance, if your words were: words ["tar", "rat", "art","face", "cafe", "hello") Then your function should return the following string : tar: ['rat'art) rat: ['tar'. 'art) art: ['tar'. rat] face: ['cate') cafe: ['face hello: Note that find_anagrans should return a string, not print it. Part 4 - test it Testing your code is a vital portion of this course. Create a Ist of 12 new words (words which have not been used in examples in this assignment), 6 of which have anagrams and 6 which do not. For your 6 words with anagrams, you can use 6 words which are all anagrams of each other, or 3 pairs of anagrams, or 2 groups of 3-word anagrams Construct the expected return string, and use an assert statement to verify that you get the value you expect: words - CYOUR LIST OF WORDS] expected result - Construct the expected result assert find anagrams (vords) -- expected result W test your function with your own words