1 / 9

CS100-Stacks & Queues Part 2

CS100-Stacks & Queues Part 2. As you arrive, continue looking at the non-recursive isAncestor problem from last time. You’ll be seeing more problems like it in class today. What You Will Do Today. You will solve more problems that involve “deferred work”

ewa
Download Presentation

CS100-Stacks & Queues Part 2

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. CS100-Stacks & Queues Part 2 As you arrive, continue looking at the non-recursive isAncestor problem from last time. You’ll be seeing more problems like it in class today.

  2. What You Will Do Today • You will solve more problems that involve “deferred work” • You will be able to explain how stacks and queues differ in solving deferred work problems • You will be exposed to the specialized names of functions in stacks (push, pop, peek) – you need to memorize these

  3. Similar Problem: Number Game The number gamestarts with a particular number. You win the game if you can reduce the number to 0 using only the operations allowed in the game. The operations allowed are: Subtract 7 Subtract 9 Subtract 20 Write a function isWinnable to determine if the game is winnable for a given starting value.

  4. Number Game 2 The number gamestarts with a particular number. You win the game if you can reduce the number to 0 using only the operations allowed in the game. The operations allowed are: Subtract 3 Divide by 2 (only allowed if even) Subtract 20 Write a function isWinnable to determine if the game is winnable for a given starting value. For this problem (and the other problems in today) don’t use recursion

  5. Number Game 3: Good Move The number gamestarts with a particular number. You win the game if you can reduce the number to 0 using only the operations allowed in the game. The operations allowed are: Subtract 3 Subtract -1 (a.k.a. “add 1”) Subtract 7 Write a function to determine a good move. A good move is one that can win the game in the minimum number of moves. e.g. (20 can be -1+7+7+7 or 3+3+3+3+3+3+3+-1…or others) But goodMove3(20) could return -1 or 7 but not 3, because -1+7+7+7 is the shortest solution

  6. Queue • It’s when you add to the end of a list but remove from the beginning • Java does have a Queue interface, but in this class we’ll just use a LinkedList • Sometimes adding to a queue and removing from a queue are called “enqueue” and “dequeue” • Particularly useful when you want to do something in the minimum number of steps, because smaller step counts always happen earlier

  7. Number Game 2: States List Subtract 3 Divide by 2 (only allowed if even) Subtract 20 Write a function statesList2 that returns a list of game states that win the game in a minimum number of moves. e.g. 32’s state list looks like [32, 12, 6, 3, 0]. Hint: you’ll want to keep the list of states in your “to do”, rather than nums. Challenge: write MovesList that returns the winning moves instead (e.g. [20,2,2,3] for 32) After completing this, please submit your code via ambient.

  8. Stack • A stack is a list that you add to the beginning and remove from the beginning. We call the beginning the “top” of the stack • Java has a stack class, which we will use (although we could really just use a list) • Stacks have special names: • push(element) puts a new element on the top of the stack • pop() removes an element from the top of the stack • peek() gets the element at the top of the stack without removing it • Often useful in “tree like” problems where things have subthings, which may have still more sub things

  9. Matching Parens • You’ve got a string consisting only of ‘(‘ and ‘)’ and ‘[‘ and ‘]’ • You want to know of the parents “match”. That is, every opening paren has a closing paren of the same type, and they are in the right order. For example: • “()” “([])” “[()]” “([]())” “()[]” match • “)” “(]” “)(“ “()()(“ “((())” do not match

More Related