Write a python program that reads a word list and prints out all the anagrams in the word list. In
earlier exercises you saw how to read a word list. Also, in earlier examples we saw how
the sorted characters of a word are a useful canonical representation of an anagram
(Hint: Therefore, useful as a key).
class Word: def __init__(self, data, index): self.data = data self.index = index def printAnagrams(arr): dupArray = [] size = len(arr) for i in range(size): dupArray.append(Word(arr[i], i)) for i in range(size): dupArray[i].data = ''.join(sorted(dupArray[i].data)) dupArray = sorted(dupArray, key=lambda x: x.data) for i in range(size): print(arr[dupArray[i].index]) def main(): arr = ["dog", "act", "cat", "god", "tac"] printAnagrams(arr) if __name__== '__main__': main()
Get Answers For Free
Most questions answered within 1 hours.