├── .DS_Store ├── 020619_trees ├── 1200px-Trie_example.svg.png ├── 2000px-Binary_search_tree.svg.png ├── slides.md └── structure-of-tree.jpeg ├── 032019_bubble_sort_quick_sort └── slides.md ├── 091118_intro_to_algorithms ├── problems.md └── slides.md ├── 091718_how_to_approach_a_technical_interview └── problems.md ├── 092518_data_structures ├── singly-linked-list.svg.png └── slides.md ├── 100218_big_o ├── big_O_chart.png ├── big_O_guide.md ├── problems.md └── slides.md ├── 100918_recursion ├── Call-stack-of-Fibonacci.jpg ├── fib-tree.jpeg ├── problems.md └── slides.md ├── 101718_problems ├── path.png └── problems.md ├── 103018_number_theory ├── Sieve_of_Eratosthenes_animation.gif ├── problems.md └── slides.md ├── 110618_graphs ├── adjacencymatrix.png ├── listadjacency.png ├── problems.md ├── slides.md └── undirectedgraph.png ├── 112118_problems └── problems.md ├── 112718_binary └── slides.md ├── README.md └── package.json /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghoagland-flatiron/algorithms_club/28f86fc20c030b72c725435f0a09d59959ad7a0b/.DS_Store -------------------------------------------------------------------------------- /020619_trees/1200px-Trie_example.svg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghoagland-flatiron/algorithms_club/28f86fc20c030b72c725435f0a09d59959ad7a0b/020619_trees/1200px-Trie_example.svg.png -------------------------------------------------------------------------------- /020619_trees/2000px-Binary_search_tree.svg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghoagland-flatiron/algorithms_club/28f86fc20c030b72c725435f0a09d59959ad7a0b/020619_trees/2000px-Binary_search_tree.svg.png -------------------------------------------------------------------------------- /020619_trees/slides.md: -------------------------------------------------------------------------------- 1 | # Trees 2 | 3 | --- 4 | 5 | ## What are trees? 6 | 7 | Trees are a _hierarchical data structure_. Each tree is made up of **nodes** and **edges**. 8 | + Each node contains a **value**. 9 | + Each node may or may not have edges to connect them to **child nodes**. 10 | + The node at the top of the tree is called the **root**. 11 | + Every node (except the root) has _exactly one_ parent node. 12 | + A node without any children is called a **leaf**. 13 | + The **height** of a tree is the longest distance from the root to a leaf. 14 | + The **depth** of a given node is the distance from it to the root. 15 | 16 | --- 17 | 18 | ![tree](./structure-of-tree.jpeg) 19 | 20 | Notice that any given node is the root of a tree itself, and this is called a **subtree**. (Side note: I think it should include node H?) 21 | 22 | --- 23 | 24 | ## Binary Trees 25 | 26 | A **binary tree** is a tree where each node can have a maximum of two children. By convention, these are referred to as the _left child_ and the _right child_. These children are themselves the roots of the _left subtree_ and the _right subtree_. 27 | 28 | --- 29 | 30 | ## Binary Search Trees 31 | 32 | A **binary search tree** is a binary tree with the additional rule that, given a node with a value `v`, the nodes in its _left subtree_ are all less than `v` and those in the _right subtree_ are greater than [or equal to] `v`. 33 | 34 | Notice in the image that this is also true in the subtrees. While 6 is less than 8 and in the root's _left_ subtree, because it is greater than 3, it is in that node's _right_ subtree. 35 | 36 | ![binary search tree](./2000px-Binary_search_tree.svg.png) 37 | 38 | --- 39 | 40 | ## What are the benefits of a binary search tree? 41 | 42 | Both in terms of lookup and insertion, a BST takes an average of Θ(log n). The worst case is O(n) because you could have inserted values in a strictly increasing or decreasing value. 43 | 44 | --- 45 | 46 | ## Binary Search Tree Problems 47 | 48 | ``` 49 | 8 50 | / \ 51 | 3 10 52 | / \ / \ 53 | 1 6 9 12 54 | / \ / 55 | 4 7 11 56 | ``` 57 | 58 | Make a `BinarySearchTree` class. It should take a `value`, a `left_child`, and a `right_child`. These children should be either an instance of a binary search tree or empty. 59 | 60 | Then work on the following methods: 61 | 62 | - `add(value)` 63 | 64 | - `contains(value)` 65 | 66 | - `find_biggest()` / `find_smallest()` 67 | 68 | - `depth_first_list()` * - list the values in order from smallest to largest. The above example would return 1, 3, 4, 6, 7, 8, 9, 10, 11, 12. 69 | 70 | - `breadth_first_list()` * - list the elements level by level. Starting at the root, read the values left to right and then move down to the next level. So the above would return 8, 3, 10, 1, 6, 9, 12, 4, 7, 11. 71 | 72 | - `delete_node()` ** Look up how to go about this. 73 | 74 | --- 75 | 76 | ## Tries 77 | 78 | A **trie** is another tree-based data structure. It is supposed to be pronounced like "tree" as it's from _re**trie**val_, practically pronounced like "try" to differentiate from trees in general. In this structure, it is not nodes themselves that contain the values, but the edges and the path taken determine the key for a node. 79 | 80 | --- 81 | 82 | ![trie](./1200px-Trie_example.svg.png) 83 | 84 | We're going to talk about a particular implementation of a trie for word lookup. Here, any time you want to look up a word, you follow the path letter by letter. So if I am looking for 'tea', start at the top and follow the path for "t". From that node, follow the path for "e". One last time, follow the path for "a". 85 | 86 | This might sound trivial, but down that path are _all_ words that start with 'tea' - 'team', 'tear', 'tearful', etc. This is why this data structure is very popular for predicting and/or autocompleting words. 87 | 88 | --- 89 | 90 | ![trie](./1200px-Trie_example.svg.png) 91 | 92 | Build a Trie class. This should hold: 93 | 94 | + `children` - a hash with the letters as keys and Trie instances as values may be the best implementation 95 | + `is_word` - a boolean that represents if the node is the termination of a word - in the example with "tea", the third node would have this as true as "tea" is a word, but first "t" and second "te" nodes would be false as those are not words. 96 | 97 | 98 | Methods: 99 | 100 | + `search(str)` - returns true if a given string is in the trie and represents a word. 101 | 102 | + `get_node(str)` - returns the node after following the string. So in the example, `get_node('te')`this would return the node with children for "tea", "ted", and "ten". 103 | 104 | + `insert(word)` - if a string is not present in the trie, add it. 105 | 106 | + `num_words(str)` - returns the number of words that start with a given string 107 | 108 | Advanced: 109 | + One of the useful applications of tries is that they can be used to predict text. How could you refactor your code so it can track the words that are most commonly searched? Given the first n letters, how likely is any given letter to be next? 110 | -------------------------------------------------------------------------------- /020619_trees/structure-of-tree.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghoagland-flatiron/algorithms_club/28f86fc20c030b72c725435f0a09d59959ad7a0b/020619_trees/structure-of-tree.jpeg -------------------------------------------------------------------------------- /032019_bubble_sort_quick_sort/slides.md: -------------------------------------------------------------------------------- 1 | # Sorting Algorithms 2 | ## Bubble Sort & Quick Sort 3 | 4 | --- 5 | 6 | ## What is a sorting algorithm? 7 | In the most basic terms, a sorting algorithm is a process for putting things in a certain order. The output of running a collection through a sorting algorithm should: 8 | 1. Return a collection in a _non-decreasing order_ (or non-increasing order). This means that each element is no smaller than the previous value (no bigger for non-increasing). 9 | 2. Return a _permutation_ of the original collection; there is a one-to-one mapping from the original collection to the output collection. 10 | 11 | 12 | ## Comparing Functions 13 | 14 | Your input will include both the collection and some _comparing_ function. Are you sorting alphabetically? Numerically? Are you sorting by a particular value in a hash or object? These functions take in two values and determine whether they need to be switched or not. See [here](https://ruby-doc.org/core-2.2.0/Array.html#method-i-sort) for Ruby's implementation. Given `a` and `b`: 15 | 16 | - if the function returns `-1`, `a` is less than `b` and `a` should come first in the result - no swapping needed. 17 | 18 | - if the function returns `1`, `a` is greater than `b` and `b` should come first in the result - swapping is needed. 19 | 20 | - if the function returns `0`, `a` is equivalent to `b` and they can be returned in either order - no swapping needed, but nothing bas about swapping either. 21 | 22 | We are not going to spend a lot of time on how to write different types of comparison, we are going to focus on writing the sorting algorithm. 23 | 24 | 25 | ## Bubble Sort 26 | 27 | A bubble sort works by trying to find the smallest (or largest) value first, then the next smallest, etc. It does this by running through the collection and each time it compares values, it keeps the smaller one. At the end of the array, this finds the smallest result. Now that you have found the smallest value, you look at the unsorted collection and repeat the process to find the next smallest value. 28 | Despite the name, the start of [this video] (https://www.youtube.com/watch?v=aXXWXz5rF64) provides a nice visual explanation. 29 | 30 | There are a lot of nice sorting videos out there, it is worth a look at them! 31 | 32 | - https://www.youtube.com/watch?v=WaNLJf8xzC4 33 | - https://www.youtube.com/watch?v=lyZQPjUT5B4 (this one has folk dancing!) 34 | 35 | ## Quick Sort 36 | 37 | Quick sort works a bit differently than a bubble sort, but with some similarities. In quick sort, you start by picking a random element - this can be the first, or the last, or anywhere in the middle. This is your pivot. You then compare this pivot to each other ball, moving the smaller values "left" (to the smaller end) and the larger values "right". This pivot is now in its final spot. 38 | 39 | Why? Even if the the smaller values are not in the right order, everything that is smaller is now to the "left". Similarly, everything that bigger is to the "right". 40 | 41 | Repeat this process to the left and to the right to sort those until you have one or two values, which can be immediately sorted. 42 | 43 | 44 | 45 | ## Time Complexity 46 | 47 | Bubble Sort has one of the worst time complexities out there. On average and in the worst-case scenario, the time is on the order of `n^2`. In general terms, each of the elements needs to be compared to each other element. 48 | 49 | Unlike with bubble sort, though, quick sort does more than find the position of one value. In the comparisons, it has partitioned the collection into values smaller than the pivot, the pivot itself, and values larger than the pivot. In terms of time complexity, having two arrays to sort will be faster than combining those arrays and sorting the result, since each value is now making roughly half as many comparisons, not one fewer. 50 | 51 | As a result, the average time of quick sort is `n log(n)`. However, if you are unlucky in choosing pivots and happen to choose the pivots so they are repeatedly close to the end, the partition does less to make smaller collections, so the worst-case is on the order of `n^2` as well. 52 | 53 | 54 | ## Problems 55 | 56 | For the sake of these problems, assume you have a function `compare(a, b)` that works like the comparison functions described. 57 | 58 | Relationship | Return 59 | -------------|------- 60 | a < b | -1 61 | a > b | 1 62 | a = b | 0 63 | 64 | 65 | 66 | ### Bubble Sort 67 | 68 | Your function should take in an array and return the non-decreasing array. Use `compare` to determine when values need to be swapped. 69 | 70 | ### Quick Sort 71 | 72 | Same directions as bubble sort with the added advice that it might be useful to write a helper function that does the work of picking the pivot and swapping values accordingly. 73 | -------------------------------------------------------------------------------- /091118_intro_to_algorithms/problems.md: -------------------------------------------------------------------------------- 1 | # Intro to Algorithms 2 | ## Questions 3 | 4 | ### Capitalize 5 | Write a function that captializes the first letter of every word in a string. 6 | 7 | ### String Search (or, `indexOf`) 8 | Note: You may not use built-in methods like `indexOf` to shortcut for this. 9 | 10 | Given two strings, find the index of the first appearance of the first string inside the other. Think of this as finding the needle in the haystack. 11 | 12 | Given `'had'` and `'you had me at hello'`, you would return `4` since index 4 is where `'had'` begins. 13 | 14 | Things to consider: What are the edge cases? How will you handle them? How can you make this function more efficient? 15 | 16 | 17 | ### Roman Numerals 18 | Write a method that converts an integer to its Roman numeral equivalent. In other words, if we give our method the Arabic number 476, our method will return the Roman numeral CDLXXVI. 19 | 20 | | Arabic Number | Roman Numeral | 21 | | -------------- | ------------- | 22 | | 1 | I | 23 | | 2 | II | 24 | | 3 | III | 25 | | 4 | IV | 26 | | 5 | V | 27 | | 6 | VI | 28 | | 9 | IX | 29 | | 10 | X | 30 | | 20 | XX | 31 | | 40 | XL | 32 | | 50 | L | 33 | | 100 | C | 34 | | 500 | D | 35 | | 1000 | M | 36 | 37 | More examples: 38 | 39 | | Arabic Number | Roman Numeral | 40 | | ------------- | ------------- | 41 | | 4 | IV | 42 | | 9 | IX | 43 | | 14 | XIV | 44 | | 44 | XLIV | 45 | | 99 | XCIX | 46 | | 400 | CD | 47 | | 944 | CMXLIV | 48 | -------------------------------------------------------------------------------- /091118_intro_to_algorithms/slides.md: -------------------------------------------------------------------------------- 1 | class: center, middle 2 | 3 | # Intro to Algorithms 4 | 5 | -- 6 | 7 | An algorithm is a set of steps to accomplish a task. 8 | 9 | --- 10 | For example, you might have a morning algorithm. 11 | 12 | -- 13 | 14 | 1. Wake up 15 | 2. Brush teeth 16 | 3. Shower 17 | 18 | -- 19 | 20 | etc. 21 | 22 | --- 23 | 24 | # More algorithms 25 | - Compressing data 26 | - Encryption 27 | - Searching 28 | - Finding directions 29 | - Pretty much any process that can be broken down into small repeatable steps. 30 | --- 31 | # Historical note: 32 | 33 | > The word 'algorithm' has its roots in latinizing the name of Muhammad ibn Musa al-Khwarizmi in a first step to algorismus. Al-Khwārizmī (Persian: خوارزمی‎, c. 780–850) was a Persian mathematician, astronomer, geographer, and scholar in the House of Wisdom in Baghdad, whose name means 'the native of Khwarezm', a region that was part of Greater Iran and is now in Uzbekistan. 34 | 35 | From [Wikipedia](https://en.wikipedia.org/wiki/Algorithm). 36 | 37 | --- 38 | # What does this mean for us? 39 | 40 | Pretty much any time you write a function or a helper method, you are writing some kind of algorithm. 41 | 42 | --- 43 | 44 | # Using Algorithms 45 | 46 | Sometimes it is useful to use a built-in or already worked out algorithm - someone else wrote an optimized sorting algorithm for many languages, so you don't have to. If you are doing something more complicated or specific to you, however, you should write your own. 47 | 48 | --- 49 | 50 | # Using Algorithms 51 | 52 | There are two main ways to judge algorithms: 53 | 54 | 1. Correctness - will your algorithm return the right values? What happens when you put in wrong values? What happens to edge cases? 55 | 2. Efficiency - how quickly does your algorithm run? Is there a way to make it run more efficiently? 56 | **Note:** We will _definitely_ return to this topic and it will probably get its own meeting of Algorithm Club. In the meantime, ask Gigi for the link to her Big O lecture. 57 | 58 | --- 59 | 60 | # Algorithms and the Technical Interview 61 | 62 | As you all probably know, many companies vet candidates by testing their ability to write algorithms, often by whiteboarding. 63 | 64 | 65 | **These interviews are just as much about your communication skills as your problem-solving skills.** 66 | 67 | 68 | As you work on problems here and in interviews, make sure you check your understanding of the question with the interviewer and talk about your approach before you start writing any function. (Again, we'll talk a bit more about this in future.) 69 | 70 | --- 71 | 72 | Problem time! 73 | -------------------------------------------------------------------------------- /091718_how_to_approach_a_technical_interview/problems.md: -------------------------------------------------------------------------------- 1 | # How to approach a technical interview 2 | ## The REACTO approach 3 | From my time at Grace Hopper/Fullstack, I learned this useful mnemonic to approach technical interviews. 4 | For more information, this is their blog post on the topic: https://www.fullstackacademy.com/blog/how-to-ace-a-technical-interview-reacto 5 | ``` 6 | R E A C T O 7 | e x p o e p 8 | p a p d s t 9 | e m r e t i 10 | a p o m 11 | t l a i 12 | e c z 13 | h e 14 | ``` 15 | 16 | ## Questions 17 | ### Coconuts 18 | You are stuck on a desert island and for some inscrutable reason your survival depends on figuring out which one of 9 otherwise identical coconuts is heavier than the others. You didn't bring your bathroom scale because... you're stuck there, it's not really a choice. But you have some planks and a rock and have made a balance scales. What is the minimum number of times you need to weigh the coconuts to be certain which is the odd one out - the sun is so hot and you really need this heavy coconut. 19 | 20 | ### Balanced Brackets 21 | 22 | 1. Write a function that analyzes a string and tells if the the parentheses in the string are balanced - i.e. `(abcd)` is balanced but `(efgh))` is not. Also note that while `)(ijkl)(()` may have an equal number of open and closed parentheses, it is not valid. 23 | 24 | 2. Refactor your function to work for not just parentheses but square and curly braces as well. Note that `([{({qrst()})}])` is valid but `([mnop)]` is invalid. 25 | 26 | ### Count Duplicates 27 | 28 | Write a function that will return the count of _distinct_ case-insensitive alphabetic characters and numeric digits that occur more than once in the input string. The input string can be assumed to contain only alphabets (both uppercase and lowercase) and numeric digits. 29 | (From [Codewars](http://www.codewars.com/kata/54bf1c2cd5b56cc47f0007a1/train/python)) 30 | -------------------------------------------------------------------------------- /092518_data_structures/singly-linked-list.svg.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghoagland-flatiron/algorithms_club/28f86fc20c030b72c725435f0a09d59959ad7a0b/092518_data_structures/singly-linked-list.svg.png -------------------------------------------------------------------------------- /092518_data_structures/slides.md: -------------------------------------------------------------------------------- 1 | class: center, middle 2 | 3 | # Data Structures 4 | ## Linked Lists and Binary Search Trees 5 | 6 | --- 7 | 8 | # Data Structures and Abstract Data Types 9 | 10 | A **data structure** is the concrete implementation of an **abstract data type** (ADT). 11 | 12 | You may see these terms used fairly interchangeably. An ADT is more high-level description of the data relationship and a description of how that data may operated upon whereas a data structure has more information on how these things are defined and actually implemented. 13 | 14 | --- 15 | 16 | # Data Structures and Abstract Data Types 17 | From the [Wikipedia article on ADTs](https://en.wikipedia.org/wiki/Abstract_data_type) (emphasis mine): 18 | > In computer science, an abstract data type (ADT) is a mathematical model for data types, where a **data type** is defined by its behavior (semantics) from the **point of view of a user** of the data, specifically in terms of possible values, possible operations on data of this type, and the behavior of these operations. This contrasts with **data structures**, which are concrete representations of data, and are the **point of view of an implementer**, not a user. 19 | 20 | --- 21 | 22 | # Linked Lists 23 | 24 | A singly-linked list is a collection of ordered nodes. Each node in a linked list has a **value** and a reference to the **next** node in the list. Think of it a bit like a treasure hunt, where each place you find points to the next place. 25 | 26 | A doubly-linked list has a reference to the **previous** node in addition to the next node. 27 | 28 | 29 | ![Image of a linked list][linked-list] 30 | 31 | --- 32 | 33 | # Linked Lists 34 | 35 | The last node will point to `nil` or equivalent value so you know when you have hit the end. 36 | 37 | ![Image of a linked list][linked-list] 38 | 39 | --- 40 | 41 | # Singly Linked Lists 42 | 43 | When implementing, you will need two classes. 44 | 45 | First, a **Node** class, where each node holds **value** and a reference to the **next** node, 46 | 47 | Next, you will need a **LinkedList** class that holds the **head** or first value in a linked list. This is where most of your methods will live. 48 | Some methods you should work on: 49 | 50 | - `add_value_at_index(index, value_to_add)` 51 | - `contains(value)` 52 | - `length()` 53 | - `delete(value)` 54 | - `contains_loop()` 55 | - `clone()`* - copy of each node with pointers to the new copies. 56 | 57 | -- 58 | 59 | [Break for code] 60 | 61 | 62 | --- 63 | 64 | # Binary Tree 65 | 66 | A binary tree is a collection of nodes where each node has between zero and two children. Put simply, each node has a **value**, **left** child, and a **right** child. Where a node does not have a left or right child, the node should point to `nil`. The **root** of this tree has value 5. 67 | 68 | Binary Tree: 69 | 70 | ``` 71 | 5 72 | / \ 73 | 7 9 74 | \ / \ 75 | 8 1 4 76 | / \ \ 77 | 3 4 9 78 | ``` 79 | 80 | --- 81 | 82 | ## Binary Search Tree 83 | 84 | A binary search tree is a binary tree with the following additional properties: 85 | 86 | - The left subtree of a node contains only nodes with values less than the node’s value. 87 | - The right subtree of a node contains only nodes with values greater than or equal to the node’s value. 88 | - The left and right subtree each must also be a binary search tree. 89 | 90 | Binary Search Tree: 91 | 92 | ``` 93 | 8 94 | / \ 95 | 3 10 96 | / \ / \ 97 | 1 6 9 12 98 | / \ / 99 | 4 7 11 100 | ``` 101 | 102 | 103 | -- 104 | 105 | A key thing to remember here is that while a node might be the child of one node, it may have children for which it is the root. 106 | 107 | 108 | --- 109 | 110 | # Binary Search Tree Methods 111 | 112 | ``` 113 | 8 114 | / \ 115 | 3 10 116 | / \ / \ 117 | 1 6 9 12 118 | / \ / 119 | 4 7 11 120 | ``` 121 | - `add(value)` 122 | 123 | - `contains(value)` 124 | 125 | - `find_biggest()` / `find_smallest()` 126 | 127 | - `depth_first_list()` * - list the values in order from smallest to largest. The above example would return 1, 3, 4, 6, 7, 8, 9, 10, 11, 12. 128 | 129 | - `breadth_first_list()` * - list the elements level by level. Starting at the root, read the values left to right and then move down to the next level. So the above would return 8, 3, 10, 1, 6, 9, 12, 4, 7, 11. 130 | 131 | - `delete_node()` ** 132 | 133 | - `self.create()` - creates a BST based on an array. 134 | 135 | 136 | 137 | [linked-list]: singly-linked-list.svg.png "Singly linked list" 138 | -------------------------------------------------------------------------------- /100218_big_o/big_O_chart.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghoagland-flatiron/algorithms_club/28f86fc20c030b72c725435f0a09d59959ad7a0b/100218_big_o/big_O_chart.png -------------------------------------------------------------------------------- /100218_big_o/big_O_guide.md: -------------------------------------------------------------------------------- 1 | # Big O Cheatsheet 2 | 3 | For a helper with various data structures and sorting algorithms, view the [Big O Cheatsheet site](http://bigocheatsheet.com/). 4 | ![Graph of various complexities](./big_O_chart.png) 5 | 6 | A longer video using the same slides can be found on at https://www.youtube.com/watch?v=tx0qG3DhA8Q&feature=youtu.be 7 | 8 | 9 | ## O(1): Constant 10 | Things with O(1) do not change at all when the input changes. 11 | 12 | **Examples:** 13 | 14 | Space: Storing any value that is not dependent on input or does not grow as the input grows. Examples: counters, booleans, a single variable. 15 | 16 | Time: Setting a single variable, accessing a value in an object with a key or in an array with the index, performing built-in mathematical operations 17 | 18 | ## O(n): Linear 19 | Operations that are related directly to the input. So each time the input grows by a unit, the time increases by a set amount. It may be one more step or ten, but each step accounts for a set increase. 20 | 21 | Time: mapping or iterating over an array, performing an action a number of times based on the input. 22 | 23 | Space: mapping or copying arrays, creating a new value for each iteration 24 | 25 | ## O(n^2): Quadratic and 26 | ## O(n^c): Polynomial 27 | Think loops on loops. The number of nested loops will give you the exponent. So a loop in a loop is n^2. 28 | 29 | Time: Listing all possible combinations of 2 (or c) objects, often a brute force solution when trying to find pairs of things. 30 | 31 | Space: All possible combinations of two (or c) objects. 32 | 33 | 34 | --- 35 | ## Less straightforward complexities 36 | The following examples are good to know but usually involve more thought or things happing under the hood. 37 | 38 | ## O(log n): Logarithmic 39 | This is common with lookup time in tree structures. One way to think about it is that every time you move down a level in a tree, the number of things you have left to go through roughly halves (or is cut to a third or a quarter, etc.) A binary search is a good example. 40 | 41 | ## O(n log(n)): Log Linear 42 | In the wild, this is most common as the time that many common sorting algorithms take. If you are running a `.sort`, chances are the built-in function uses something like merge sort under the hood (or quick sort, which has O(n^2) but Θ(n log(n)) and Ω(n log(n))). 43 | 44 | ## O(2^n): Exponential (Or O(c^n)) 45 | Think brute-force Fibonacci. Every unit of growth doubles the number of steps you have to do. Also think of brute-force password guessing. 46 | 47 | ## O(n!): Factorial (Also O(n^n)) 48 | This is for every possible combination of n things. So as n grows, the base **and** the exponent grows. A common example is the Traveling Salesman Problem. This is **very** bad and usually something you want to avoid. 49 | -------------------------------------------------------------------------------- /100218_big_o/problems.md: -------------------------------------------------------------------------------- 1 | # Big O 2 | ## Fibonacci Sequence 3 | The Fibonacci sequence starts with 0 and 1 and then every successive element is the sum of the previous two in the sequence. 4 | ``` 5 | 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... 6 | ``` 7 | The brute force approach has a O(2^n). How might you do this problem in linear time? 8 | 9 | ## Ransom Note Checker 10 | Given two strings representing your note and a magazine, return a boolean that tells you if you can create the note from the magazine. 11 | 12 | *Extra Credit*: 13 | Return something that tells me where in the magazine I can find all the words for my ransom note. 14 | 15 | ## Pair Sum 16 | Given a sorted array and a number, return true if any pairs of numbers in the array that sum to that number, else return false. 17 | -------------------------------------------------------------------------------- /100218_big_o/slides.md: -------------------------------------------------------------------------------- 1 | class: center, middle 2 | 3 | # Big O 4 | 5 | --- 6 | 7 | # What Is This Big O Thing I Keep Hearing About? 8 | 9 | Big O is a way to measure how fast a function will run or how much memory it will use. 10 | 11 | --- 12 | 13 | # What Is This Big O Thing I Keep Hearing About? 14 | 15 | Big O measures how the space used or length of time will change as the input “grows”. 16 | 17 | This can refer to either the **time complexity** or **space complexity** of a particular function. 18 | 19 | --- 20 | 21 | # What Is This Big O Thing I Keep Hearing About? 22 | 23 | When we talk about Big O, it tends to be very abstract - we mostly want to know roughly the space used or the time taken as the function inputs grow towards infinity. 24 | 25 | --- 26 | 27 | # How Big Do You Mean? 28 | 29 | What we define as the inputs growing depends entirely on the function - if you have a function that takes in arrays and concatenates them, it doesn't matter as much in terms of speed how long any particular array is, just how many total there are. If you are iterating over an array, however, the length of the array will mean running the iteration one more time for each additional element. 30 | 31 | --- 32 | 33 | # What Is Big O? 34 | 35 | **Big O** is specifically the **worst-case** scenario in terms of time or space. 36 | 37 | **Big Θ** (Big Theta) is the **average** scenario. 38 | 39 | **Big Ω** (Big Omega) is the **best-case** scenario. 40 | 41 | --- 42 | 43 | # How Do We Measure Big O? 44 | 45 | When we want to measure, these are rough approximations. Does the function take up the same amount of space no matter the input or do you need to hold a changed value? Does every step halve the number of things you have yet to look at? These more abstract things can help you identify Big O in the wild. 46 | 47 | --- 48 | 49 | # Highlights 50 | 51 | ## O(1) - Constant Time (or Space) 52 | 53 | Think something that will always take the same amount of time. This is looking up a value using a key in hashes, accessing an array at a particular index, assigning a variable to a set number or boolean. 54 | 55 | ## O(n) - Linear Time 56 | 57 | These are things where you have to iterate or look at each element of an array or string or whatever it is. Think of mapping an array - you have to look at each element and you are returning a new array of the same length. 58 | 59 | --- 60 | 61 | # Highlights 62 | 63 | ## O(n^2) - Quadratic Time or 64 | ## O(n^c) - Polynomial Time 65 | 66 | Think nested loops. If you have an array of length n and you are looping over it inside of another loop, for each element in the array, you are looking at each element again. So inside of a step happens n times, you are doing something n times. 67 | 68 | --- 69 | -------------------------------------------------------------------------------- /100918_recursion/Call-stack-of-Fibonacci.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghoagland-flatiron/algorithms_club/28f86fc20c030b72c725435f0a09d59959ad7a0b/100918_recursion/Call-stack-of-Fibonacci.jpg -------------------------------------------------------------------------------- /100918_recursion/fib-tree.jpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghoagland-flatiron/algorithms_club/28f86fc20c030b72c725435f0a09d59959ad7a0b/100918_recursion/fib-tree.jpeg -------------------------------------------------------------------------------- /100918_recursion/problems.md: -------------------------------------------------------------------------------- 1 | # Recursion 2 | ## Questions 3 | 4 | 1. `reverse` - Write a recursive function to reverse a string. So `reverse('recursion') => 'noisrucer'`. What is your base case? How can you build up from there to the next step or how can you get reduce a long string to a base case? 5 | 6 | 2. `arr_sum` - Given an array that is composed of numbers and arrays that themselves may contain arrays and/or numbers that may be arbitrarily deeply nested, write a function that returns the sum of all the numbers. For example, `arr_sum([1, [2, [3, [4, [5]]]]]) => 15` and `arr_sum([[[[[[2], [3]]]]]]) => 5`. You may not convert to a string or otherwise flatten. 7 | 8 | 3. Boggle Checker - given a function `is_word?` that returns true if a string is a word and false if it is not and an array that represents the letters on a Boggle board, return an array of all possible words. 9 | 10 | ### What is Boggle? 11 | 12 | Boggle is a game where you have a 4 x 4 grid of letters and you look for words. Words are made of letters that are directly adjacent to or diagonal from the last letter. A letter in a particular position can only be used once per word. 13 | 14 | ``` 15 | B A B Z 16 | T R E M 17 | O P M J 18 | Y W H T 19 | ``` 20 | 21 | Above, we have the words 'art', 'bat', and 'rope', and 'babe' but not 'rare' - we can't use the same 'r' twice. 22 | -------------------------------------------------------------------------------- /100918_recursion/slides.md: -------------------------------------------------------------------------------- 1 | class: center, middle 2 | 3 | # Recursion 4 | 5 | --- 6 | 7 | # What is recursion? 8 | 9 | > In mathematics and computer science, a class of objects or methods exhibit recursive behavior when they can be defined by two properties: 10 | > 1. A simple base case (or cases)—a terminating scenario that does not use recursion to produce an answer 11 | > 2. A set of rules that reduce all other cases toward the base case 12 | 13 | Thanks, [Wikipedia](https://en.wikipedia.org/wiki/Recursion). 14 | 15 | --- 16 | 17 | # What? 18 | 19 | Imagine you are trying to solve a problem and you realize that having the solution for a "smaller" piece of the problem would help! 20 | 21 | In code, we can do this by calling a function inside of its definition. 22 | 23 | A recursive function needs at least two cases - one where the function calls itself and one where it does not. A case where the function does not call itself is a _base case_. This is where the function stops calling itself so there won't be an infinite loop. The other type of case, the _recursive case_, is one where the function calls itself in a way that it will eventually lead to the base case. This usually means calling the function with a smaller number or a shorter array - something that, with enough calls, will reach the condition for the base case. 24 | 25 | --- 26 | 27 | # Fibonacci 28 | 29 | The Fibonacci sequence where every number is the sum of the previous two, starting with 0 and 1. In order to get the nth value, we need the n-1 and n-2 value. 30 | Those words should 31 | --- 32 | 33 | # Fibonacci continued 34 | 35 | ```ruby 36 | def fib(n) 37 | if n <= 0 38 | 0 39 | elsif n == 1 40 | 1 41 | else 42 | fib(n - 1) + fib (n - 2) 43 | end 44 | end 45 | ``` 46 | 47 | --- 48 | 49 | # Fibonacci continued 50 | ![Fibonacci tree](./fib-tree.jpeg) 51 | [Source](https://cdn-images-1.medium.com/max/925/1*svQ784qk1hvBE3iz7VGGgQ.jpeg) 52 | 53 | 54 | --- 55 | 56 | # Binary Search Tree 57 | 58 | Summing up a binary tree is another example. If we wanted to sum up the tree below, we could think of this from the root as adding the sum of all elements to the left and all of the elements to the right to the value of the root itself. 59 | 60 | ``` 61 | 5 62 | / \ 63 | 7 9 64 | \ / \ 65 | 8 1 4 66 | / \ \ 67 | 3 4 9 68 | ``` 69 | 70 | --- 71 | 72 | # Recursion and Big O 73 | 74 | In the real world, recursion is often frowned upon for the time it takes or how easily it can lead to a stack overflow. There are some types of search that use it or you may see it in some games to create a game loop. Just remember that you need to have some idea of how the function is going to be terminated with a base case. 75 | 76 | --- 77 | 78 | class: center, middle 79 | # Question Time! 80 | -------------------------------------------------------------------------------- /101718_problems/path.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghoagland-flatiron/algorithms_club/28f86fc20c030b72c725435f0a09d59959ad7a0b/101718_problems/path.png -------------------------------------------------------------------------------- /101718_problems/problems.md: -------------------------------------------------------------------------------- 1 | # Miscellaneous 2 | ## Questions 3 | 4 | 1. How many windows are there in New York? 5 | 6 | This is a classic question designed to see how you handle abstraction. Think about approaches and discuss how you would communicate your answer to this or a similar question - what is this question actually asking? 7 | 8 | 2. Write a function that performs a **binary search**. 9 | 10 | Given a sorted array and a value, this function will return true if the array contains the value and false otherwise. 11 | 12 | Think of this like looking up a word in the dictionary - start in the middle and determine if you have found what you are looking for and return true or if you need to look in the section before or after and repeat the process. Repeat until the value is found or until there are no more elements. 13 | 14 | ``` 15 | input: [1, 3, 4, 6, 7, 8, 10], 7 16 | 17 | length => 7 18 | midpoint => index: 3, value: 6 19 | 20 | [1, 3, 4, 6, 7, 8, 10] 21 | | ^ | 22 | 23 | 7 > 6 => new section from index 4 - index 6 24 | 25 | length => 3 26 | midpoint => index: 5, value: 8 27 | 28 | [1, 3, 4, 6, 7, 8, 10] 29 | | ^ | 30 | 31 | 7 < 8 => new section from index 5 - index 5 32 | 33 | length => 1 34 | midpoint => index: 5, value: 7 35 | 36 | [1, 3, 4, 6, 7, 8, 10] 37 | |^| 38 | 39 | 7 = 7 => return true 40 | ``` 41 | 42 | 3. Sudoku Checker 43 | 44 | Write a function that takes in an array that represents a sudoku grid. Return true or false based on whether it is a valid solution. 45 | Rules of sudoku: https://en.wikipedia.org/wiki/Sudoku 46 | 47 | 4. Number of Paths 48 | 49 | Question from [Pramp](https://www.pramp.com/). 50 | 51 | You’re testing a new driverless car that is located at the Southwest (bottom-left) corner of an n×n grid. The car is supposed to get to the opposite, Northeast (top-right), corner of the grid. Given n, the size of the grid’s axes, write a function numOfPathsToDest that returns the number of the possible paths the driverless car can take. 52 | 53 | ![the car must stay in the lower section](./path.png) 54 | 55 | For convenience, let’s represent every square in the grid as a pair (i,j). The first coordinate in the pair denotes the east-to-west axis, and the second coordinate denotes the south-to-north axis. The initial state of the car is (0,0), and the destination is (n-1,n-1). 56 | 57 | The car must abide by the following two rules: it cannot cross the diagonal border. In other words, in every step the position (i,j) needs to maintain i >= j. See the illustration above for n = 5. In every step, it may go one square North (up), or one square East (right), but not both. E.g. if the car is at (3,1), it may go to (3,2) or (4,1). 58 | 59 | Explain the correctness of your function, and analyze its time and space complexities. 60 | -------------------------------------------------------------------------------- /103018_number_theory/Sieve_of_Eratosthenes_animation.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghoagland-flatiron/algorithms_club/28f86fc20c030b72c725435f0a09d59959ad7a0b/103018_number_theory/Sieve_of_Eratosthenes_animation.gif -------------------------------------------------------------------------------- /103018_number_theory/problems.md: -------------------------------------------------------------------------------- 1 | # Number Theory 2 | ## More Information 3 | 4 | If you want to learn more about cryptography, here is a site that helps you build up your crypto skills: 5 | https://cryptopals.com/ 6 | 7 | Here is a link to a talk I gave that is an introduction to the topic of elliptic curve cryptography: 8 | https://www.youtube.com/watch?v=JPTfivFVGe8 9 | 10 | ## Problems 11 | 12 | ### Decoder Ring 13 | You want to trade secret messages with your friends. You decide that you are going to shift all of your messages over by a few letters so `A` becomes `F`, `B` becomes `G`, and `Z` becomes `E`. 14 | 15 | | plaintext | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | 16 | |------------|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---| 17 | | encrypted | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | A | B | C | D | E | 18 | 19 | Write two functions, one to encode and one to decode, that take in the number of letters you are shifting by and the string you are encoding or decoding. 20 | 21 | For a more advanced version, try making an [Affine Substitution Cipher](https://en.wikipedia.org/wiki/Affine_cipher). 22 | 23 | ### Sieve of Eratosthenes 24 | 25 | This is a method for finding all primes less than a set number `n`. Starting from 2, you remove all multiples of two up to and including `n`. You move on to the next number that hasn't been removed and repeat the process until you have found all the primes. Since you have removed the multiples of all the smaller primes, if a number has not been removed it must be prime. 26 | 27 | ![Sieve of Eratosthenes Animation](./Sieve_of_Eratosthenes_animation.gif) 28 | 29 | For more information, visit https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes 30 | 31 | ### Euclidean Algorithm 32 | 33 | A quicker way to find the GCD of two numbers than finding their prime factorizations and comparing is to use the Euclidean algorithm. 34 | 35 | An introduction to the Euclidean algorithm can be found at http://sites.math.rutgers.edu/~greenfie/gs2004/euclid.html 36 | 37 | This explains how it works and provides some links that explain _why_ this works. 38 | 39 | Basically, assign `a` and `b` so `a > b`. 40 | Divide `a` by `b` to find the `q` and `r` where 41 | 42 | `a = qb + r` 43 | and 44 | `0 <= r < b` 45 | 46 | Repeat this process with `b` and `r` as your new `a` and `b`. The process ends when `r = 0` and then your `q` value is the GCD. 47 | 48 | Example: 49 | ``` 50 | 88 and 121 51 | 52 | a = q * b + r 53 | 54 | 121 = 1 * 88 + 33 55 | 88 = 2 * 33 + 22 56 | 33 = 1 * 22 + 11 57 | 22 = 2 * 11 + 0 58 | ^ stop 59 | 60 | GCD(121, 88) = 11 61 | ``` 62 | -------------------------------------------------------------------------------- /103018_number_theory/slides.md: -------------------------------------------------------------------------------- 1 | 2 | # Number Theory 3 | 4 | --- 5 | 6 | # What is Number Theory? 7 | 8 | Number theory is the study of integers, i.e. whole numbers. Much of the subject is devoted to studying the general properties of integers. 9 | 10 | Of particular interest are __prime numbers__, numbers only divisible by themselves and 1. 11 | 12 | --- 13 | 14 | # Modulo 15 | 16 | We say that a number `a` is congruent to a number `b` modulo a number `n` if there are some integer `m`, so that `a + mn = b`. Another way of thinking about this is that `a` and `b` have the same **remainder** when divided by `n`. 17 | 18 | $$ 19 | a \equiv b (\bmod n) 20 | $$ 21 | 22 | 23 | --- 24 | 25 | # Prime numbers 26 | 27 | Why are prime numbers so special? The Fundamental Theorem of Arithmetic says that every number greater than one can be represented as the product of a unique set of prime numbers. This fact, while it may seem dry, is the foundation of much of our modern life. 28 | 29 | For the past thirty years, much of our digital security has been built on this foundation. 30 | 31 | --- 32 | 33 | # Greatest common denominator 34 | 35 | The GCD of a pair of numbers is the largest number that divides both numbers. So the GCD of 30 and 24 is 6. 36 | 37 | How can we figure this out? If we look at the prime factorization of both numbers, we can see that their common factors are a single two and a three. 38 | 39 | $$ 40 | 30 = 2 * 3 * 5 41 | \\ 42 | 24 = 2 * 2 * 2 * 3 = 2^3 * 3 43 | $$ 44 | 45 | --- 46 | 47 | # Greatest common denominator 48 | 49 | Two numbers are said to be _relatively prime_ or _coprime_ if their GCD is 1. Another way of saying this is that the largest number that divides both the given numbers is 1. For example, neither 10 nor 21 is prime, but they are coprime. 50 | 51 | $$ 52 | 10 = 2 * 5 53 | \\ 54 | 21 = 3 * 7 55 | $$ 56 | 57 | --- 58 | 59 | # So what? 60 | 61 | Imagine you multiply two very large prime numbers - each hundreds of digits long. If I am trying to factorize this number, I am going to have to check every prime number between 1 and the smaller prime find either one by brute force since nothing but 1, the number itself, and these two primes can divide the number. 62 | 63 | This is called the **discrete log problem** and is the basis of much of our modern cryptosystems. 64 | 65 | --- 66 | -------------------------------------------------------------------------------- /110618_graphs/adjacencymatrix.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghoagland-flatiron/algorithms_club/28f86fc20c030b72c725435f0a09d59959ad7a0b/110618_graphs/adjacencymatrix.png -------------------------------------------------------------------------------- /110618_graphs/listadjacency.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghoagland-flatiron/algorithms_club/28f86fc20c030b72c725435f0a09d59959ad7a0b/110618_graphs/listadjacency.png -------------------------------------------------------------------------------- /110618_graphs/problems.md: -------------------------------------------------------------------------------- 1 | # Graphs 2 | 3 | ## Questions 4 | 5 | ### Build a Graph 6 | 1. Write a _vertex_ class and a _graph_ class. This will be for a **directed** graph with vertices using adjacency lists to store edges. What information does each need? How can you store that? 7 | 8 | 2. Build the following methods: 9 | 10 | On a vertex `x`: 11 | - `x.neighbors()` - lists the vertices this vertex has edges to 12 | - `x.adjacent(y)` - does x have an edge to y? 13 | - `x.add_edge(y)` - makes an edge from x to y 14 | 15 | On a graph `G`: 16 | - `G.contains(x)` - is vertex x in this graph? 17 | - `G.add_vertex(x)` - adds a vertex to the graph 18 | - `G.remove_vertex(x)` - removes a vertex from the graph and any edges going to it 19 | 20 | ### Traverse a Graph 21 | Oftentimes in graphs, we want to look for a path between two vertices (think six degrees of Kevin Bacon). Write a method that returns an array of the vertices you would need to traverse from vertex x to y or nil if there is none. 22 | 23 | Things to consider: what happens if you get in a loop? How will you know which vertices you have visited? 24 | 25 | 1. `G.breadth_first_path(x, y)` - a *breadth-first* search looks first at the neighbors of x, and then the neighbors' neighbors, and so on until you have either found y or have visited every node you can reach. [Link to more details.](https://www.tutorialspoint.com/data_structures_algorithms/breadth_first_traversal.htm) 26 | 2. `G.depth_first_path(x, y)` - a *depth-first* search follows a single path until it either finds y or has visited every node possible from that branch. [Link to more details.](https://www.tutorialspoint.com/data_structures_algorithms/depth_first_traversal.htm) 27 | 28 | What are the benefits of each of these types of search? What are the drawbacks? 29 | -------------------------------------------------------------------------------- /110618_graphs/slides.md: -------------------------------------------------------------------------------- 1 | class: center, middle 2 | 3 | # Graphs 4 | 5 | --- 6 | 7 | # What is a graph? 8 | 9 | A graph consists of two main components. 10 | 11 | 1. A finite set of **vertices**, also called **nodes**. 12 | 2. A finite set of **edges** that connect two nodes. These edges may be _directed_, so an edge connecting vertex `a` to vertex `b` is different from the edge connecting `b` to `a`, or _undirected_, where those edges are the same. An edge can also have other properties such as weight. 13 | 14 | Graphs have many applications as a data structure. One application is to represent networks like the subway system or city streets or social networks. 15 | 16 | ![Undirected graph](./undirectedgraph.png) 17 | 18 | --- 19 | 20 | # How do we represent graphs? 21 | 22 | ![Undirected graph](./undirectedgraph.png) 23 | 24 | How do we represent the edges in data? 25 | 26 | One way is through an _adjacency list_, where each node is represented in a hash with a linked list of the nodes it is connected to. 27 | ![Adjacency list](./listadjacency.png) 28 | 29 | --- 30 | 31 | # How do we represent graphs? 32 | 33 | ![Undirected graph](./undirectedgraph.png) 34 | 35 | Another way to represent the relationships between nodes is via an _adjacency matrix_ where 1 represents a directed edge between a pair of nodes and 0 means there is no edge. 36 | 37 | ![Adjacency matrix](./adjacencymatrix.png) 38 | 39 | --- 40 | -------------------------------------------------------------------------------- /110618_graphs/undirectedgraph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ghoagland-flatiron/algorithms_club/28f86fc20c030b72c725435f0a09d59959ad7a0b/110618_graphs/undirectedgraph.png -------------------------------------------------------------------------------- /112118_problems/problems.md: -------------------------------------------------------------------------------- 1 | # Problems 2 | 3 | ## Find Missing Element 4 | 5 | Given and array of numbers and a second array formed by shuffling the elements of the first array and deleting a random element, return the element that is missing from the second array. 6 | 7 | This method can be done in linear time - no nested loops! 8 | 9 | ## Anagram Detection 10 | 11 | Write a function that accepts two parameters, a parent and a child string. Determine how many times the child string - or an anagram of the child string - appears in the parent string. There is a solution which can be done in near instant time. 12 | 13 | ```js 14 | f('AdnBndAndBdaBn', 'dAn') 15 | // 4 ("Adn", "ndA", "dAn", "And") 16 | f('AbrAcadAbRa', 'cAda') 17 | // 2 (Acad, cadA) 18 | ``` 19 | 20 | [Source][anagram-source] 21 | 22 | 23 | ## I before E? 24 | 25 | You may have heard the mnemonic 'i before e, except after c'. Is this reasonable? 26 | 27 | We will be using the word list found [here][word-list]. 28 | 29 | In order to determine if this whole statement is plausible, we need to determine if the following two sub-statements are plausible: 30 | 1. 'i' before 'e' when not proceeded by 'c'. 31 | 2. 'e' before 'i' when proceeded by 'c'. 32 | 33 | If both of those statements are plausible, the whole statement us said to be plausible. 34 | 35 | 36 | We say that a statement is plausible if the number of words having the feature is more than two times the number of words having the opposite feature (where feature is 'ie' or 'ei' preceded or not by 'c' as appropriate). 37 | 38 | 39 | So our first statement needs to know the number of times we have 'ie' with no 'c' vs. 'ei' with no 'c'. The second statement will compare the number of 'cie' to the number of 'cei'. 40 | 41 | [Source][i-e-source] 42 | 43 | 44 | [anagram-source]: https://github.com/blakeembrey/code-problems/tree/master/problems/anagram-detection#anagram-detection 45 | 46 | [word-list]: http://wiki.puzzlers.org/pub/wordlists/unixdict.txt 47 | 48 | [i-e-source]: http://rosettacode.org/wiki/I_before_E_except_after_C#Python 49 | -------------------------------------------------------------------------------- /112718_binary/slides.md: -------------------------------------------------------------------------------- 1 | 2 | # Binary 3 | 4 | --- 5 | ## What is binary? 6 | 7 | A binary number system has numbers in base 2 - that is to say we only use the symbols `0` and `1` to represent a digit. The number system we are used to using is decimal, where we use the symbols `0` to `9` in any single digit. If we are counting in binary, just as in decimal, when we have gone through our symbols, we add a symbol to the left to represent the overflow. 8 | 9 | | decimal | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 10 | |---------|------|------|------|------|------|------|------|------|------| 11 | | binary | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 12 | 13 | ---- 14 | 15 | ## Thinking in binary 16 | 17 | One way to think about this is how we can represent a number as a sum of powers of 2. 18 | 19 | In decimal, we can easily use the number to describe how it can be shown as the sum of numbers modulo 10 multiplied by a power of 10. 20 | 21 | 22 | | 4 | 7 | 1 | 23 | |----------------|----------------|----------------| 24 | | 102 | 101 | 100 | 25 | | 100 | 10 | 1 | 26 | 27 | 28 | 29 | 471 = 4 \* 102 + 7 \* 101 + 1 \* 100 30 | 31 | = 4 * 100 + 7 * 10 + 1 * 1 32 | = 400 + 70 + 1 33 | 34 | 35 | 36 | 37 | Or 203: 38 | 39 | 203 = 2 \* 102 + 0 \* 101 + 3 \* 100 40 | 41 | = (2 * 100) + (0 * 10) + (3 * 1) 42 | = 200 + 00 + 3 43 | 44 | 45 | --- 46 | ## Converting Binary to Decimal 47 | 48 | 49 | So if we see the binary number 1010, we can convert this to decimal in a similar way. 50 | 51 | | 1 | 0 | 1 | 0 | 52 | |---------------|---------------|---------------|---------------| 53 | | 23 | 22 | 21 | 20 | 54 | | 8 | 4 | 2 | 1 | 55 | 56 | Here, we take this same breakdown of the binary `1010` and convert it to decimal. 57 | 58 | `1010` => (1 \* 23) + (0 \* 22) + (1 \* 21)+ (0 \* 20) 59 | 60 | => (1 * 8) + (0 * 4) + (1 * 2) + (0 * 1) 61 | 62 | => 8 + 0 + 2 + 0 63 | 64 | => 10 65 | 66 | --- 67 | 68 | ## Converting from Decimal to Binary 69 | 70 | So say we now want to convert the number 27 to binary. 71 | 72 | Let's think about this from largest digit to smallest digit. 73 | 74 | First, what is the largest power of two that is less than 27? 75 | 76 | | 20 | 21 | 22 | 23 | 24 | 25 | 77 | |---------------|---------------|---------------|---------------|---------------|---------------| 78 | | 1 | 2 | 4 | 8 | 16 | 32 | 79 | 80 | So 24 (16) is the largest power of 2 less than 27. So the leftmost digit will be in the 5th place (remember it's zero indexed!) 81 | 82 | So then we can take out 16 from 27. 83 | 84 | 27 = 16 + 11 85 | 86 | = 24 + 11 87 | 88 | We now repeat this process for 11. 89 | 90 | Does 11 contain 23 (8) i.e. is 11 >= 8? Yes. 91 | 92 | So we have a 1 in the 8s digit. 93 | 94 | 27 = 16 + 8 + 3 95 | 96 | = (1 \* 24) + (1 \* 23) + 3 97 | 98 | Does 3 contain 22 (4) i.e. is 3 >= 4? No. 99 | 100 | Then there is a 0 in the 4s digit. 101 | 102 | 27 = 16 + 8 + 0 + 3 103 | 104 | = (1 \* 24) + (1 \* 23) + (0 \* 22) + 3 105 | 106 | 107 | --- 108 | 109 | Does 3 contain 21 (2) i.e. is 3 >= 2? Yes. 110 | 111 | Then there is a 1 in the 2s digit. 112 | 113 | 27 = 16 + 8 + 0 + 2 + 1 114 | 115 | = (1 \* 24) + (1 \* 23) + (0 \* 22) + (1 \* 21) + 1 116 | 117 | 118 | Does 1 contain 20 (1) i.e. is 1 >= 1? Yes. 119 | 120 | Then there is a 1 in the 1s digit. 121 | 122 | 27 = 16 + 8 + 0 + 2 + 1 123 | 124 | = (1 \* 24) + (1 \* 23) + (0 \* 22) + (1 \* 21) + (1 \* 20) 125 | 126 | From this, we can pull out those leading 1s and 0s to show that 127 | 128 | ``` 129 | 27 => 11011 130 | ``` 131 | 132 | --- 133 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Algorithms Club 2 | All information is in the folder with the date and the topic. The slides can be viewed with remark.js or [here](https://remarkjs.com/remarkise). (CLI maybe to come.) 3 | Clone this repo and you can pull down any new information. 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "algorithms_club", 3 | "version": "1.0.0", 4 | "description": "Algorithms", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "author": "", 10 | "license": "ISC" 11 | } 12 | --------------------------------------------------------------------------------