Skip to content

Commit

Permalink
Add solutions to: example, forcomp
Browse files Browse the repository at this point in the history
  • Loading branch information
tischsoic committed Mar 19, 2020
1 parent ea4a3d7 commit 3f4a30c
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 14 deletions.
33 changes: 33 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# sbt
# (may want to keep parts of 'project')
bin/
project/
target/
build/
null/

# eclipse
build
.classpath
.project
.settings
.worksheet

# intellij idea
*.log
*.iml
*.ipr
*.iws
.idea

# mac
.DS_Store

# other?
.history
.scala_dependencies
.cache
.cache-main

#general
*.class
11 changes: 9 additions & 2 deletions example/src/main/scala/example/Lists.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ object Lists {
* @param xs A list of natural numbers
* @return The sum of all elements in `xs`
*/
def sum(xs: List[Int]): Int = ???
def sum(xs: List[Int]): Int = if (xs.isEmpty) 0 else xs.head + sum(xs.tail)

/**
* This method returns the largest element in a list of integers. If the
Expand All @@ -37,5 +37,12 @@ object Lists {
* @return The largest element in `xs`
* @throws java.util.NoSuchElementException if `xs` is an empty list
*/
def max(xs: List[Int]): Int = ???
def max(xs: List[Int]): Int = xs match {
case Nil => throw new java.util.NoSuchElementException
case x::Nil => x
case _ =>
val tailMax = max(xs.tail)

if (xs.head > tailMax) xs.head else tailMax
}
}
4 changes: 2 additions & 2 deletions example/src/test/scala/example/ListsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import org.junit.Assert.assertEquals
}

@Test def `one plus one is three (0pts)?`: Unit = {
assert(1 + 1 == 3) // This assertion fails! Go ahead and fix it.
assert(1 + 1 == 2) // This assertion fails! Go ahead and fix it.
}

/**
Expand All @@ -50,7 +50,7 @@ import org.junit.Assert.assertEquals
* when writing tests.
*/
@Test def `details why one plus one is not three (0pts)`: Unit = {
Assert.assertEquals(3, 1 + 1) // Fix me, please!
Assert.assertEquals(3, 1 + 2) // Fix me, please!
}

/**
Expand Down
33 changes: 33 additions & 0 deletions forcomp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# sbt
# (may want to keep parts of 'project')
bin/
project/
target/
build/
null/

# eclipse
build
.classpath
.project
.settings
.worksheet

# intellij idea
*.log
*.iml
*.ipr
*.iws
.idea

# mac
.DS_Store

# other?
.history
.scala_dependencies
.cache
.cache-main

#general
*.class
44 changes: 37 additions & 7 deletions forcomp/src/main/scala/forcomp/Anagrams.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ 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(identity).map({case (c, l) => (c, l.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.
Expand All @@ -53,10 +55,12 @@ 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(w => wordOccurrences(w))

/** Returns all the anagrams of a given word. */
def wordAnagrams(word: Word): List[Word] = ???
def wordAnagrams(word: Word): List[Word] =
dictionaryByOccurrences(wordOccurrences(word))

/** Returns the list of all subsets of the occurrence list.
* This includes the occurrence itself, i.e. `List(('k', 1), ('o', 1))`
Expand All @@ -80,7 +84,14 @@ 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 match {
case Nil => List(List())
case (c, n)::os =>
for (
i <- (0 to n).toList;
ot <- combinations(os)
) yield if (i == 0) ot else (c, i)::ot
}

/** Subtracts occurrence list `y` from occurrence list `x`.
*
Expand All @@ -92,7 +103,14 @@ 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 = (x, y) match {
case (Nil, _) => Nil
case (_, Nil) => x
case ((x1, m)::xs, (y1, n)::ys) =>
if (x1 == y1) (if (m <= n) subtract(xs, ys) else (x1, m - n)::subtract(xs, ys))
else if (x1 < y1) (x1, m)::subtract(xs, y)
else subtract(x, ys)
}

/** Returns a list of all anagram sentences of the given sentence.
*
Expand Down Expand Up @@ -134,7 +152,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] =
{
val occurrences = sentenceOccurrences(sentence)
def anagrams(occurrences: Occurrences): List[Sentence] =
if (occurrences == Nil) List(Nil)
else for (
occurrence <- combinations(occurrences) if dictionaryByOccurrences.contains(occurrence);
word <- dictionaryByOccurrences(occurrence);
sentence <- anagrams(subtract(occurrences, occurrence))
) yield word :: sentence

anagrams(occurrences)
}
}

object Dictionary {
Expand Down
15 changes: 15 additions & 0 deletions forcomp/src/main/scala/forcomp/a.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

val w = "Scala"

w.toLowerCase.toSeq.sorted.groupBy(identity)

w.toLowerCase.toSeq.groupBy(identity).map({case (c, l) => (c, l.length)}).toList.sorted

val n = 3
val x = 1 to n

print(x.toList)

import forcomp.Anagrams._

dictionaryByOccurrences(List(('a', 1), ('e', 1), ('t', 1)))
6 changes: 3 additions & 3 deletions forcomp/src/test/scala/forcomp/AnagramsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class AnagramsSuite {


@Test def `subtract: lard - r (10pts)`: Unit = {
val lard = List(('a', 1), ('d', 1), ('l', 1), ('r', 1))
val r = List(('r', 1))
val lard = List(('a', 1), ('d', 1), ('l', 2), ('r', 1))
val r = List(('l', 1), ('r', 1))
val lad = List(('a', 1), ('d', 1), ('l', 1))
assertEquals(lad, subtract(lard, r))
}
Expand Down Expand Up @@ -91,5 +91,5 @@ class AnagramsSuite {
}


@Rule def individualTestTimeout = new org.junit.rules.Timeout(10 * 1000)
// @Rule def individualTestTimeout = new org.junit.rules.Timeout(10 * 1000)
}

0 comments on commit 3f4a30c

Please sign in to comment.