Algorithms!

Algorithms!

A fancier word for $h!t you're already doing

Today's Tour

  • Definition of an algorithm
  • Ways to design an algorithm
  • Example designing an algorithm
  • Good techniques for designing/solving algorithms
  • MVP

Algorithm- what is it?

  • A set of instructions to accomplish a task

Algorithm- what is it?

  • A set of instructions to accomplish a task

That's it.

Algorithm- what is it?

  • A set of instructions to accomplish a task

That's it.

Sorry to disappoint- no nuclear physics here, folks. 

Examples of Algorithms

  • A recipe
  • Instructions on how to drive somewhere
  • How to get ready in the morning
  • An approach to dealing with fights
  • Instructions for the baby sitter
  • IKEA $^!*%# instructions

 

All these are just a set of instructions to accomplish a specific task. 

Algorithm Design

  1. Define the rules of the problem
  2. Determine any constraints
  3. Explore the problem and discover techniques or patterns that might be useful
  4. Generate a simple plan that should solve the problem
  5. Turn that plan into steps- put your pseudocode skills to use!

Algorithm Design Examples

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

Algorithm Design Examples

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?

Algorithm Design Examples

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?

Algorithm Design Examples

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

Algorithm Design Examples

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

Good Techniques

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 

 

Good Tools/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

1. Dont Get Bogged Down In Code!

-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 

2. Work with a Friend

-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!

3. Draw!

-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!

4. Making 'Mistakes'

-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

 5. Walk Through a Simple Example 

-Do step by step through the simplest case you can think of

 

-Pretend you are the computer and REALLY go through it

 

 

 

 6. Solve an Easier Problem 

-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]...

An Algorithm that Intimidated Me

Text

MVP- Minimum Viable Product

  • Get something out the door that works to the smallest possible degree.
  • Rapidly iterate from there. 

MVP

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. 

MVP

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. 

MVP

This makes your job much easier. 

 

You don't need to solve the whole thing at once, just one tiny next portion of it. 

MVP

Celebrate each step along the way.

 

Each one gets you a little bit closer to what you're trying to accomplish. 

MVP

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. 

MVP- iterate rapidly

Once you've got a super basic version up and working, make rapid changes to improve it from there. 

Today's Exercises

These exercises are a review from last week and are not necessarily tied to today's lecture material 

https://github.com/TelegraphPrep/week2

Josh's Copy of Algorithms!

By Joshua Newman

Josh's Copy of Algorithms!

  • 1,155