90 likes | 152 Views
Combine Until Encode Decode. Scala HW5 Part 2. Combine. Not sure what a correct test case is Fork(Leaf('d',4),Fork(Leaf('a',2),Leaf('b',3),List('a', 'b'),5),List('d', 'a', 'b'),9)) val t2 = Fork(Fork(Leaf('a',2), Leaf('b',3), List('a','b'), 5), Leaf('d',4), List('a','b','d'), 9).
E N D
Combine Until Encode Decode Scala HW5 Part 2
Combine • Not sure what a correct test case is • Fork(Leaf('d',4),Fork(Leaf('a',2),Leaf('b',3),List('a', 'b'),5),List('d', 'a', 'b'),9)) • val t2 = Fork(Fork(Leaf('a',2), Leaf('b',3), List('a','b'), 5), Leaf('d',4), List('a','b','d'), 9)
Combine function, not complete • def combine1(trees: List[CodeTree]): List[CodeTree] = trees match{ • case first::second::restofList => { • val ct = makeCodeTree(first,second) • //not correct, check weights and add to either front or back. Build test cases for both • //how to insert in right place? can do a recursive traversal to insert • //model after insertion sort code • // if( weight(ct) < weight(restofList(0))) • return ct::restofList • // else • // return (restofList++ct • } • case _ => trees //if less than 2 return trees • }
def combine2(trees: List[CodeTree]): List[CodeTree] = { • if (trees.length>=2) { • val ct = makeCodeTree(trees(0),trees(1)) • return ct::trees.drop(2) • } • else • trees • }
Until, currying, first called then second • def until(singleton:List[CodeTree]=>Boolean, combiner:List[CodeTree]=>List[CodeTree])(zzz: List[CodeTree]):CodeTree = singleton(zzz) match{ • case true =>/*println(zzz);*/zzz.head • case false =>until(singleton,combiner)(combiner(zzz)) • } • Test case build tree: • def createCodeTree(chars: List[Char]): CodeTree = { • until(singleton,combine)(makeOrderedLeafList(times(chars))) • }
Encode, do in steps • Create 01s from tree, val tree=Fork(Leaf('a',2),Leaf('b',3),List('a', 'b'),5) • Left is 0 right is 1. a=0,b=1 ab=01, step 1: loop through tree, no accumulator def encodeMe(tree:CodeTree,charList:List[Char]):Unit={ def encodeMeAcc(tree:CodeTree,testMe:Char,acc:List[Int]):Unit=tree match{ case Leaf(c:Char,w:Int) =>println("Leaf char:"+c+" weight:"+w); case Fork(left:CodeTree, right:CodeTree, charList:List[Char], weight:Int)=>{ println("Fork testMe:"+testMe+" chars(left):"+chars(left)+" chars(right):"+chars(right)) if(chars(left).contains(testMe)){ println(“left”) }else if(chars(right).contains(testMe)){ println("rigth!!! add 1 to list"); } } // for(i<-charList){ // println("i:"+i) // encodeMeAcc(tree,i,List[Int]()); map doesnt work, key point in returning iterator, doesnt affect ctor charList.flatMap(x=>encodeMeAcc(tree,x,List[Int]())) } }
Encode accumulator • def encodeMe(tree:CodeTree,charList:List[Char]):List[Int]={ • def encodeMeAcc(tree:CodeTree,testMe:Char,acc:List[Int]):List[Int]=tree match{ • case Leaf(c:Char,w:Int) =>println("Leaf char:"+c+" weight:"+w);println("leaf acc:"+acc);acc • case Fork(left:CodeTree, right:CodeTree, charList:List[Char], weight:Int)=>{ • println("Fork testMe:"+testMe+" chars(left):"+chars(left)+" chars(right):"+chars(right)) • if(chars(left).contains(testMe)){ • println("left!!! add 0 to list"); • val addMe = acc++List(0) • println("addMe:"+addMe); • encodeMeAcc(left,testMe,addMe) • }else //if(chars(right).contains(testMe)){ • println("rigth!!! add 1 to list"); • val addMe = acc++List(1) • println("addMe:"+addMe); • encodeMeAcc(right,testMe,addMe) • } • } • for(i<-charList){ • println("i:"+i) • encodeMeAcc(tree,i,List[Int]()) • } • }
Decode • Nested pattern matching def decodeA(tree: CodeTree, bits: List[Bit]): Unit = { def decodeAcc(tree: CodeTree, bits: List[Bit]):Unit = tree match{ case Leaf(c:Char,w:Int) => println("leaf") case Fork(left:CodeTree, right:CodeTree, _,_ )=> bits match{ case Nil=>println("Nil list") case x::xs=>println("x:"+x+" xs:"+xs); decodeAcc(tree, xs) } } decodeAcc(tree,bits) } println("decode") decodeA(Fork(Leaf('a',2),Leaf('b',3),List('a', 'b'),5),List(0,1,0,1,0,1))
Decode add acc • def decodeB(ptree: CodeTree, bits: List[Bit]): List[Char] = { • def decodeAcc(tree: CodeTree, bits: List[Bit],acc:List[Char]):List[Char] = tree match{ • case Leaf(c:Char,w:Int) => println("leaf c:"+c+" acc:"+acc ); decodeAcc(ptree,bits,List(c)++acc) • case Fork(left:CodeTree, right:CodeTree, list:List[Char], w:Int )=> bits match{ • case Nil=>println("Nil"); acc • case x::xs=>println("x:"+x+" xs:"+xs); • if(x==0) { • println("found 0 acc:"+acc) • decodeAcc(left,xs,acc) • } • else{ • println("found 1 acc:"+acc) • decodeAcc(right,xs,acc) • } • } • } • decodeAcc(tree,bits,List[Char]()) • } • println("decode") • val b = decodeB(Fork(Leaf('a',2),Leaf('b',3),List('a', 'b'),5),List(0,1,0,1,0,1)) • println("b:"+b);