4 * Sentence anagrams generation
5 * MSE course, T-AdvPrPa course
12 type Occurrences
= List
[(Char
, Int
)]
13 type Sentence
= List
[Word
]
16 * The dictionary is simply a sequence of words.
17 * It is predefined and obtained as a sequence using the utility method `loadDictionary`.
19 val dictionary
: List
[Word
] = loadDictionary()
22 * Question 1 : converts the word into its character occurrence list.
25 * wordOccurences("abcd") gives List(('a', 1), ('b', 1), ('c', 1), ('d', 1))
26 * wordOccurences("aabcddd") gives List(('a', 2), ('b', 1), ('c', 1), ('d', 3))
28 * Note: the upper case and lower case version of the character are treated as the
29 * same character, and are represented as a lower case character in the occurrence list.
30 * The list is sorted alphabeticaly.
32 def wordOccurrences(w
: Word
): Occurrences
=
33 w
.toLowerCase().groupBy(identity
).toList
.map
{ case (_
, letters
) => (letters(0), letters
.length
) }.sortWith((a
, b
) => a
._1
< b
._1
)
36 * Question 2: Valid words
38 * The `dictionaryByOccurrences` is a `Map` from different occurrences to a sequence of all
39 * the words that have that occurrence count. This map serves as an easy way to obtain all the anagrams of a word given its occurrence list.
41 lazy val dictionaryByOccurrences
: Map
[Occurrences
, List
[Word
]] =
42 dictionary
groupBy (w
=> wordOccurrences(w
))
45 * Question 3: Returns all the anagrams of a given word
47 def wordAnagrams(word
: Word
): List
[Word
] =
48 dictionaryByOccurrences get
wordOccurrences(word
) match {
49 case Some(words
) => words
54 * Question 4: Returns all the subsets of an occurrence list
58 * Generates all the subsets of a set
60 def combinations(occurrences
: Occurrences
): List
[Occurrences
] = ???
63 * Question 5: remove occurrences from x that are in y
65 def subtract(x
: Occurrences
, y
: Occurrences
): Occurrences
= ???
68 * Question 6 - Generate sentence anagrams
71 /** Converts a sentence into its character occurrence list. */
72 def sentenceOccurrences(s
: Sentence
): Occurrences
= ???
74 def sentenceAnagrams(sentence
: Sentence
): List
[Sentence
] = ???