spcs:summer2014:teaching_notes

Prime Numbers

- every positive integer can be a product of primes.
- prime factorization
- Sieve of Eratosthenes

Permutations/Combinations

- Permutations - order matters
- with repetition: lock
- n^k

- without repetition: rainbow
- n!/(n-r)!

- Combinations - order doesn't matter
- with repetition: icecream
- (n r)
- n!/r!(n-r!)

- without repetition: fruit salad
- (n+n-r r)

Bit Manipulation

- ^ XOR, ~ boolean NOT
- 0110 + 0110 is the same as 0110 * 2, shifting 0110 left by 1.
- Multiplying by 2^2 just shifts a number by n
- a ^ (~a) is all 1's
- bit shifting
- arithmetic shift
- logical shift
- circular rotate

- Uses
- boolean isPalindrome(int x)
- get bit - num & (1 « i) != 0
- set bit - num | (1 « i)
- clear bit - num & mask, mask = ~(1 « i)
- clear from MSB or 0

- powers of 2: http://en.wikipedia.org/wiki/Power_of_two

- In Java, all integer types are signed, and the “«” and “»” operators perform arithmetic shifts.
- Java adds the operator “»>” to perform logical right shifts,
- but because the logical and arithmetic left-shift operations are identical, there is no “«<” operator in Java.

- Strings
- Arrays
- Linked Lists

Node deleteNode(Node head, int d) { Node n = head if (n.data == d) { return head.next; // moved head } while (n.next != null) { if (n.next.data == d) { n.next = n.next.next; return head; // head didn't change } n = n.next; } return head; }

- Stacks
- Queues
- Trees
- balanced or unbalanced, full and complete
- Tries - Unlike a binary search tree, no node in the tree stores the key associated with that node; instead, its position in the tree defines the key with which it is associated. All the descendants of a node have a common prefix of the string associated with that node, and the root is associated with the empty string.

- Graphs - http://pastebin.com/MtJQDqVZ
- AdjacencyList
- Matrix

- HashMap

public HashMap<Integer, Student> buildMap(Student[] students) { HashMap<Integer, Student> map = name HashMap<Integer, Student>(); for (Student s : students) map.put(s.getId(),s); return map; }

If you're only interested in the keys, you can iterate through the keySet() of the map:

Map<String, Object> map = ...; for (String key : map.keySet()) { // ... }

If you only need the values, use values():

for (Object value : map.values()) { // ... }

Finally, if you want both the key and value, use entrySet():

for (Map.Entry<String, Object> entry : map.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); // ... }

One caveat: if you want to remove items mid-iteration, you'll need to do so via an Iterator (see karim79's answer). However, changing item values is OK (see Map.Entry).

- Recursion - take at least O(n) space, where n is depth of recursive call.
- dynamic programming caches results

- Sorting
- Searching
- Big O - http://bigocheatsheet.com/
- P - Polynomial Time
- NP - Verification is P
- NPComplete is NPHard that can be reduced to NP problem (for verification only)
- NPHard - Shortest traveling sales person

4 principles of OOP

- Data Abstraction
- Encapsulation
- Inheritance
- Polymorphism

- Object-Oriented
- factory - cardgame
- singleton - restaurant
- “does it make sense to call this method, even if no Obj has been constructed yet?” If so, it should definitely be static.

- System Design
- Memory Limits
- order of appearance, hash value, random/arbitrarily

- Testing
- who
- what cases
- bounds/environmental
- failure conditions
- how to test
- manual/automated
- black/whitebox

- values
- normal
- extremes
- nulls
- strange

- troubleshooting

- Design Patterns - http://en.wikipedia.org/wiki/Software_design_pattern

/soe/sherol/.html/teaching/data/pages/spcs/summer2014/teaching_notes.txt · Last modified: 2014/06/18 19:42 by ffpaladin