Preston Parry
Data Scientist, Machine Learning Engineer, addicted cyclist and rock climber
That's it.
That's it.
Sorry to disappoint- no nuclear physics here, folks.
All these are just a set of instructions to accomplish a specific task.
Reverse a string.
Define the problem:
1. Take in a string
2. Return a string
3. Returned string must be the reversed order of the original string
Reverse a string.
Constraints:
1. Are we allowed to use native JS functions?
2. Are we allowed to change it from a string into an array?
Reverse a string.
Exploring Patterns/Techniques:
1. Does JS have a native method that does this?
2. Can we try swapping items?
3. Could we just iterate through the string backwards?
4. What if we turned the string into an array of characters?
Reverse a string.
Simple plan:
1. Get input into a form we can iterate through backwards
2. iterate through it backwards
3. add each character to reversedString variable
4. return reversedString
Reverse a string.
Pseudocode
1. split the string into an array
2. iterate through that array backwards
3. push each item into a reversedArray
4. join reversedArray
5. return reversedArray
People seem to believe that they are either naturally good or bad at algorithms
-Really its just a skill you can develop
-And like any other skill you need a bag of tools/tricks
-Lets talk about some of these tools and tricks
1: Don't get bogged down in code!
2: Work with a friend
3: Get out your whiteboard and draw a picture
4: Don't be afraid of trying something out that doesn't work
5: Walk through a simple baby example
6: Change the algorithm to something easier to solve or something you know how to solve
-Its really easy to get lost in the trees and lose sight of the forest
-Going straight to JavaScript and trying to make everything work perfectly is not going to end well
-Making all the indices correct is frustrating
-Stay at a very high level with psuedocode
-Find a friend (or rubber duck) to bounce ideas off
-This will force you to verbalize what you are trying to do
-Often times you will realize the problem and come to a solution just by trying to describe it
-The vast majority of major algorithms are done via collaboration
-Don't be an island!
-Turns out thinking abstractly doesn't come naturally to humans (nor does statistics and big numbers)
-But we can take in an incredible amount of information via visuals
A Beautiful Tree!
-My favorite way to write algorithms is just trying anything I can think of and seeing what happens
-Worst Case Scenario: it didn't work and we just learnt more about the problem
-Best Case Scenario: it totally works
-Middle Case Scenario: it kind of worked but need to modify it
-Do step by step through the simplest case you can think of
-Pretend you are the computer and REALLY go through it
-There are no rules saying you have to solve the exact algorithm straight away
-Think of what this particular algorithm reminds you of and solve that!
-Example: You have an array and I ask you to return me the 7th largest item in the array
-Thought Process: Well if the array is already sorted I just give you array[array.length-7-1]...
Text
What does this mean for you?
Make the most basic possible version first.
Don't worry about edge cases.
Don't worry about handling everything.
Just get something out the door that is one step closer to what you're trying to accomplish.
If you're asked to put two dozen things on a board at specific positions:
Start by putting a single thing on the board.
Then put two dozen things anywhere on the board.
Then worry about how to position them all.
This makes your job much easier.
You don't need to solve the whole thing at once, just one tiny next portion of it.
Celebrate each step along the way.
Each one gets you a little bit closer to what you're trying to accomplish.
MVP lets you write code that isn't great. It's not your goal to write amazing code the first time through it!
In fact, if you write amazing code on your first try, you're probably doing it wrong and spending way too much time on that first attempt.
Leave yourself notes when you're writing crappy code. //TODO: this is inefficient. We can stop checking values if XYZ condition is false.
Once you've got a super basic version up and working, make rapid changes to improve it from there.
These exercises are a review from last week and are not necessarily tied to today's lecture material
https://github.com/TelegraphPrep/week2
By Preston Parry
Data Scientist, Machine Learning Engineer, addicted cyclist and rock climber