X-Git-Url: http://git.euphorik.ch/?p=Scala.git;a=blobdiff_plain;f=Assignment_06%2Fsrc%2Fmain%2Fscala%2FAnagrams.scala;fp=Assignment_06%2Fsrc%2Fmain%2Fscala%2FAnagrams.scala;h=e52912447851b71ad2ecc792504fa3f7472f3abb;hp=0000000000000000000000000000000000000000;hb=6c413508bbba63f2421fd57d87eba4266c5beca0;hpb=3caf6fced99f7fe7d910dfdc3baa9ade89145848 diff --git a/Assignment_06/src/main/scala/Anagrams.scala b/Assignment_06/src/main/scala/Anagrams.scala new file mode 100644 index 0000000..e529124 --- /dev/null +++ b/Assignment_06/src/main/scala/Anagrams.scala @@ -0,0 +1,76 @@ +package anagrams + +/** + * Sentence anagrams generation + * MSE course, T-AdvPrPa course + */ +object Anagrams { + /** + * Types definitions + */ + type Word = String + type Occurrences = List[(Char, Int)] + type Sentence = List[Word] + + /** + * The dictionary is simply a sequence of words. + * It is predefined and obtained as a sequence using the utility method `loadDictionary`. + */ + val dictionary: List[Word] = loadDictionary() + + /** + * Question 1 : converts the word into its character occurrence list. + * + * For instance, + * wordOccurences("abcd") gives List(('a', 1), ('b', 1), ('c', 1), ('d', 1)) + * wordOccurences("aabcddd") gives List(('a', 2), ('b', 1), ('c', 1), ('d', 3)) + * + * Note: the upper case and lower case version of the character are treated as the + * same character, and are represented as a lower case character in the occurrence list. + * The list is sorted alphabeticaly. + */ + def wordOccurrences(w: Word): Occurrences = + w.toLowerCase().groupBy(identity).toList.map{ case (_, letters) => (letters(0), letters.length) }.sortWith((a, b) => a._1 < b._1) + + /** + * Question 2: Valid words + * + * The `dictionaryByOccurrences` is a `Map` from different occurrences to a sequence of all + * 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. + */ + lazy val dictionaryByOccurrences: Map[Occurrences, List[Word]] = + dictionary groupBy (w => wordOccurrences(w)) + + /** + * Question 3: Returns all the anagrams of a given word + */ + def wordAnagrams(word: Word): List[Word] = + dictionaryByOccurrences get wordOccurrences(word) match { + case Some(words) => words + case None => List() + } + + /** + * Question 4: Returns all the subsets of an occurrence list + */ + + /** + * Generates all the subsets of a set + */ + def combinations(occurrences: Occurrences): List[Occurrences] = ??? + + /** + * Question 5: remove occurrences from x that are in y + */ + def subtract(x: Occurrences, y: Occurrences): Occurrences = ??? + + /** + * Question 6 - Generate sentence anagrams + */ + + /** Converts a sentence into its character occurrence list. */ + def sentenceOccurrences(s: Sentence): Occurrences = ??? + + def sentenceAnagrams(sentence: Sentence): List[Sentence] = ??? + +}