Loops Three main types of loop: Foreach Loop Has the form foreach(itemName in listName) do stuff here Needs list to be useful Great for doing a set of steps using every item in a list. Not as flexible as the other loops While Loop Has the form while(condition) do stuff here Initial conditions need to be set up before the loop, and a way to change the condition needs to be present inside the loop. ex. theIndex:=1 while(theIndex<10) Print(theIndex) theIndex:=theIndex+1 ex. Read(password) while(password!=savedPass) Print(“Incorrect Password. Please try again”) Read(password) Print(“Access Granted”) For Loop Has the form for(initial Condition; continuing condition, end of loop action) do stuff here Don’t need to be able to write code using it Just as capable as a while loop, but format is more geared for looping with a definite end. Algorithms General Utility: Sum of Numbers: sum:=0 foreach(num in listOfNumbers) sum:=sum+num return sum Average of numbers sum:=0 foreach(num in listOfNumbers) sum:=sum+num return sum/listOfNumbers.length Maximum Value of a List index:=1 maxValue=-infinity while(index<=theList.length) if(maxValue<theList[index]) maxValue:=theList[index] index:=index+1 Index of Max Value index:=1 maxIndex=1 while(index<=theList.length) if(theList[maxIndex]<theList[index]) maxIndex:=index index:=index+1 Search: Linear Search: Start at one end and work your way through to the end if necessary. Runs in n time. Good for small sets of data Data doesnt need to be sorted Binary Search: Pick the middle term, and check if the search term is greater than or less than the one picked. Go to the appropriate side and pick the middle term of that section. Repeat. Runs in lg( n) time Faster access than linear search on larger data sets Data DOES need to be sorted Sort: Bubble Sort: Each item is checked, then “bubbled” to its correct location. Runs in n2 time. Relativley simple to code. Merge Sort: List is split into two smaller lists, and each of those lists has merge sort run on them. The resulting sorted lists are merged back together by comparing the first term in each list, putting the smaller one in the larger list, and comparing the next items in the list. 1<lg(n)<n<n*lg(n)<n^2<n!<n^n Data Structures Array: Simple data structure, consists of a single contiguous block of memory. Fixed size causes issues when adding/deleting items, but allows for fast access Linked List: Simple data structure consisting of a series of nodes and pointers. Each node has a single pointer that points to the next item in the list (think scavenger hunt). Allows for easy insertion and deletion, but slows down access time. Be able to sketch a diagram illustrating the structure of the list and the process of adding or removing a node. Stack: Advanced data structure that is a Last In- First out setup. Be able to read code that uses the operators associated with stacks: pop() push(item) and peek() Queue: Like stack, only First in first out. Be able to read code that uses the operators: enqueue(item) and dequeue() Binary Tree: Advanced data structure consisting of nodes and left and right pointers. Top of the tree is called the root, nodes that dont have any children are called leaves. Binary Search Tree: Binary tree organized to allow for fast searching. Given any node, Larger items go to right, smaller go to left. Given an input string, be able to create a valid BST Heap: Binary tree that must satisfy the given heap property and be a complete tree Max Heap: given any node, all descendant nodes will be less in value Min Heap: given any node, all descendant nodes will be greater in value Complete: all rows in the tree are full except for the last. any holes in the last row must be to the far right. Be able to create a valid heap from a given set of values. Be able to identify ideal data structures to use for a given situation. Low Level Basics Transistors and Vacuum Tubes: These, in computers, serve the purpose of an amplifier or a switch. Vacuum tubes are old (and generate a lot of heat and are unreliable if you ever turn the machine off). They can be combined to build logic gates. Logic Gates: Be able to do circuits, given the inputs For logic gates and boolean algebra: 1=True 0=False ● And: True if both are True 1 AND 1 = 1 1 AND 0 = 0 0 AND 1 = 0 0 AND 0 = 0 ● Or: True if either is True 1 AND 1 = 1 1 AND 0 = 1 0 AND 1 = 1 0 AND 0 = 0 ● Not: only takes one input -- simply returns the opposite of the input Not 1 = 0 Not 0 = 1 ● XOR (eXclusive OR): True if one input is true but false if both or neither. Or but not and. 1 XOR 1 = 0 1 XOR 0 = 1 0 XOR 1 = 1 0 XOR 0 = 0 Binary: All data/programs are stored in a binary format -- base 2. Bits: A single binary digit (a 1 or a 0) is a bit Bytes : A byte is 8 bits (for our purposes). Computers are designed to work on byte-sized groups of binary digits (accessing a single bit is not as straightforward). Character Sets: Letters/numbers/control characters have different standards ASCII: uses 7 bits per character (often uses 1 byte) works for latin alphabet languages but not others Unicode: newer scheme, among others, that allows many more characters, has a variable number of bytes for each charachter (usually between 1 and 4 bytes) RGB color scheme: Store color as separate Red, Green, and Blue values -- normally 1 Byte each, so 3 bytes for each pixel. That means a range of 0-255. rgb(255,255,255) is white, rgb(0,0,0) is black. Text/Audio/Video Compression: Can often use patterns in the file (such as repeats or frequency) to reduce the file sizes -- real time high quality online video would not be possible with the current networks we have without compression Run-length encoding. Stores a value and then how often its repeated in a row Frequency Encoding (Huffman): Calculates the frequency of appearance for each charachter, and creates variable length codes for each one using a Huffman Tree.