Scala HW5 Part 2 Combine Until Encode Decode. 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',

Slides:



Advertisements
Similar presentations
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Advertisements

ML Lists.1 Standard ML Lists. ML Lists.2 Lists A list is a finite sequence of elements. [3,5,9] ["a", "list" ] [] Elements may appear more than once [3,4]
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
F28PL1 Programming Languages Lecture 14: Standard ML 4.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Recursion in Scala. 2 Definitions A recursive method is a method that calls itself A method is indirectly recursive if it calls a method that calls a.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Data Structures: A Pseudocode Approach with C 1 Chapter 5 Contd... Objectives Explain the design, use, and operation of a linear list Implement a linear.
An Introduction to Sorting Chapter 8 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Map and Fold Building Powerful Abstractions. Hello. I’m Zach, one of Sorin’s students.
Insertion sort, Merge sort COMP171 Fall Sorting I / Slide 2 Insertion sort 1) Initially p = 1 2) Let the first p elements be sorted. 3) Insert the.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 26 Binary Search Trees.
Proving correctness. Proof based on loop invariants  an assertion which is satisfied before each iteration of a loop  At termination the loop invariant.
CS21, Tia Newhall Binary Search Trees (BST) 1.Hierarchical data structure with a single pointer to root node 2.Each node has at most two child nodes (a.
Binary Search Trees Section Trees Trees are efficient Many algorithms can be performed on trees in O(log n) time. Searching for elements.
Huffman code uses a different number of bits used to encode characters: it uses fewer bits to represent common characters and more bits to represent rare.
CS 46B: Introduction to Data Structures July 30 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
Scala Parallel Collections Aleksandar Prokopec EPFL.
Data Structures Arrays both single and multiple dimensions Stacks Queues Trees Linked Lists.
Computer Science 112 Fundamentals of Programming II Introduction to Stacks.
 Pearson Education, Inc. All rights reserved Searching and Sorting.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
data ordered along paths from root to leaf
Scala Parallel Collections Aleksandar Prokopec EPFL.
Ceng-112 Data Structures I Figure 9-1 The heap is guaranteed to hold the largest node of the tree in the root. The smaller nodes of a heap can be.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. 1 Chapter 19 Binary Search Trees.
CSC317 1 So far so good, but can we do better? Yes, cheaper by halves... orkbook/cheaperbyhalf.html.
Hossain Shahriar Announcement and reminder! Tentative date for final exam shown below, please choose a time slot! December 19.
23-Feb-16 Lists. Arrays and Lists Arrays are a fixed length and occupy sequential locations in memory This makes random access (for example, getting the.
List Operations CSCE 314 Spring CSCE 314 – Programming Studio Tuple and List Patterns Pattern matching with wildcards for tuples fst (a, _) = a.
 Introduction to Search Algorithms  Linear Search  Binary Search 9-2.
COMP 103 Course Review. 2 Menu  A final word on hash collisions in Open Addressing / Probing  Course Summary  What we have covered  What you should.
UMBC CMSC 331 Case Classes in Scala Intro. Basic Properties Case classes are regular classes which export their constructor parameters and which provide.
Huffman Codes ASCII is a fixed length 7 bit code that uses the same number of bits to define each character regardless of how frequently it occurs. Huffman.
Chapter 25 Binary Search Trees
ML: a quasi-functional language with strong typing
Assignment 4 Kang-Pil Kim, Jin Kim
Searching Given a collection and an element (key) to find… Output
Algorithm Analysis The case of recursion.
Sorting Algorithms.
Cse 373 April 14th – TreEs pt 2.
Algorithm Analysis CSE 2011 Winter September 2018.
CMSC 330: Organization of Programming Languages
Expressions and Control Flow in JavaScript
Lists 20-Sep-18.
Revised based on textbook author’s notes.
Important Concepts from Clojure
Important Concepts from Clojure
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2013.
Nicholas Shahan Spring 2016
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2018.
Logical Operations In Matlab.
Three Special Structures – Case, Do While, and Do Until
Linear Search Binary Search Tree
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2017.
Scala Apologia 27-Apr-19.
Heaps By JJ Shepherd.
Shadows CSE 681.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2019.
Tutorial Number 8 - Daniel Razavi
HW4 Review Case Classes.
Scala/Spark Review 6/13/2014 Redo Week 1 First Scala Course/Demo Scalatestest Spray Can (in JobServer)/sbt.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
Review Previously in: Lots of language features: functions, lists, records, tuples, variants, pattern matching Today: No new language features New idioms.
Presentation transcript:

Scala HW5 Part 2 Combine Until Encode Decode

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);