Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Stack-- first in last out, pop, push, etc
- Queue-- first in first out
- def balanced(str:String): Boolean = {
- brackets = HashMap('(' -> ')', '[' -> ']', '{' -> '}')
- stk = Stack.empty[Char]
- for(c <- str) {
- if(brackets.contains(c)) stk.push(brackets(c))
- else if(stk.isEmpty || stk.pop != c) return false
- }
- stk.isEmpty
- }
- =========
- Recursion review
- def sum(list:List[Int]): Int = {
- if(list.isEmpty) 0
- else list.head + sum(list.tail)
- }
- list(1,2,3,4)
- 1 DIVIDE (2,3,4)
- CONQUER (9)
- GLUE
- list.head + sum(list.tail)
- 10
- =========
- HashMap stuff
- import scala.collection.immutable.HashMap
- val pets = HashMap("cat" -> 1, "dog" -> 2, "tank" -> 100)
- this is immutable because of val i guess
- val newPets = pets + ("infantry" -> 1)
- ===
- Set-- stuff in a set is unique
- mySet = TreeSet(2,4,6,8)
- mySet(10) => false
- ==========
- LinkedList
- case class SList(item:Int, next:SList)
- this is just the exact same as the normal List class except it uses item and next instead of head and tail
- def length(list: SList): Int = {
- if(list == null) 0
- else 1 + length(list.next)
- }
Add Comment
Please, Sign In to add comment