From 604ff923ff39c9e067e93349a9a0f81c94c595f5 Mon Sep 17 00:00:00 2001 From: beauvill Date: Fri, 25 Oct 2019 11:52:33 +0200 Subject: [PATCH] Finished assignement 5 --- src/main/scala/forcomp/Anagrams.scala | 36 +++++++++++++++++++++------ 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/src/main/scala/forcomp/Anagrams.scala b/src/main/scala/forcomp/Anagrams.scala index 36eea6d..ad2ef8d 100644 --- a/src/main/scala/forcomp/Anagrams.scala +++ b/src/main/scala/forcomp/Anagrams.scala @@ -33,10 +33,10 @@ object Anagrams extends AnagramsInterface { * * Note: you must use `groupBy` to implement this method! */ - def wordOccurrences(w: Word): Occurrences = ??? + def wordOccurrences(w: Word): Occurrences = w.toLowerCase.toSeq.groupBy(e => e).view.mapValues(t => t.length).toList.sorted /** Converts a sentence into its character occurrence list. */ - def sentenceOccurrences(s: Sentence): Occurrences = ??? + def sentenceOccurrences(s: Sentence): Occurrences = wordOccurrences(s.mkString) /** The `dictionaryByOccurrences` is a `Map` from different occurrences to a sequence of all * the words that have that occurrence count. @@ -53,11 +53,13 @@ object Anagrams extends AnagramsInterface { * List(('a', 1), ('e', 1), ('t', 1)) -> Seq("ate", "eat", "tea") * */ - lazy val dictionaryByOccurrences: Map[Occurrences, List[Word]] = ??? + lazy val dictionaryByOccurrences: Map[Occurrences, List[Word]] = dictionary.groupBy(wordOccurrences) /** Returns all the anagrams of a given word. */ - def wordAnagrams(word: Word): List[Word] = ??? - + def wordAnagrams(word: Word): List[Word] = dictionaryByOccurrences(wordOccurrences(word)) + + // val o : List[Occurrences] = occurrences.map( x => (for(i <- 1 until (x._2+1)) yield (x._1,i)).toList) + // o.foldRight(List[Occurrences](Nil))((x,y) => y ++ (for(i <- x; j <- y) yield (i :: j))) /** Returns the list of all subsets of the occurrence list. * This includes the occurrence itself, i.e. `List(('k', 1), ('o', 1))` * is a subset of `List(('k', 1), ('o', 1))`. @@ -80,7 +82,10 @@ object Anagrams extends AnagramsInterface { * Note that the order of the occurrence list subsets does not matter -- the subsets * in the example above could have been displayed in some other order. */ - def combinations(occurrences: Occurrences): List[Occurrences] = ??? + def combinations(occurrences: Occurrences): List[Occurrences] = + occurrences.map( x => (for(i <- 1 until (x._2+1)) yield (x._1,i)).toList) + .foldRight(List[Occurrences](Nil))((x,y) => + y ::: (for(i <- x; j <- y) yield (i :: j))) /** Subtracts occurrence list `y` from occurrence list `x`. * @@ -92,7 +97,10 @@ object Anagrams extends AnagramsInterface { * Note: the resulting value is an occurrence - meaning it is sorted * and has no zero-entries. */ - def subtract(x: Occurrences, y: Occurrences): Occurrences = ??? + def subtract(x: Occurrences, y: Occurrences): Occurrences = + y.foldLeft(Map(x: _*))((map, pair) => { + map.updated(pair._1, map.apply(pair._1) - pair._2) + }).filter(e => e._2 !=0 ).toList.sorted /** Returns a list of all anagram sentences of the given sentence. * @@ -134,7 +142,19 @@ object Anagrams extends AnagramsInterface { * * Note: There is only one anagram of an empty sentence. */ - def sentenceAnagrams(sentence: Sentence): List[Sentence] = ??? + def sentenceAnagrams(sentence: Sentence): List[Sentence] = { + def iter(occurrences: Occurrences): List[Sentence] = { + if (occurrences.isEmpty) List(Nil) + else for { + combination <- combinations( occurrences ) + word <- dictionaryByOccurrences getOrElse (combination, Nil) + sentence <- iter( subtract(occurrences,wordOccurrences(word)) ) + if !combination.isEmpty + } yield word :: sentence + } + + iter( sentenceOccurrences(sentence) ) +} } object Dictionary {