mirror of
https://github.com/jlengrand/FunctionalProgrammingScalaCoursera.git
synced 2026-03-10 15:49:51 +00:00
Merge remote-tracking branch 'origin/master'
This commit is contained in:
@@ -110,7 +110,7 @@ object Huffman {
|
||||
/**
|
||||
* Checks whether the list `trees` contains only one single code tree.
|
||||
*/
|
||||
def singleton(trees: List[CodeTree]): Boolean = trees.tail isEmpty
|
||||
def singleton(trees: List[CodeTree]): Boolean = trees.size == 1
|
||||
|
||||
/**
|
||||
* The parameter `trees` of this function is a list of code trees ordered
|
||||
@@ -202,9 +202,24 @@ object Huffman {
|
||||
/**
|
||||
* This function encodes `text` using the code tree `tree`
|
||||
* into a sequence of bits.
|
||||
* Note: I am not taking any precaution here as to whether the characters are in the tree or not
|
||||
*/
|
||||
def encode(tree: CodeTree)(text: List[Char]): List[Bit] = {
|
||||
def encodeIter(subtree: CodeTree, textLeft: List[Char], acc: List[Bit]): List[Bit] = {
|
||||
if(textLeft.isEmpty) acc
|
||||
else{
|
||||
subtree match{
|
||||
case Fork(left, right, _, _) => {
|
||||
if (chars(left).contains(textLeft.head)) encodeIter(left, textLeft, 1 :: acc)
|
||||
else encodeIter(right, textLeft, 0 :: acc)
|
||||
}
|
||||
case Leaf(_, _) => encodeIter(tree, textLeft.tail, acc)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
encodeIter(tree, text, List[Bit]())
|
||||
}
|
||||
|
||||
// Part 4b: Encoding using code table
|
||||
@@ -215,7 +230,7 @@ object Huffman {
|
||||
* This function returns the bit sequence that represents the character `char` in
|
||||
* the code table `table`.
|
||||
*/
|
||||
def codeBits(table: CodeTable)(char: Char): List[Bit] = ???
|
||||
def codeBits(table: CodeTable)(char: Char): List[Bit] = table.find(x => x._1 == char).get._2
|
||||
|
||||
/**
|
||||
* Given a code tree, create a code table which contains, for every character in the
|
||||
@@ -225,14 +240,23 @@ object Huffman {
|
||||
* 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 = ???
|
||||
def convert(tree: CodeTree): CodeTable =
|
||||
tree match {
|
||||
case Leaf(c, _) => 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 = ???
|
||||
def mergeCodeTables(a: CodeTable, b: CodeTable): CodeTable = {
|
||||
def addBit(bit: Bit)(code: (Char, List[Bit])): (Char, List[Bit]) =
|
||||
(code._1, bit :: code._2)
|
||||
|
||||
a.map(addBit(0)) ::: b.map(addBit(1))
|
||||
}
|
||||
|
||||
/**
|
||||
* This function encodes `text` according to the code tree `tree`.
|
||||
@@ -240,6 +264,12 @@ object Huffman {
|
||||
* 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] = ???
|
||||
def quickEncode(tree: CodeTree)(text: List[Char]): List[Bit] = {
|
||||
def encodeStep(table:CodeTable, text: List[Char], acc: List[Bit]) : List[Bit] = {
|
||||
if (text.isEmpty) acc
|
||||
else encodeStep(table, text.tail, codeBits(table)(text.head) ::: acc )
|
||||
}
|
||||
encodeStep(convert(tree), text, List[Bit]())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user