1 / 103

Take-Home Lab #04

Take-Home Lab #04. CS1020 – Data Structures And Algorithms 1 AY2015-16 Semester 2. Queues are “one-way”. Only insert from the back, can only take the head. (FIFO). FIFO = First-In First-Out. Big “Queue of Stacks”. 1. 7. 9. 1. 3. 1. 3. 1. 5. 3. 100. 4. Dequeue. Enqueue. 8.

vlandry
Télécharger la présentation

Take-Home Lab #04

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Take-Home Lab #04 CS1020 – Data Structures And Algorithms 1 AY2015-16 Semester 2

  2. Queues are “one-way”. Only insert from the back, can only take the head. (FIFO) FIFO = First-In First-Out Big “Queue of Stacks” 1 7 9 1 3 1 3 1 5 3 100 4 Dequeue Enqueue 8 17 5 22 5 4 Head Tail Queue<E> = new LinkedList<E>(); http://docs.oracle.com/javase/8/docs/api/java/util/Queue.html Stack and Queue

  3. Interface Queue<E> <<interface>> Queue<E> • //see Java API + add(E) : boolean + peek() : E + poll() : E … Stack and Queue

  4. Interface Queue<E> <<interface>> Queue<E> • //see Java API add(E) + add(E) : boolean + peek() : E + poll() : E … Queue Stack and Queue

  5. Interface Queue<E> <<interface>> Queue<E> • //see Java API poll() + add(E) : boolean + peek() : E + poll() : E … Queue Stack and Queue

  6. Class Stack<E> Stack<E> • elements: LinkedList<E> + push(E) : void + pop() : E + peek() : E + getSize() : int + isEmpty() : boolean Stack and Queue

  7. Class Stack<E> Stack<E> • elements: LinkedList<E> + push(E) : void + pop() : E + peek() : E + getSize() : int + isEmpty() : boolean 1 3 5 4 1 Stack and Queue

  8. push(10) • Class Stack<E> Stack<E> • elements: LinkedList<E> 10 + push(E) : void + pop() : E + peek() : E + getSize() : int + isEmpty() : boolean 1 3 5 4 1 Stack and Queue

  9. peek() Returns 10 • Class Stack<E> Stack<E> • elements: LinkedList<E> 10 + push(E) : void + pop() : E + peek() : E + getSize() : int + isEmpty() : boolean 1 3 5 4 1 Stack and Queue

  10. pop() Returns 10 • Class Stack<E> Stack<E> • elements: LinkedList<E> 10 + push(E) : void + pop() : E + peek() : E + getSize() : int + isEmpty() : boolean 1 3 5 4 1 Stack and Queue

  11. Class Stack<E> Stack<E> • elements: LinkedList<E> publicintgetSize() { returnelements.size(); } publicbooleanisEmpty() { returnthis.getSize() == 0; } + push(E) : void + pop() : E + peek() : E + getSize() : int + getIndex() : int + isEmpty() : boolean Stack and Queue

  12. Problem 1 Swinging Monkeys

  13. Count the number of possible swings: • Monkeys can swing from one tree to another directly as long as there is no tree in between that is taller than or have the same height as either one of the two trees. • Given the sequence of tree heights, determine the number of pair of trees that the monkeys can swing from and to. Swinging Monkeys

  14. 5 Trees: 19m – 17m – 20m – 20m – 20m Output: 5 Swinging Monkeys

  15. Naïve solution: • For all trees, count how many trees that the monkey can swing to from that tree. • Maintain two for loops, one for the source and one for the destination: for (inti = 0; i < n; i++) { for(intj = i + 1; j < n; j++) { if(canSwing(i,j)) { count++; } } } Swinging Monkeys

  16. privatebooleancanSwing(intfrom, intto) { for(inti = from + 1; i < to; i++) { if(trees[i] >= Math.min(from, to)) { returnfalse; } } returntrue; } Swinging Monkeys

  17. 5 Trees: 19m – 17m – 20m – 20m – 17m Observe: from the tree before the Blue tree, the Monkey cannot jump to the tree after the Blue tree Swinging Monkeys

  18. After we process tree i, we can forget all the trees before ithat are shorter than i. • What will be the property of the sequence of trees that are not forgotten? • Decreasing order from the tree with smallest index. • Why? • Suppose it is not decreasing in sequence: • After we process Blue, we can forget Yellow. • Yellow would have been removed from the sequence. Swinging Monkeys

  19. Swinging Monkeys • Property of the Sequence: • Decreasing Order of Height • At the start of the processing of the next tree,the Monkey can jump from all tree in the sequence to the next tree • If the next tree is higher than some elements,these elements in the sequence cannot jump beyond the next tree • Discussions: • What Data Structure do we need to implement? • Stack: why? • Because we need to remember the previous elements in order

  20. Problem 2 Chemistry

  21. Given a set of mappings and chemical formula: • Calculate the molecule mass of the formula C 12 H 1 N 14 O 16 (NH4)2CO3 Problem Description Chemistry

  22. Let’s work on a few examples based on the given mapping (NH4)2CO3 C 12 H 1 N 14 O 16 CH4OH ((CH3)CH4C3H8)4 Problem Description Chemistry

  23. Let’s work on a few examples based on the given mapping (NH4)2CO3 14 + 4 x 1 C 12 H 1 N 14 O 16 CH4OH ((CH3)CH4C3H8)4 Problem Description Chemistry

  24. Let’s work on a few examples based on the given mapping (NH4)2CO3 18 * 2 C 12 H 1 N 14 O 16 CH4OH ((CH3)CH4C3H8)4 Problem Description Chemistry

  25. Let’s work on a few examples based on the given mapping 36+ CO3 12 + 3 x 16 C 12 H 1 N 14 O 16 CH4OH ((CH3)CH4C3H8)4 Problem Description Chemistry

  26. Let’s work on a few examples based on the given mapping 36+ CO3 60 C 12 H 1 N 14 O 16 CH4OH ((CH3)CH4C3H8)4 Problem Description Chemistry

  27. Let’s work on a few examples based on the given mapping 36+ 60 C 12 H 1 N 14 O 16 CH4OH ((CH3)CH4C3H8)4 Problem Description Chemistry

  28. Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 CH4OH ((CH3)CH4C3H8)4 Problem Description Chemistry

  29. Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 CH4OH 12 + 4 x 1 + 16 + 1 ((CH3)CH4C3H8)4 Problem Description Chemistry

  30. Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 CH4OH 33 ((CH3)CH4C3H8)4 Problem Description Chemistry

  31. Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 Mr = 33 CH4OH ((CH3)CH4C3H8)4 Problem Description Chemistry

  32. Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 Mr = 33 CH4OH ((CH3)CH4C3H8)4 12 + 3 x 1 Chemistry

  33. Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 Mr = 33 CH4OH ((CH3)CH4C3H8)4 15 Chemistry

  34. Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 Mr = 33 CH4OH (15 + CH4C3H8)4 12 + 4 x 1 + 3 x 12 + 8 x 1 Chemistry

  35. Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 Mr = 33 CH4OH (15 + CH4C3H8)4 60 Chemistry

  36. Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 Mr = 33 CH4OH (15 + 60)4 Chemistry

  37. Let’s work on a few examples based on the given mapping (NH4)2CO3 Mr = 96 C 12 H 1 N 14 O 16 Mr = 33 CH4OH Mr = 300 ((CH3)CH4C3H8)4 Problem Description Chemistry

  38. ? What data structure should I use for: Mass Mapping Formula Processing Stack HashMap Chemistry

  39. HashMap<K, V> hashMapName= newHashMap<K, V>(); HashMap HashMap<Character, Integer> massMapping= newHashMap<Character, Integer>(); Setting Keys and Values Retrieving Values Returns 12 massMapping.put(‘C’, 12); massMapping.get(‘C’); Returns 1 massMapping.put(‘H’, 1); massMapping.get(‘H’); Returns 14 massMapping.put(‘N’, 14); massMapping.get(‘N’); https://docs.oracle.com/javase/8/docs/api/java/util/HashMap.html

  40. What should my stack contain? Mass of each Atom The Elements OR Stack https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html

  41. What should my stack contain? Mass of each Atom The Elements Stack We will use a stack of integers CH4 Stack https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html

  42. What should my stack contain? Mass of each Atom The Elements Stack We will use a stack of integers CH4 12 Stack https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html

  43. What should my stack contain? Mass of each Atom The Elements Stack We will use a stack of integers 1 CH4 12 Stack https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html

  44. What should my stack contain? Mass of each Atom The Elements Stack We will use a stack of integers 4 1 CH4 12 Stack https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html

  45. What should my stack contain? Mass of each Atom The Elements Stack We will use a stack of integers Total = 12 + 4 = 16 4 1 CH4 12 Stack https://docs.oracle.com/javase/8/docs/api/java/util/Stack.html

  46. How do I store the formula? As a character array String nextLine = sc.nextLine(); char[] formula = nextLine.toCharArray(); ? How do I process the formula? Loop through all characters processInput(formula); Chemistry

  47. publicvoid run() { Scanner sc = new Scanner(System.in); intn = sc.nextInt(); sc.nextLine(); initializeMassMapping(sc, n); //implement it  String nextLine = sc.nextLine(); char[] formula = nextLine.toCharArray(); processInput(formula); inttotal = accumulate(); //will be defined later System.out.println(total); sc.close(); } Chemistry

  48. privatevoidprocessInput(char[] formula) { for(charc : formula) { //what should I do??? } } Next character can be one of these: What you should do if you encounter it: ( Open Bracket Start New “Session” ) Close Bracket Acummulate Current “Session” X Atom Name Push Its Mass n Multiply Top of The Stack by n Number of atoms (or molecules) Chemistry

  49. privatevoidprocessInput(char[] formula) { for(charc : formula) { if (c == '(') { //open bracket results.push(-1); //start a new “session” } elseif (c == ')') { //close bracket intsum = accumulate(); //acummulate is a “helper” //then push the sum into the stack } elseif (Character.isDigit(c)) {//it’s a number //multiply the top of the stack. How?  } else { //then c must be a ??? //push the corresponding atom’s mass } } } Chemistry

  50. privateint accumulate() { intresult = 0; inttop = results.pop(); while(/* top is not -1 and stack is not empty */){ //add current top to result //update top to be next element in stack } returnresult; } Chemistry

More Related