The first one is to
Write a program which prints out all anagrams for a given word. For more information about Anagram, please check this wikipedia page.It's not said but I assume that, besides the word, the program must also read a dictionary of words in which it will look for anagrams. My solution is simple and very much alike the solution to last week's second challenge.
The ideia is to use a hash function that generates a key for each word so that anagrams always produce the same key and non-anagrams always lead to different keys. The hash function I use lowercases the word so that we compare letters case insensitively. Then it splits the word in all of its letters, sorts, and joins them together. So, for example, "Perl" is keyed as "elpr".
The script first generates the key for the input word. Then it iterates for all dictionary words, printing those that have a key equal to the input word's key.
The second challenge is to
Write a program to find the sequence of characters that has the most anagrams.My solution first reads all of the dictionary words and classify them in anagrams using the same hash function of the first script. Then it finds and prints the keys associated with the maximum number of anagrams.
And this is how they work. First I use the second script to grok the sequence of characters that has the most anagrams in my Ubuntu dictionary. Then I use the first script to grok all the anagrams associated with it:
-----
I came up with another solution to the second challenge that is shorter, faster and uses no modules:
Nenhum comentário:
Postar um comentário