Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Thinking Like a Programmer


Published on

Problem that I see regularly in the first years I TA - they're too overwhelmed by syntax and don't manage to break the problem down into bits so that they can code it.

  • As a student athlete I’m always on the grind at basketball practice and I’ve been really short on time all through high school. I usually order a research paper or English essay here and there. The website is called ⇒ ⇐ and they really help me out, man. Don’t know where I’d be without it.
    Are you sure you want to  Yes  No
    Your message goes here

Thinking Like a Programmer

  1. 1. Thinking Like a Programmer Cate Huston
  2. 2. What Does This Mean? • For beginners, writing a (small) program should have two main components. • Understanding the problem and breaking it down into small steps. Write these down on paper. • Converting those steps into code with the correct syntax. These steps should be max 2-3 lines of code for each step. • These can be distinct - this can be helpful when you’re learning.
  3. 3. Syntax • Don’t worry so much about it! • It’s easy to look up later when you convert your logic to code. • Google! • Notes. • Syntax varies between languages, but the process of breaking the problem down is still the same.
  4. 4. Photo by Flikr User anikarenina Think of code constructs as building blocks that you can use to assemble your program. It’s like lego, you have to assemble something bit by bit.
  5. 5. Example 1: Persistence • The persistence of a number is how many times you have to replace the number by the sum or product of its digits until one reaches a single digit. • See: Persistence_of_a_number • Try and break this down into pieces. In this case, we will calculate the product. • Clue: we need two loops, one inside the other. • These are called nested loops.
  6. 6. Example 1: Persistence • Get in the initial number N • P = 0 // Persistence is zero • While N > 9 • tmp = 1 // 1 is neutral for multiplication • for each digit D in N • tmp = tmp * D • P++ // add one to P • N = tmp // update N with the new number • Output P
  7. 7. Converting to Code • Each of these lines of “pseudocode” require at most 2-3 lines of code in any language. • In many languages, they would require one line line of code. • If you don’t know how to convert anything, look it up on Google or in your notes.
  8. 8. Example 2: Guessing Game • Let’s make a guessing game. We’ll break it down into phases. • Phase 1: The computer makes a random number, and asks the user to guess it. It then confirms whether or not the user got the number right. • Phase 2: We give the user a limited number of guesses. • Phase 3: We allow the user to play again (if they want).
  9. 9. Example 2: Guessing Game: Phase 1 • Phase 1: Make a random number and ask the user to guess it, confirm if they got it right or not. • Maybe you don’t know how to make a random number, but just imagine you do - it’s easy to look up if you decide to code this. • Try it!
  10. 10. Example 2: Guessing Game: Phase 1 • R = random number 1-50 • N = guess from user • if (R == N) • Output “Well done!” • else • Output “You’re wrong!”
  11. 11. Example 2: Guessing Game: Phase 2 • Phase 2: Give the user a limited number of guesses • We’ll need a boolean variable to keep track if they’ve found it or not • We’ll need a “for” loop to keep track of how many guesses they’ve made and stop when they’ve used them all up. • Try it!
  12. 12. Example 2: Guessing Game: Phase 2 • R = random number 1-50 • correct = false // they haven’t guessed it yet • for i = 1 to 10 • // i is our counter, the user has 10 guesses • N = guess from user • if (R == N) • correct = true • exit loop • next i • if (correct) • Output “Well done!” • else • Output “Wrong”
  13. 13. Example 2: Guessing Game: Phase 3 • Phase 3: Allow the user to play again, if they want. • Use a while loop for this. • Our code from Phase 2 will stay the same, inside this loop. • Try it!
  14. 14. Example 2: Guessing Game: Phase 3 • continue = true // we’ll update this later • do • // code from Phase 2 goes here • Output “Play Again? Y/N” • response = input from user • if (response == “N”) • continue = false • while (continue)
  15. 15. Photo by Flikr User fd Try more examples, as many as you can find. Practice breaking down the problem down into “code-able” pieces. Don’t worry about syntax whilst you’re doing this.
  16. 16. @kittenthebad