|
| 1 | +Algorithm is a process or set of rules to be followed in calculations or other problem-solving operations, especially by a computer. |
| 2 | + |
| 3 | +Almost everything you do in programming involves some kind of algorithm. It's the foundation of computer science and programming. |
| 4 | + |
| 5 | +### Problem Solving |
| 6 | + |
| 7 | +- Understand the problem |
| 8 | +- Explore concrete examples |
| 9 | +- Break it down |
| 10 | +- Solve/Simplify |
| 11 | +- Look back and refactor |
| 12 | + |
| 13 | +### Understand the problem |
| 14 | + |
| 15 | +- Can I restate the problem in my own words? |
| 16 | +- What are the inputs that go into the problem? |
| 17 | +- What are the outputs that should come from the solution to the problem? |
| 18 | +- Can the outputs be determined from the inputs? In other words, do I have enough information to solve the problem? (You may not be able to answer this question until you set about solving the problem. That's okay; it's still worth considering the question at this early stage.) |
| 19 | +- How should I label the important pieces of data that are a part of the problem? |
| 20 | + |
| 21 | +Example: |
| 22 | + |
| 23 | +Write a function which takes two numbers and returns their sum. |
| 24 | + |
| 25 | +1. Can I restate the problem in my own words? |
| 26 | + - Implement addition |
| 27 | +2. What are the inputs that go into the problem? |
| 28 | + - Integers? |
| 29 | + - Floats? |
| 30 | + - What about string for large numbers? |
| 31 | +3. What are the outputs that should come from the solution to the problem? |
| 32 | + - Integers? |
| 33 | + - Floats? |
| 34 | + - String? |
| 35 | +4. Can the outputs be determined from the inputs? In other words, do I have enough information to solve the problem? |
| 36 | + - Yes |
| 37 | + |
| 38 | +### Explore concrete examples |
| 39 | + |
| 40 | +Coming up with examples can help you understand the problem better. |
| 41 | + |
| 42 | +Examples also provide sanity checks that your eventual solution works how it should. |
| 43 | + |
| 44 | +- Start with simple examples |
| 45 | +- Progress to more complex examples |
| 46 | +- Explore examples with empty inputs |
| 47 | +- Explore examples with invalid inputs |
| 48 | + |
| 49 | +Example: |
| 50 | + |
| 51 | +Write a function which takes in a string and returns counts of each character in the string. |
| 52 | + |
| 53 | +```json |
| 54 | +charCount("aaaa"); // {a: 4} |
| 55 | + |
| 56 | +charCount("hello"); // {h: 1, e: 1, l: 2, o: 1} |
| 57 | +``` |
| 58 | + |
| 59 | +Do we need to worry about case sensitivity? |
| 60 | +Do we need to worry about spaces? |
| 61 | +Do we need to worry about characters that are not letters? |
| 62 | +Do we need to worry about characters that are not present in the input? |
| 63 | + |
| 64 | +### Break it down |
| 65 | + |
| 66 | +Explicitly write out the steps you need to take. |
| 67 | + |
| 68 | +This forces you to think about the code you'll write before you write it, and helps you catch any conceptual issues or misunderstandings before you dive in. |
| 69 | + |
| 70 | +Example: |
| 71 | + |
| 72 | +Write a function which takes in a string and returns counts of each character in the string. |
| 73 | + |
| 74 | +```json |
| 75 | +function charCount(str) { |
| 76 | + // make object to return at end |
| 77 | + // loop over string, for each character |
| 78 | + // if the char is a number/letter AND is a key in object, add one to count |
| 79 | + // if the char is a number/letter AND not in object, add it to object and set value to 1 |
| 80 | + // if the char is something else (space, period, etc.) don't do anything |
| 81 | + // return object at end |
| 82 | +} |
| 83 | + |
| 84 | +charCount("hello"); |
| 85 | +/* |
| 86 | +{ |
| 87 | + h: 1, |
| 88 | + e: 1, |
| 89 | + l: 2, |
| 90 | + o: 1 |
| 91 | +} |
| 92 | +*/ |
| 93 | + |
| 94 | +charCount("Your PIN number is 1234"); |
| 95 | +/* |
| 96 | +{ |
| 97 | + 1: 1, |
| 98 | + 2: 1, |
| 99 | + 3: 1, |
| 100 | + 4: 1, |
| 101 | + b: 1, |
| 102 | + e: 1, |
| 103 | + i: 2, |
| 104 | + m: 1, |
| 105 | + n: 2, |
| 106 | + o: 1, |
| 107 | + p: 1, |
| 108 | + r: 2, |
| 109 | + s: 1, |
| 110 | + u: 2, |
| 111 | + y: 1 |
| 112 | +} |
| 113 | +*/ |
| 114 | +``` |
| 115 | + |
| 116 | +### Solve/Simplify |
| 117 | + |
| 118 | +If you can't solve the problem, solve a simpler problem. |
| 119 | + |
| 120 | +- Find the core difficulty in what you're trying to do |
| 121 | +- Temporarily ignore that difficulty |
| 122 | +- Write a simplified solution |
| 123 | +- Then incorporate that difficulty back in |
| 124 | + |
| 125 | +Example: |
| 126 | + |
| 127 | +Write a function which takes in a string and returns counts of each character in the string. |
| 128 | + |
| 129 | +```json |
| 130 | +function charCount(str) { |
| 131 | + const obj = {}; |
| 132 | + for (let char of str) { |
| 133 | + char = char.toLowerCase(); |
| 134 | + if (/[a-z0-9]/.test(char)) { |
| 135 | + obj[char] = ++obj[char] || 1; |
| 136 | + } |
| 137 | + } |
| 138 | + return obj; |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +### Look back and refactor |
| 143 | + |
| 144 | +- Can you check the result? |
| 145 | +- Can you derive the result differently? |
| 146 | +- Can you understand it at a glance? |
| 147 | +- Can you use the result or method for some other problem? |
| 148 | +- Can you improve the performance of your solution? |
| 149 | +- Can you think of other ways to refactor? |
| 150 | +- How have other people solved this problem? |
| 151 | + |
| 152 | +Example: |
| 153 | + |
| 154 | +Write a function which takes in a string and returns counts of each character in the string. |
| 155 | + |
| 156 | +```json |
| 157 | +function charCount(str) { |
| 158 | + const obj = {}; |
| 159 | + for (let char of str) { |
| 160 | + char = char.toLowerCase(); |
| 161 | + if (/[a-z0-9]/.test(char)) { |
| 162 | + obj[char] = ++obj[char] || 1; |
| 163 | + } |
| 164 | + } |
| 165 | + return obj; |
| 166 | +} |
| 167 | +``` |
0 commit comments