Algorithms Practice

You already know how to do this

Creative Commons License

Today's Lesson

  • We're going to practice writing out the pseudocode for a bunch of algorithms today to get in some practice.
  • This is a group effort! Each person will contribute the next line of the pseudocode. 

How to Write an Algorithm 

  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!
  • To review what we covered yesterday, here are the 5 steps to write an algorithm:

#1: Directions From Bart

  • Problem: Write an algorithm for getting to Telegraph Academy from BART. Be super specific- this should have > 20 steps. 

Questions To Ask:

  • Which BART station?
  • How much of a rush am I in?
  • Do I have a bike?
  • Do I have a car?
  • Can I take a bus?
  • What time of day is it?
    • If I'm biking, do I have lights?
  • How much stuff am I carrying?

#1: Directions From Bart

  • Problem: Write an algorithm for getting to Telegraph Academy from BART
  • Some example first steps:
    • Exit BART train
    • Walk North along the platform to the northernmost stairs, since this is closest to the street exit we want.
    • Walk up stairs
    • If you have enough money on Clipper Card, pay with Clipper Card
      • If not, reload Clipper Card
    • Find stairs for Central Ave West
    • Walk up stairs for Central Ave West
    • ...

#2: Fizz Buzz

  • Problem: log the numbers 1 - 100, replacing any number divisible by 3 with the word "fizz", and any number divisible by 5 with the word "buzz". Numbers divisible by both become "fizz buzz".

#3: Getting Ready For Class

  • Problem: What do you do to get ready for class between when you walk in the door and when class kicks off?

#1: Getting Ready For Class

  • Simple Sample Algorithm: 

1. Sit in closest open seat to the door

2. Since I brought no bag, only my computer, place computer on desk

3. Meditate and think of nothing/talk to no one until class starts

#3: Getting Ready For Class

  • More Complex Sample Algorithm: 

1. Scan the room for which friends are here so far.

2. Sit next to friend I haven't already paired with.

3. Chit-chat.

4. Invite friend to grab snacks in the kitchen.

5. Walk to kitchen.

6. Decide which snacks to eat (this has it's own algorithm)

  A. Snap pictures of all snacks

  B. Send pictures to personal nutritionist

  C. Follow two of the nutritionist's three recommendations

  D. Choose ranch dressing if eating more than 10 vegetables.

...

#4: Making Breakfast

  • This problem is intentionally open-ended, as many interview questions are. Remember our 5 rules for creating an algorithm and start there:
  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!

Key Takeaways

  • Keep in mind those 5 guidelines for creating an algorithm
  • Practice, practice, practice!
  • You've totally got this :)

Today's Exercises!

  • Do problems 1-5 on CoderByte. 
  • If you don't have an account, you'll need to create one (they're free!).
  • http://coderbyte.com/CodingArea/Challenges/


Things to keep in mind:

  • Try to come up with your own solution from scratch before looking up a solution
  • For syntax, feel free to use references like devdocs.io or use google
  • Support your pair in her/his learning experience
  • Have fun!!

Algorithms Practice

By telegraphprep

Algorithms Practice

Here are a bunch of examples we can work through to become more familiar with thinking algorithmically

  • 1,018