Word puzzles |
The following line reads this file into a set of words:
val words = java.io.File("words.txt").useLines { it.toSet() }
In this project, we will solve a number of word puzzles using this word list. Write a script or program containing functions that solve each of these puzzles.
Include calls to all functions so that running your code will print out all the answers.
Two words are anagrams if they contain the same letters, arranged in a different order.
Write a function findAnagrams(word: String) that returns the list of all anagrams of word. Here are some examples:
>>> findAnagrams("lovely") [lovely, volley] >>> findAnagrams("sprouts") [sprouts, stupors] >>> findAnagrams("sorting") [sorting, storing, trigons] >>> findAnagrams("protest") [potters, protest, spotter] >>> findAnagrams("persist") [esprits, persist, priests, spriest, sprites, stirpes, stripes]
You may be tempted to implement this function by generating all permutations of the letters and testing for each permutation whether it is a word. This is very inefficient when the input is long. Instead, use this function:
fun sortWord(s: String): String = s.toCharArray().sorted().joinToString(separator="")
It takes a string and returns another string containing the same letters, but sorted alphabetically. Now two words u and w are anagrams if and only if sortWord(u) == sortWord(w).
"E pluribus unum" means "making one from many". The input is a phrase, the output should be a list of all words that can be made from exactly the letters in the input.
Make sure that your findAnagram() function works correctly for the following examples. In other words, your function needs to convert upper case to lower case, and needs to ignore the spaces.
fun ePluribusUnum(phrase: String) { println("E pluribus unum: '$phrase' --> '${findAnagrams(phrase)}'") } ePluribusUnum("SEA SALT") ePluribusUnum("PANIC ROOMS") ePluribusUnum("COTERIES") ePluribusUnum("TREETOP ACTOR")
The phrase "I before E, except after C" is a widely known mnemonic which is supposed to help when spelling English words.
Let's check if the two clauses of the phrase are plausible.
Count how many words in words.txt:
What do you think about the rule above?
Write a function to solve each of the following word puzzles. In each puzzle, the task is to fill in the missing letters (indicated by # signs), so that together with the given letters, you obtain two (or more) words.
A doctor: # ###### A fish: #T######So, in case it isn't clear yet: the first word has seven letters. If you insert a T between the first two letters, you get another word. For instance, the first word could be "SINGERS", the second "STINGERS"—except that this pair of words does not meet the description. Can you find the right word pair from your program's output?
Eggs: ###### Animals: Z######
Makes you famous: #####T##### Makes you infamous: ##### #####
sweet and sticky: ####C## It's foot operated: ####D##
In the next puzzle, the same five letters make four different words:
E##### AU##### CRE##### EXPE#####
Finally, in the last puzzle, three letters make four different words—and no two of these words rhyme!
RH### B### C### T###
Word puzzles |