Compare commits
No commits in common. "submission-patmat" and "codecs" have entirely different histories.
submission
...
codecs
@ -7,7 +7,9 @@ stages:
|
||||
|
||||
compile:
|
||||
stage: build
|
||||
image: lampepfl/moocs-dotty:2019-09-17-2
|
||||
image: lampepfl/moocs-dotty:2019-10-16
|
||||
except:
|
||||
- tags
|
||||
tags:
|
||||
- cs210
|
||||
script:
|
||||
@ -19,10 +21,12 @@ compile:
|
||||
|
||||
grade:
|
||||
stage: grade
|
||||
except:
|
||||
- tags
|
||||
tags:
|
||||
- cs210
|
||||
image:
|
||||
name: registry.gitlab.com/fnux/cs210-grading-images/progfun1-patmat:20191009-626d0012efc94653bff8736b2570386000f65ea2
|
||||
name: registry.gitlab.com/fnux/cs210-grading-images/progfun2-codecs:20191027-dfbea8aed96096ed3af1cf1958549b97328d4c25
|
||||
entrypoint: [""]
|
||||
allow_failure: true
|
||||
before_script:
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
# CS-210: Pattern Matching (Huffman)
|
||||
|
||||
# CS-210: Codecs
|
||||
|
||||
Please follow the [instructions from the main course
|
||||
respository](https://gitlab.epfl.ch/lamp/cs-210-functional-programming-2019/blob/master/week4/00-homework4.md).
|
||||
respository](https://gitlab.epfl.ch/lamp/cs-210-functional-programming-2019/blob/master/week11/00-homework8.md).
|
||||
|
||||
Grading and submission details can be found [here](https://gitlab.epfl.ch/lamp/cs-210-functional-programming-2019/blob/master/week1/02-grading-and-submission.md).
|
||||
|
||||
20
build.sbt
20
build.sbt
@ -1,12 +1,16 @@
|
||||
course := "progfun1"
|
||||
assignment := "patmat"
|
||||
course := "progfun2"
|
||||
assignment := "codecs"
|
||||
name := course.value + "-" + assignment.value
|
||||
testSuite := "patmat.HuffmanSuite"
|
||||
testSuite := "codecs.CodecsSuite"
|
||||
|
||||
scalaVersion := "0.19.0-bin-20190918-dd68eb8-NIGHTLY"
|
||||
|
||||
scalacOptions ++= Seq("-language:implicitConversions", "-deprecation")
|
||||
|
||||
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % Test
|
||||
scalaVersion := "0.19.0-RC1"
|
||||
scalacOptions ++= Seq("-deprecation")
|
||||
libraryDependencies ++= Seq(
|
||||
("org.scalacheck" %% "scalacheck" % "1.14.2" % Test).withDottyCompat(scalaVersion.value),
|
||||
("org.typelevel" %% "jawn-parser" % "0.14.2").withDottyCompat(scalaVersion.value),
|
||||
"com.novocode" % "junit-interface" % "0.11" % Test
|
||||
)
|
||||
|
||||
testOptions in Test += Tests.Argument(TestFrameworks.JUnit, "-a", "-v", "-s")
|
||||
|
||||
initialCommands in console := """import codecs.{_, given}"""
|
||||
|
||||
Binary file not shown.
@ -18,6 +18,8 @@ object MOOCSettings extends AutoPlugin {
|
||||
override def trigger = allRequirements
|
||||
|
||||
override val projectSettings: Seq[Def.Setting[_]] = Seq(
|
||||
parallelExecution in Test := false
|
||||
parallelExecution in Test := false,
|
||||
// Report test result after each test instead of waiting for every test to finish
|
||||
logBuffered in Test := false
|
||||
)
|
||||
}
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
libraryDependencies += "com.novocode" % "junit-interface" % "0.11" % Test
|
||||
// Used for base64 encoding
|
||||
libraryDependencies += "commons-codec" % "commons-codec" % "1.10"
|
||||
|
||||
|
||||
283
src/main/scala/codecs/codecs.scala
Normal file
283
src/main/scala/codecs/codecs.scala
Normal file
@ -0,0 +1,283 @@
|
||||
package codecs
|
||||
|
||||
/**
|
||||
* A data type modeling JSON values.
|
||||
*
|
||||
* For example, the `42` integer JSON value can be modeled as `Json.Num(42)`
|
||||
*/
|
||||
sealed trait Json {
|
||||
/**
|
||||
* Try to decode this JSON value into a value of type `A` by using
|
||||
* the given decoder.
|
||||
*
|
||||
* Note that you have to explicitly fix `A` type parameter when you call the method:
|
||||
*
|
||||
* {{{
|
||||
* someJsonValue.decodeAs[User] // OK
|
||||
* someJsonValue.decodeAs // Wrong!
|
||||
* }}}
|
||||
*/
|
||||
def decodeAs[A](given decoder: Decoder[A]): Option[A] = decoder.decode(this)
|
||||
}
|
||||
|
||||
object Json {
|
||||
/** The JSON `null` value */
|
||||
case object Null extends Json
|
||||
/** JSON boolean values */
|
||||
case class Bool(value: Boolean) extends Json
|
||||
/** JSON numeric values */
|
||||
case class Num(value: BigDecimal) extends Json
|
||||
/** JSON string values */
|
||||
case class Str(value: String) extends Json
|
||||
/** JSON objects */
|
||||
case class Obj(fields: Map[String, Json]) extends Json
|
||||
/** JSON arrays */
|
||||
case class Arr(items: List[Json]) extends Json
|
||||
}
|
||||
|
||||
/**
|
||||
* A type class that turns a value of type `A` into its JSON representation.
|
||||
*/
|
||||
trait Encoder[-A] {
|
||||
|
||||
def encode(value: A): Json
|
||||
|
||||
/**
|
||||
* Transforms this `Encoder[A]` into an `Encoder[B]`, given a transformation function
|
||||
* from `B` to `A`.
|
||||
*
|
||||
* For instance, given a `Encoder[String]`, we can get an `Encoder[UUID]`:
|
||||
*
|
||||
* {{{
|
||||
* def uuidEncoder(given stringEncoder: Encoder[String]): Encoder[UUID] =
|
||||
* stringEncoder.transform[UUID](uuid => uuid.toString)
|
||||
* }}}
|
||||
*
|
||||
* This operation is also known as ?contramap?.
|
||||
*/
|
||||
def transform[B](f: B => A): Encoder[B] =
|
||||
Encoder.fromFunction[B](value => this.encode(f(value)))
|
||||
}
|
||||
|
||||
object Encoder extends GivenEncoders {
|
||||
|
||||
/**
|
||||
* Convenient method for creating an instance of encoder from a function `f`
|
||||
*/
|
||||
def fromFunction[A](f: A => Json) = new Encoder[A] {
|
||||
def encode(value: A): Json = f(value)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
trait GivenEncoders {
|
||||
|
||||
/** An encoder for the `Unit` value */
|
||||
given Encoder[Unit] = Encoder.fromFunction(_ => Json.Null)
|
||||
|
||||
/** An encoder for `Int` values */
|
||||
given Encoder[Int] = Encoder.fromFunction(n => Json.Num(BigDecimal(n)))
|
||||
|
||||
/** An encoder for `String` values */
|
||||
given Encoder[String] =
|
||||
Encoder.fromFunction(str => Json.Str(str))
|
||||
|
||||
/** An encoder for `Boolean` values */
|
||||
given Encoder[Boolean] =
|
||||
Encoder.fromFunction(v => Json.Bool(v))
|
||||
|
||||
/**
|
||||
* Encodes a list of values of type `A` into a JSON array containing
|
||||
* the list elements encoded with the given `encoder`
|
||||
*/
|
||||
given [A](given encoder: Encoder[A]): Encoder[List[A]] =
|
||||
Encoder.fromFunction(as => Json.Arr(as.map(encoder.encode)))
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* A specialization of `Encoder` that returns JSON objects only
|
||||
*/
|
||||
trait ObjectEncoder[-A] extends Encoder[A] {
|
||||
// Refines the encoding result to `Json.Obj`
|
||||
def encode(value: A): Json.Obj
|
||||
|
||||
/**
|
||||
* Combines `this` encoder with `that` encoder.
|
||||
* Returns an encoder producing a JSON object containing both
|
||||
* fields of `this` encoder and fields of `that` encoder.
|
||||
*/
|
||||
def zip[B](that: ObjectEncoder[B]): ObjectEncoder[(A, B)] =
|
||||
ObjectEncoder.fromFunction { (a, b) =>
|
||||
Json.Obj(this.encode(a).fields ++ that.encode(b).fields)
|
||||
}
|
||||
}
|
||||
|
||||
object ObjectEncoder {
|
||||
|
||||
/**
|
||||
* Convenient method for creating an instance of object encoder from a function `f`
|
||||
*/
|
||||
def fromFunction[A](f: A => Json.Obj): ObjectEncoder[A] = new ObjectEncoder[A] {
|
||||
def encode(value: A): Json.Obj = f(value)
|
||||
}
|
||||
|
||||
/**
|
||||
* An encoder for values of type `A` that produces a JSON object with one field
|
||||
* named according to the supplied `name` and containing the encoded value.
|
||||
*/
|
||||
def field[A](name: String)(given encoder: Encoder[A]): ObjectEncoder[A] =
|
||||
ObjectEncoder.fromFunction(a => Json.Obj(Map(name -> encoder.encode(a))))
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* The dual of an encoder. Decodes a serialized value into its initial type `A`.
|
||||
*/
|
||||
trait Decoder[+A] {
|
||||
/**
|
||||
* @param data The data to de-serialize
|
||||
* @return The decoded value wrapped in `Some`, or `None` if decoding failed
|
||||
*/
|
||||
def decode(data: Json): Option[A]
|
||||
|
||||
/**
|
||||
* Combines `this` decoder with `that` decoder.
|
||||
* Returns a decoder that invokes both `this` decoder and `that`
|
||||
* decoder and returns a pair of decoded value in case both succeed,
|
||||
* or `None` if at least one failed.
|
||||
*/
|
||||
def zip[B](that: Decoder[B]): Decoder[(A, B)] =
|
||||
Decoder.fromFunction { json =>
|
||||
this.decode(json).zip(that.decode(json))
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms this `Decoder[A]` into a `Decoder[B]`, given a transformation function
|
||||
* from `A` to `B`.
|
||||
*
|
||||
* This operation is also known as ?map?.
|
||||
*/
|
||||
def transform[B](f: A => B): Decoder[B] =
|
||||
Decoder.fromFunction(json => this.decode(json).map(f))
|
||||
}
|
||||
|
||||
object Decoder extends GivenDecoders {
|
||||
|
||||
/**
|
||||
* Convenient method to build a decoder instance from a function `f`
|
||||
*/
|
||||
def fromFunction[A](f: Json => Option[A]): Decoder[A] = new Decoder[A] {
|
||||
def decode(data: Json): Option[A] = f(data)
|
||||
}
|
||||
|
||||
/**
|
||||
* Alternative method for creating decoder instances
|
||||
*/
|
||||
def fromPartialFunction[A](pf: PartialFunction[Json, A]): Decoder[A] =
|
||||
fromFunction(pf.lift)
|
||||
|
||||
}
|
||||
|
||||
trait GivenDecoders {
|
||||
|
||||
/** A decoder for the `Unit` value */
|
||||
given Decoder[Unit] =
|
||||
Decoder.fromPartialFunction { case Json.Null => () }
|
||||
|
||||
/** A decoder for `Int` values. Hint: use the `isValidInt` method of `BigDecimal`. */
|
||||
// TODO Define a given `Decoder[Int]` instance
|
||||
given Decoder[Int] =
|
||||
Decoder.fromFunction{ case Json.Num(v) => if v.isValidInt then Some(v.intValue) else None
|
||||
case _ => None}
|
||||
|
||||
/** A decoder for `String` values */
|
||||
// TODO Define a given `Decoder[String]` instance
|
||||
given Decoder[String] =
|
||||
Decoder.fromPartialFunction{ case Json.Str(str) => str}
|
||||
|
||||
/** A decoder for `Boolean` values */
|
||||
// TODO Define a given `Decoder[Boolean]` instance
|
||||
given Decoder[Boolean] =
|
||||
Decoder.fromPartialFunction{ case Json.Bool(v) => v}
|
||||
/**
|
||||
* A decoder for JSON arrays. It decodes each item of the array
|
||||
* using the given `decoder`. The resulting decoder succeeds only
|
||||
* if all the JSON array items are successfully decoded.
|
||||
*/
|
||||
given [A](given decoder: Decoder[A]): Decoder[List[A]] =
|
||||
Decoder.fromFunction {
|
||||
case Json.Arr(items: List[Json]) => Some(items.map(v => decoder.decode(v).get))
|
||||
case _ => None
|
||||
}
|
||||
|
||||
/**
|
||||
* A decoder for JSON objects. It decodes the value of a field of
|
||||
* the supplied `name` using the given `decoder`.
|
||||
*/
|
||||
def field[A](name: String)(given decoder: Decoder[A]): Decoder[A] =
|
||||
Decoder.fromFunction{
|
||||
case Json.Obj(field: Map[String, Json]) => decoder.decode(field.get(name).get)
|
||||
case _ => None
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
case class Person(name: String, age: Int)
|
||||
|
||||
object Person extends PersonCodecs
|
||||
|
||||
trait PersonCodecs {
|
||||
|
||||
/** The encoder for `Person` */
|
||||
given Encoder[Person] =
|
||||
ObjectEncoder.field[String]("name")
|
||||
.zip(ObjectEncoder.field[Int]("age"))
|
||||
.transform[Person](user => (user.name, user.age))
|
||||
|
||||
/** The corresponding decoder for `Person` */
|
||||
given Decoder[Person] ={
|
||||
Decoder.field[String]("name").zip(Decoder.field[Int]("age")).transform[Person](user => Person(user._1, user._2))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
case class Contacts(people: List[Person])
|
||||
|
||||
object Contacts extends ContactsCodecs
|
||||
|
||||
trait ContactsCodecs {
|
||||
|
||||
// TODO Define the encoder and the decoder for `Contacts`
|
||||
// The JSON representation of a value of type `Contacts` should be
|
||||
// a JSON object with a single field named ?people? containing an
|
||||
// array of values of type `Person` (reuse the `Person` codecs)
|
||||
given Encoder[Contacts] =
|
||||
ObjectEncoder.field[List[Person]]("people").transform[Contacts](c => c.people)
|
||||
|
||||
given Decoder[Contacts] =
|
||||
Decoder.field[List[Person]]("people").transform[Contacts](p => Contacts(p))
|
||||
|
||||
}
|
||||
|
||||
// In case you want to try your code, here is a simple `Main`
|
||||
// that can be used as a starting point. Otherwise, you can use
|
||||
// the REPL (use the `console` sbt task).
|
||||
object Main {
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
println(renderJson(42))
|
||||
println(renderJson("foo"))
|
||||
|
||||
val maybeJsonString = parseJson(""" "foo" """)
|
||||
val maybeJsonObj = parseJson(""" { "name": "Alice", "age": 42 } """)
|
||||
val maybeJsonObj2 = parseJson(""" { "name": "Alice", "age": "42" } """)
|
||||
// Uncomment the following lines as you progress in the assignment
|
||||
println(maybeJsonString.flatMap(_.decodeAs[Int]))
|
||||
println(maybeJsonString.flatMap(_.decodeAs[String]))
|
||||
println(maybeJsonObj.flatMap(_.decodeAs[Person]))
|
||||
println(maybeJsonObj2.flatMap(_.decodeAs[Person]))
|
||||
println(renderJson(Person("Bob", 66)))
|
||||
}
|
||||
|
||||
}
|
||||
74
src/main/scala/codecs/json.scala
Normal file
74
src/main/scala/codecs/json.scala
Normal file
@ -0,0 +1,74 @@
|
||||
package codecs
|
||||
|
||||
import org.typelevel.jawn.{ Parser, SimpleFacade }
|
||||
import scala.collection.mutable
|
||||
import scala.util.Try
|
||||
|
||||
// Utility methods that decode values from `String` JSON blobs, and
|
||||
// render values to `String` JSON blobs
|
||||
|
||||
/**
|
||||
* Parse a JSON document contained in a `String` value into a `Json` value, returns
|
||||
* `None` in case the supplied `s` value is not a valid JSON document.
|
||||
*/
|
||||
def parseJson(s: String): Option[Json] = Parser.parseFromString[Json](s).toOption
|
||||
|
||||
/**
|
||||
* Parse the JSON value from the supplied `s` parameter, and then try to decode
|
||||
* it as a value of type `A` using the given `decoder`.
|
||||
*
|
||||
* Returns `None` if JSON parsing failed, or if decoding failed.
|
||||
*/
|
||||
def parseAndDecode[A](s: String)(given decoder: Decoder[A]): Option[A] =
|
||||
for {
|
||||
json <- parseJson(s)
|
||||
a <- decoder.decode(json)
|
||||
} yield a
|
||||
|
||||
/**
|
||||
* Render the supplied `value` into JSON using the given `encoder`.
|
||||
*/
|
||||
def renderJson[A](value: A)(given encoder: Encoder[A]): String =
|
||||
render(encoder.encode(value))
|
||||
|
||||
private def render(json: Json): String = json match {
|
||||
case Json.Null => "null"
|
||||
case Json.Bool(b) => b.toString
|
||||
case Json.Num(n) => n.toString
|
||||
case Json.Str(s) => renderString(s)
|
||||
case Json.Arr(vs) => vs.map(render).mkString("[", ",", "]")
|
||||
case Json.Obj(vs) => vs.map { case (k, v) => s"${renderString(k)}:${render(v)}" }.mkString("{", ",", "}")
|
||||
}
|
||||
|
||||
private def renderString(s: String): String = {
|
||||
val sb = new StringBuilder
|
||||
sb.append('"')
|
||||
var i = 0
|
||||
val len = s.length
|
||||
while (i < len) {
|
||||
s.charAt(i) match {
|
||||
case '"' => sb.append("\\\"")
|
||||
case '\\' => sb.append("\\\\")
|
||||
case '\b' => sb.append("\\b")
|
||||
case '\f' => sb.append("\\f")
|
||||
case '\n' => sb.append("\\n")
|
||||
case '\r' => sb.append("\\r")
|
||||
case '\t' => sb.append("\\t")
|
||||
case c =>
|
||||
if (c < ' ') sb.append("\\u%04x" format c.toInt)
|
||||
else sb.append(c)
|
||||
}
|
||||
i += 1
|
||||
}
|
||||
sb.append('"').toString
|
||||
}
|
||||
|
||||
given SimpleFacade[Json] {
|
||||
def jnull() = Json.Null
|
||||
def jtrue() = Json.Bool(true)
|
||||
def jfalse() = Json.Bool(false)
|
||||
def jnum(s: CharSequence, decIndex: Int, expIndex: Int) = Json.Num(BigDecimal(s.toString))
|
||||
def jstring(s: CharSequence) = Json.Str(s.toString)
|
||||
def jarray(vs: List[Json]) = Json.Arr(vs)
|
||||
def jobject(vs: Map[String, Json]) = Json.Obj(vs)
|
||||
}
|
||||
@ -1,238 +0,0 @@
|
||||
package patmat
|
||||
|
||||
/**
|
||||
* A huffman code is represented by a binary tree.
|
||||
*
|
||||
* Every `Leaf` node of the tree represents one character of the alphabet that the tree can encode.
|
||||
* The weight of a `Leaf` is the frequency of appearance of the character.
|
||||
*
|
||||
* The branches of the huffman tree, the `Fork` nodes, represent a set containing all the characters
|
||||
* present in the leaves below it. The weight of a `Fork` node is the sum of the weights of these
|
||||
* leaves.
|
||||
*/
|
||||
abstract class CodeTree
|
||||
case class Fork(left: CodeTree, right: CodeTree, chars: List[Char], weight: Int) extends CodeTree
|
||||
case class Leaf(char: Char, weight: Int) extends CodeTree
|
||||
|
||||
/**
|
||||
* Assignment 4: Huffman coding
|
||||
*
|
||||
*/
|
||||
trait Huffman extends HuffmanInterface {
|
||||
|
||||
// Part 1: Basics
|
||||
def weight(tree: CodeTree): Int = tree match{
|
||||
case Fork(_,_,_, weight) => weight
|
||||
case Leaf(_, weight) => weight
|
||||
} // tree match ...
|
||||
|
||||
def chars(tree: CodeTree): List[Char] = tree match{
|
||||
case Fork(_,_,chars,_) => chars
|
||||
case Leaf(char,_) => List(char)
|
||||
} // tree match ...
|
||||
|
||||
def makeCodeTree(left: CodeTree, right: CodeTree) =
|
||||
Fork(left, right, chars(left) ::: chars(right), weight(left) + weight(right))
|
||||
|
||||
// Part 2: Generating Huffman trees
|
||||
|
||||
/**
|
||||
* In this assignment, we are working with lists of characters. This function allows
|
||||
* you to easily create a character list from a given string.
|
||||
*/
|
||||
def string2Chars(str: String): List[Char] = str.toList
|
||||
|
||||
/**
|
||||
* This function computes for each unique character in the list `chars` the number of
|
||||
* times it occurs. For example, the invocation
|
||||
*
|
||||
* times(List('a', 'b', 'a'))
|
||||
*
|
||||
* should return the following (the order of the resulting list is not important):
|
||||
*
|
||||
* List(('a', 2), ('b', 1))
|
||||
*
|
||||
* The type `List[(Char, Int)]` denotes a list of pairs, where each pair consists of a
|
||||
* character and an integer. Pairs can be constructed easily using parentheses:
|
||||
*
|
||||
* val pair: (Char, Int) = ('c', 1)
|
||||
*
|
||||
* In order to access the two elements of a pair, you can use the accessors `_1` and `_2`:
|
||||
*
|
||||
* val theChar = pair._1
|
||||
* val theInt = pair._2
|
||||
*
|
||||
* Another way to deconstruct a pair is using pattern matching:
|
||||
*
|
||||
* pair match {
|
||||
* case (theChar, theInt) =>
|
||||
* println("character is: "+ theChar)
|
||||
* println("integer is : "+ theInt)
|
||||
* }
|
||||
*/
|
||||
def times(chars: List[Char]): List[(Char, Int)] = {
|
||||
chars.groupBy(x => x).map(t => (t._1, t._2.length)).iterator.toList
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of `Leaf` nodes for a given frequency table `freqs`.
|
||||
*
|
||||
* The returned list should be ordered by ascending weights (i.e. the
|
||||
* head of the list should have the smallest weight), where the weight
|
||||
* of a leaf is the frequency of the character.
|
||||
*/
|
||||
def makeOrderedLeafList(freqs: List[(Char, Int)]): List[Leaf] =
|
||||
freqs.sortBy(_._2).map(pair => new Leaf(pair._1, pair._2))
|
||||
|
||||
/**
|
||||
* Checks whether the list `trees` contains only one single code tree.
|
||||
*/
|
||||
def singleton(trees: List[CodeTree]): Boolean = trees.size == 1
|
||||
|
||||
/**
|
||||
* The parameter `trees` of this function is a list of code trees ordered
|
||||
* by ascending weights.
|
||||
*
|
||||
* This function takes the first two elements of the list `trees` and combines
|
||||
* them into a single `Fork` node. This node is then added back into the
|
||||
* remaining elements of `trees` at a position such that the ordering by weights
|
||||
* is preserved.
|
||||
*
|
||||
* If `trees` is a list of less than two elements, that list should be returned
|
||||
* unchanged.
|
||||
*/
|
||||
def combine(trees: List[CodeTree]): List[CodeTree] = {
|
||||
val ordered = trees.sortBy(weight)
|
||||
if (trees.isEmpty)
|
||||
trees
|
||||
else if (singleton(trees))
|
||||
trees
|
||||
else
|
||||
makeCodeTree(ordered.head, ordered.tail.head) :: ordered.tail.tail
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will be called in the following way:
|
||||
*
|
||||
* until(singleton, combine)(trees)
|
||||
*
|
||||
* where `trees` is of type `List[CodeTree]`, `singleton` and `combine` refer to
|
||||
* the two functions defined above.
|
||||
*
|
||||
* In such an invocation, `until` should call the two functions until the list of
|
||||
* code trees contains only one single tree, and then return that singleton list.
|
||||
*/
|
||||
def until(done: List[CodeTree] => Boolean, merge: List[CodeTree] => List[CodeTree])(trees: List[CodeTree]): List[CodeTree] =
|
||||
if(done(trees))
|
||||
trees
|
||||
else
|
||||
until(done, merge)(merge(trees))
|
||||
|
||||
/**
|
||||
* This function creates a code tree which is optimal to encode the text `chars`.
|
||||
*
|
||||
* The parameter `chars` is an arbitrary text. This function extracts the character
|
||||
* frequencies from that text and creates a code tree based on them.
|
||||
*/
|
||||
def createCodeTree(chars: List[Char]): CodeTree =
|
||||
until(singleton, combine)(makeOrderedLeafList(times(chars))).head
|
||||
|
||||
|
||||
// Part 3: Decoding
|
||||
|
||||
type Bit = Int
|
||||
|
||||
/**
|
||||
* This function decodes the bit sequence `bits` using the code tree `tree` and returns
|
||||
* the resulting list of characters.
|
||||
*/
|
||||
def decode(tree: CodeTree, bits: List[Bit]): List[Char] = {
|
||||
def acc(rest: CodeTree, bits: List[Bit]): List[Char] =
|
||||
rest match {
|
||||
case Leaf (c, _) => if (bits.isEmpty) List(c) else c :: acc(tree, bits)
|
||||
case Fork(l, r, _, _) => if (bits.head == 0) acc(l, bits.tail) else acc(r, bits.tail)
|
||||
}
|
||||
acc(tree, bits)
|
||||
}
|
||||
|
||||
/**
|
||||
* A Huffman coding tree for the French language.
|
||||
* Generated from the data given at
|
||||
* http://fr.wikipedia.org/wiki/Fr%C3%A9quence_d%27apparition_des_lettres_en_fran%C3%A7ais
|
||||
*/
|
||||
val frenchCode: CodeTree = Fork(Fork(Fork(Leaf('s',121895),Fork(Leaf('d',56269),Fork(Fork(Fork(Leaf('x',5928),Leaf('j',8351),List('x','j'),14279),Leaf('f',16351),List('x','j','f'),30630),Fork(Fork(Fork(Fork(Leaf('z',2093),Fork(Leaf('k',745),Leaf('w',1747),List('k','w'),2492),List('z','k','w'),4585),Leaf('y',4725),List('z','k','w','y'),9310),Leaf('h',11298),List('z','k','w','y','h'),20608),Leaf('q',20889),List('z','k','w','y','h','q'),41497),List('x','j','f','z','k','w','y','h','q'),72127),List('d','x','j','f','z','k','w','y','h','q'),128396),List('s','d','x','j','f','z','k','w','y','h','q'),250291),Fork(Fork(Leaf('o',82762),Leaf('l',83668),List('o','l'),166430),Fork(Fork(Leaf('m',45521),Leaf('p',46335),List('m','p'),91856),Leaf('u',96785),List('m','p','u'),188641),List('o','l','m','p','u'),355071),List('s','d','x','j','f','z','k','w','y','h','q','o','l','m','p','u'),605362),Fork(Fork(Fork(Leaf('r',100500),Fork(Leaf('c',50003),Fork(Leaf('v',24975),Fork(Leaf('g',13288),Leaf('b',13822),List('g','b'),27110),List('v','g','b'),52085),List('c','v','g','b'),102088),List('r','c','v','g','b'),202588),Fork(Leaf('n',108812),Leaf('t',111103),List('n','t'),219915),List('r','c','v','g','b','n','t'),422503),Fork(Leaf('e',225947),Fork(Leaf('i',115465),Leaf('a',117110),List('i','a'),232575),List('e','i','a'),458522),List('r','c','v','g','b','n','t','e','i','a'),881025),List('s','d','x','j','f','z','k','w','y','h','q','o','l','m','p','u','r','c','v','g','b','n','t','e','i','a'),1486387)
|
||||
|
||||
/**
|
||||
* What does the secret message say? Can you decode it?
|
||||
* For the decoding use the `frenchCode' Huffman tree defined above.
|
||||
*/
|
||||
val secret: List[Bit] = List(0,0,1,1,1,0,1,0,1,1,1,0,0,1,1,0,1,0,0,1,1,0,1,0,1,1,0,0,1,1,1,1,1,0,1,0,1,1,0,0,0,0,1,0,1,1,1,0,0,1,0,0,1,0,0,0,1,0,0,0,1,0,1)
|
||||
|
||||
/**
|
||||
* Write a function that returns the decoded secret
|
||||
*/
|
||||
def decodedSecret: List[Char] = decode(frenchCode, secret)
|
||||
|
||||
// Part 4a: Encoding using Huffman tree
|
||||
|
||||
/**
|
||||
* This function encodes `text` using the code tree `tree`
|
||||
* into a sequence of bits.
|
||||
*/
|
||||
def encode(tree: CodeTree)(text: List[Char]): List[Bit] = {
|
||||
def encodeChar(tree: CodeTree)(char: Char): List[Bit] = tree match {
|
||||
case Leaf(_, _) => Nil
|
||||
case Fork(l, r, _, _) => if (chars(l).contains(char)) 0 :: encodeChar(l)(char) else 1 :: encodeChar(r)(char)
|
||||
}
|
||||
|
||||
text.flatMap(encodeChar(tree))
|
||||
}
|
||||
|
||||
// Part 4b: Encoding using code table
|
||||
|
||||
type CodeTable = List[(Char, List[Bit])]
|
||||
|
||||
/**
|
||||
* This function returns the bit sequence that represents the character `char` in
|
||||
* the code table `table`.
|
||||
*/
|
||||
def codeBits(table: CodeTable)(char: Char): List[Bit] =
|
||||
table.filter((codeChar) => codeChar._1 == char).head._2
|
||||
|
||||
/**
|
||||
* Given a code tree, create a code table which contains, for every character in the
|
||||
* code tree, the sequence of bits representing that character.
|
||||
*
|
||||
* Hint: think of a recursive solution: every sub-tree of the code tree `tree` is itself
|
||||
* a valid code tree that can be represented as a code table. Using the code tables of the
|
||||
* sub-trees, think of how to build the code table for the entire tree.
|
||||
*/
|
||||
def convert(tree: CodeTree): CodeTable = tree match {
|
||||
case Leaf(c, w) => List( (c, List()) )
|
||||
case Fork(l, r, _, _) => mergeCodeTables(convert(l), convert(r))
|
||||
}
|
||||
|
||||
/**
|
||||
* This function takes two code tables and merges them into one. Depending on how you
|
||||
* use it in the `convert` method above, this merge method might also do some transformations
|
||||
* on the two parameter code tables.
|
||||
*/
|
||||
def mergeCodeTables(a: CodeTable, b: CodeTable): CodeTable = {
|
||||
a.map(code => (code._1, 0 :: code._2)) ::: b.map(code => (code._1, 1 :: code._2))
|
||||
}
|
||||
/**
|
||||
* This function encodes `text` according to the code tree `tree`.
|
||||
*
|
||||
* To speed up the encoding process, it first converts the code tree to a code table
|
||||
* and then uses it to perform the actual encoding.
|
||||
*/
|
||||
def quickEncode(tree: CodeTree)(text: List[Char]): List[Bit] = text.flatMap(codeBits(convert(tree)))
|
||||
}
|
||||
|
||||
object Huffman extends Huffman
|
||||
|
||||
object Main extends App {
|
||||
import Huffman._
|
||||
|
||||
println( decodedSecret )
|
||||
}
|
||||
@ -1,23 +0,0 @@
|
||||
package patmat
|
||||
|
||||
/**
|
||||
* The interface used by the grading infrastructure. Do not change signatures
|
||||
* or your submission will fail with a NoSuchMethodError.
|
||||
*/
|
||||
trait HuffmanInterface {
|
||||
def weight(tree: CodeTree): Int
|
||||
def chars(tree: CodeTree): List[Char]
|
||||
def times(chars: List[Char]): List[(Char, Int)]
|
||||
def makeOrderedLeafList(freqs: List[(Char, Int)]): List[Leaf]
|
||||
def singleton(trees: List[CodeTree]): Boolean
|
||||
def combine(trees: List[CodeTree]): List[CodeTree]
|
||||
def until(done: List[CodeTree] => Boolean, merge: List[CodeTree] => List[CodeTree])(trees: List[CodeTree]): List[CodeTree]
|
||||
def createCodeTree(chars: List[Char]): CodeTree
|
||||
def decode(tree: CodeTree, bits: List[Int]): List[Char]
|
||||
def decodedSecret: List[Char]
|
||||
def encode(tree: CodeTree)(text: List[Char]): List[Int]
|
||||
def convert(tree: CodeTree): List[(Char, List[Int])]
|
||||
def quickEncode(tree: CodeTree)(text: List[Char]): List[Int]
|
||||
def frenchCode: CodeTree
|
||||
def secret: List[Int]
|
||||
}
|
||||
115
src/test/scala/codecs/CodecsSuite.scala
Normal file
115
src/test/scala/codecs/CodecsSuite.scala
Normal file
@ -0,0 +1,115 @@
|
||||
package codecs
|
||||
|
||||
import org.scalacheck
|
||||
import org.scalacheck.{ Gen, Prop }
|
||||
import org.scalacheck.Prop.propBoolean
|
||||
import org.junit.{ Assert, Test }
|
||||
import scala.reflect.ClassTag
|
||||
|
||||
class CodecsSuite extends GivenEncoders, GivenDecoders, PersonCodecs, ContactsCodecs, TestEncoders, TestDecoders {
|
||||
|
||||
def checkProperty(prop: Prop): Unit = {
|
||||
val result = scalacheck.Test.check(scalacheck.Test.Parameters.default, prop)
|
||||
def fail(labels: Set[String], fallback: String): Nothing =
|
||||
if labels.isEmpty then throw new AssertionError(fallback)
|
||||
else throw new AssertionError(labels.mkString(". "))
|
||||
result.status match {
|
||||
case scalacheck.Test.Passed | _: scalacheck.Test.Proved => ()
|
||||
case scalacheck.Test.Failed(_, labels) => fail(labels, "A property failed.")
|
||||
case scalacheck.Test.PropException(_, e, labels) => fail(labels, s"An exception was thrown during property evaluation: $e.")
|
||||
case scalacheck.Test.Exhausted => fail(Set.empty, "Unable to generate data.")
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check that a value of an arbitrary type `A` can be encoded and then successfully
|
||||
* decoded with the given pair of encoder and decoder.
|
||||
*/
|
||||
def encodeAndThenDecodeProp[A](a: A)(given encA: Encoder[A], decA: Decoder[A]): Prop = {
|
||||
val maybeDecoded = decA.decode(encA.encode(a))
|
||||
maybeDecoded.contains(a) :| s"Encoded value '$a' was not successfully decoded. Got '$maybeDecoded'."
|
||||
}
|
||||
|
||||
@Test def `it is possible to encode and decode the 'Unit' value (0pts)`(): Unit = {
|
||||
checkProperty(Prop.forAll((unit: Unit) => encodeAndThenDecodeProp(unit)))
|
||||
}
|
||||
|
||||
@Test def `it is possible to encode and decode 'Int' values (1pt)`(): Unit = {
|
||||
checkProperty(Prop.forAll((x: Int) => encodeAndThenDecodeProp(x)))
|
||||
}
|
||||
|
||||
@Test def `the 'Int' decoder should reject invalid 'Int' values (2pts)`(): Unit = {
|
||||
val decoded = summon[Decoder[Int]].decode(Json.Num(4.2))
|
||||
assert(decoded.isEmpty, "decoding 4.2 as an integer value should fail")
|
||||
}
|
||||
|
||||
@Test def `a 'String' value should be encoded as a JSON string (1pt)`(): Unit = {
|
||||
assert(summon[Encoder[String]].encode("foo") == Json.Str("foo"))
|
||||
}
|
||||
|
||||
@Test def `it is possible to encode and decode 'String' values (1pt)`(): Unit = {
|
||||
checkProperty(Prop.forAll((s: String) => encodeAndThenDecodeProp(s)))
|
||||
}
|
||||
|
||||
@Test def `a 'Boolean' value should be encoded as a JSON boolean (1pt)`(): Unit = {
|
||||
val encoder = summon[Encoder[Boolean]]
|
||||
assert(encoder.encode(true) == Json.Bool(true))
|
||||
assert(encoder.encode(false) == Json.Bool(false))
|
||||
}
|
||||
|
||||
@Test def `it is possible to encode and decode 'Boolean' values (1pt)`(): Unit = {
|
||||
checkProperty(Prop.forAll((b: Boolean) => encodeAndThenDecodeProp(b)))
|
||||
}
|
||||
|
||||
@Test def `a 'List[A]' value should be encoded as a JSON array (0pts)`(): Unit = {
|
||||
val xs = 1 :: 2 :: Nil
|
||||
val encoder = summon[Encoder[List[Int]]]
|
||||
assert(encoder.encode(xs) == Json.Arr(List(Json.Num(1), Json.Num(2))))
|
||||
}
|
||||
|
||||
@Test def `it is possible to encode and decode lists (5pts)`(): Unit = {
|
||||
checkProperty(Prop.forAll((xs: List[Int]) => encodeAndThenDecodeProp(xs)))
|
||||
}
|
||||
|
||||
@Test def `a 'Person' value should be encoded as a JSON object (1pt)`(): Unit = {
|
||||
val person = Person("Alice", 42)
|
||||
val json = Json.Obj(Map("name" -> Json.Str("Alice"), "age" -> Json.Num(42)))
|
||||
val encoder = summon[Encoder[Person]]
|
||||
assert(encoder.encode(person) == json)
|
||||
}
|
||||
|
||||
@Test def `it is possible to encode and decode people (4pts)`(): Unit = {
|
||||
checkProperty(Prop.forAll((s: String, x: Int) => encodeAndThenDecodeProp(Person(s, x))))
|
||||
}
|
||||
|
||||
@Test def `a 'Contacts' value should be encoded as a JSON object (1pt)`(): Unit = {
|
||||
val contacts = Contacts(List(Person("Alice", 42)))
|
||||
val json = Json.Obj(Map("people" ->
|
||||
Json.Arr(List(Json.Obj(Map("name" -> Json.Str("Alice"), "age" -> Json.Num(42)))))
|
||||
))
|
||||
val encoder = summon[Encoder[Contacts]]
|
||||
assert(encoder.encode(contacts) == json)
|
||||
}
|
||||
|
||||
@Test def `it is possible to encode and decode contacts (4pts)`(): Unit = {
|
||||
val peopleGenerator = Gen.listOf(Gen.resultOf((s: String, x: Int) => Person(s, x)))
|
||||
checkProperty(Prop.forAll(peopleGenerator)(people => encodeAndThenDecodeProp(Contacts(people))))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
trait TestEncoders extends EncoderFallbackInstance
|
||||
|
||||
trait EncoderFallbackInstance {
|
||||
|
||||
given [A](given ct: ClassTag[A]): Encoder[A] = throw new AssertionError(s"No given instance of `Encoder[${ct.runtimeClass.getSimpleName}]`")
|
||||
|
||||
}
|
||||
|
||||
trait TestDecoders extends DecoderFallbackInstance
|
||||
|
||||
trait DecoderFallbackInstance {
|
||||
|
||||
given [A](given ct: ClassTag[A]): Decoder[A] = throw new AssertionError(s"No given instance of `Decoder[${ct.runtimeClass.getSimpleName}]")
|
||||
|
||||
}
|
||||
@ -1,46 +0,0 @@
|
||||
package patmat
|
||||
|
||||
import org.junit._
|
||||
import org.junit.Assert.assertEquals
|
||||
|
||||
class HuffmanSuite {
|
||||
import Huffman._
|
||||
|
||||
trait TestTrees {
|
||||
val t1 = Fork(Leaf('a',2), Leaf('b',3), List('a','b'), 5)
|
||||
val t2 = Fork(Fork(Leaf('a',2), Leaf('b',3), List('a','b'), 5), Leaf('d',4), List('a','b','d'), 9)
|
||||
}
|
||||
|
||||
|
||||
@Test def `weight of a larger tree (10pts)`: Unit =
|
||||
new TestTrees {
|
||||
assertEquals(5, weight(t1))
|
||||
}
|
||||
|
||||
|
||||
@Test def `chars of a larger tree (10pts)`: Unit =
|
||||
new TestTrees {
|
||||
assertEquals(List('a','b','d'), chars(t2))
|
||||
}
|
||||
|
||||
@Test def `string2chars hello world`: Unit =
|
||||
assertEquals(List('h', 'e', 'l', 'l', 'o', ',', ' ', 'w', 'o', 'r', 'l', 'd'), string2Chars("hello, world"))
|
||||
|
||||
|
||||
@Test def `make ordered leaf list for some frequency table (15pts)`: Unit =
|
||||
assertEquals(List(Leaf('e',1), Leaf('t',2), Leaf('x',3)), makeOrderedLeafList(List(('t', 2), ('e', 1), ('x', 3))))
|
||||
|
||||
|
||||
@Test def `combine of some leaf list (15pts)`: Unit =
|
||||
val leaflist = List(Leaf('e', 1), Leaf('t', 2), Leaf('x', 4))
|
||||
assertEquals(List(Fork(Leaf('e',1),Leaf('t',2),List('e', 't'),3), Leaf('x',4)), combine(leaflist))
|
||||
|
||||
|
||||
@Test def `decode and encode a very short text should be identity (10pts)`: Unit =
|
||||
new TestTrees {
|
||||
assertEquals("ab".toList, decode(t1, encode(t1)("ab".toList)))
|
||||
}
|
||||
|
||||
|
||||
@Rule def individualTestTimeout = new org.junit.rules.Timeout(10 * 1000)
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user