Finished assignement 5
This commit is contained in:
parent
b1b43ca6e6
commit
604ff923ff
@ -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 {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user