wavec022

lesson 5 or 6 notes

Jan 29th, 2020
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. Stack-- first in last out, pop, push, etc
  2.  
  3. Queue-- first in first out
  4.  
  5.  
  6. def balanced(str:String): Boolean = {
  7. brackets = HashMap('(' -> ')', '[' -> ']', '{' -> '}')
  8. stk = Stack.empty[Char]
  9. for(c <- str) {
  10. if(brackets.contains(c)) stk.push(brackets(c))
  11. else if(stk.isEmpty || stk.pop != c) return false
  12. }
  13. stk.isEmpty
  14. }
  15.  
  16. =========
  17.  
  18. Recursion review
  19.  
  20. def sum(list:List[Int]): Int = {
  21. if(list.isEmpty) 0
  22. else list.head + sum(list.tail)
  23. }
  24.  
  25. list(1,2,3,4)
  26. 1 DIVIDE (2,3,4)
  27. CONQUER (9)
  28. GLUE
  29. list.head + sum(list.tail)
  30. 10
  31.  
  32.  
  33. =========
  34.  
  35. HashMap stuff
  36.  
  37. import scala.collection.immutable.HashMap
  38.  
  39. val pets = HashMap("cat" -> 1, "dog" -> 2, "tank" -> 100)
  40. this is immutable because of val i guess
  41.  
  42. val newPets = pets + ("infantry" -> 1)
  43.  
  44. ===
  45.  
  46. Set-- stuff in a set is unique
  47.  
  48. mySet = TreeSet(2,4,6,8)
  49. mySet(10) => false
  50.  
  51.  
  52. ==========
  53.  
  54. LinkedList
  55.  
  56. case class SList(item:Int, next:SList)
  57. this is just the exact same as the normal List class except it uses item and next instead of head and tail
  58.  
  59. def length(list: SList): Int = {
  60. if(list == null) 0
  61. else 1 + length(list.next)
  62. }
Add Comment
Please, Sign In to add comment