Program specification

1. Define a function called anagram(str1,str2), which takes two strings as arguments and returns True if the works have exactly the same letters in them and returns False otherwise. Note that the order of the letters is unimportant, it is merely enough for the two words to contain the same letters. For example, if you implement the function anagram correctly then you should get the following results;

>>> anagram("sidebar","seabird")
True
>>> anagram("cheese", "pancakes")
False

Hint: To check if two words contain the same letters then you can sort the letters of the words into alphabetical order then compare the lists to see if they are the same, if they differ then the two strings contain different letters. To convert a string into a list look at the built-in function list(str). A sorted version of a list can be obtained by using sorted(some list).

2. Define a function called test anagram() that takes no arguments. This function should call the anagram function with different strings to test if it is working cor- rectly. The function should print out in a human readable format the tests that it is running and whether it passed or not and should report on the total number of tests passed.

Hint: Take a look at the testing example given in the Additional Hints and Examples section at the end of this document. You can base your code on that.

3. Define a function called get dictionary wordlist(). This function should return a list of words which have been read from the file dictionary.txt (the file is available in the coursework resources download). The file is formatted with a single word on each line. Ensure that the strings in the returned list do not have newline characters (n) at the end.

Hint: You will need to find out how to read stuff from files to do this.

Warning: You should avoid displaying the whole dictionary word list on the termi- nal. If you define the function and then type get dictionary wordlist(), Python will try to display the result, which is a huge list, on the terminal. Instead, to check it is working, you should enter something like get dictionary wordlist()[0] at the terminal, which will display the first word in the list. Or you could display a range of words in the list with, for example, get dictionary wordlist()[0-100].

4. Define a function called test get dictionary wordlist(). This function should call the get dictionary wordlist() function and should test if the function is work- ing correctly. The function should print out the number of words in the dictionary and should print out the first 10 words.

5. Define a function called find anagrams in wordlist(str, str list) that takes two arguments. This function should return a list of all strings that are members of the list str list and are anagrams of the string given as the first argument.

Hint: You should use the function you wrote for Part 1.

6. Define a function called find anagrams(str) that takes one argument. This func- tion should return a list of all the words in the dictionary file, dictionary.txt, that are anagrams of the first argument.

Hint: You should use the function you wrote for Parts 3 and 5.

7. Define a function called test find anagrams() that accepts no arguments. This function should call the find anagrams function for a variety of different inputs and output the anagrams found.

Note: For this text function, there is no need to test whether the anagrams gen- erated are the correct set (since this would require full knowledge of what is in the dictionary). But the tests should be such that they give a good idea of whether find anagrams(str) is working correctly.

8. Define a function called sub anagram(str1, str2) that takes two arguments. This function should return True if str1 is a sub-anagram of str2. A string, str1, is a sub-anagram of str2 if and only if every letter that occurs in str1 occurs at least as many times in str2. For example, not is a sub-anagram of function but neither note nor tint are sub-anagrams of function.

Hint: Convert both strings two lists of characters and sort them. Loop through the characters in the first string; if the letter does not exist in the first string then it cannot be a sub-anagram (return False), otherwise remove (one occurrence of) the letter from the second string and continue looping through the first string. If you loop to the end of the first string then it is a sub-anagram of the second string and you should return True.

Note: According to the definition, if str1 is an anagram of str2, then str1 is also a sub-anagram of str2 (i.e. an anagram is a special case of a sub-anagram). Thus, for example, "flea" is a sub-anagram of "leaf".

9. Define a function called find sub anagram in wordlist(str, str list) which takes two arguments. This function should return a list of words that are in the second argument that are sub-anagrams of the first argument.

10. Define a function called test find sub anagram in wordlist() which takes no ar- guments. This function should call the find sub anagram in wordlist function on different inputs to determine if the function is working correctly. Your testing function should report whether each test gave the correct results and give the overall number of correct results.

Hint: This testing function is similar to that asked for in Part 2. As for that question you can base your code on the testing example given in the Additional Hints and Examples section at the end of this document.

11. Define a function called remove letters(str1, str2) which takes two arguments. This function should return a string that is obtained by removing the letters of the first argument from the second argument. Each occurrence of a letter in str1 should remove at most one matching letter from str2. For example, removing the letters aac from string abcabc should result in the string bbc.

12. Define a function called find two word anagrams(str) which takes one argument. This function should return a list of all strings made up of two words, separated by a space. You should try to find all pairs of words in the dictionary provided on the website such that those two words contain the same letters as the letters in the argument.

For example, for input "brandon" the function should work as follows:

>>> find_two_word_anagrams("brandon")
[’and born’, ’band nor’, ’barn don’, ’barn nod’, ’bond ran’,
’born dan’, ’bran don’, ’bran nod’, ’brand no’, ’brand on’,
’darn nob’, ’nard nob’, ’nob rand’]

Hint: This task is a little tricky and requires clear thinking about the problem. Below is an outline of the steps in a possible implementation the function;

  • initialise a list two word anagrams to the empty list ([]).
  • call the find sub anagrams in wordlist function to get a list of all the sub- anagrams of str that are contained in the word list.
  • Loop over the list of anagrams and for each anagram (part anagram) do the following
    • remove the letters of part anagram from the input string str and store the result in a variable called remaining.
    • call the find anagrams function on remaining to get a list of anagrams of the remaining letters.
    • for each anagram of the remaining letters append it to the string part anagram and add it to two word anagrams

Additional Hints and Examples

For Part 2:

An example of defining a function to test another function

## This function should return the number of distinct characters that
## occur in the string given as its argument.
def num_diff_chars_in_string( s ):
diff_syms_found = []
for char in s:
if not char in diff_syms_found:
diff_syms_found.append( char )
return len( diff_syms_found )
## This function tests num_diff_chars_in_string( s ) by calling it for
## some strings and checking the returned values.
def test_num_diff_chars_in_string():
print( "n** Testing num_diff_chars_in_string( some_string ) **n" )
### String Num diff chars
tests = [["this", 4],
["ABBA", 2],
["look", 3],
[":) :( <3 :/", 6]]
num_correct = 0
for test in tests:
result = num_diff_chars_in_string( test[0] )
print( "For input ’" + test[0] + "’, output is:", result, end="" )
if result == test[1]:
print( " --- Correct :)" )
num_correct += 1
else:
print( " --- WRONG!! Should be:", test[1] )
print( "nThe function gave the correct result for", num_correct,
"out of", len(tests), "tests." )

Hey, whats going on! The testing function says that num diff chars in string(s) gives the wrong answer for one of the tests. Can you work out where the error lies?

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.