How To 

Think 

Like a 

Programmer

Algorithms & Languages
By @ErikRalston

Previously on How to think like a programmer...


Introduction

Knowledge is a coral reef not a beach

Traffic Feelings vs. Flow


Data structures

Strategies for Organizing Information in Programs


 




Overview


Computer? Program? Algorithm?

Thinking Through a Problem

Code = Story




What is a 
computer?



A Computer Is...


A "Turing Machine"

  1. A set of instructions
  2. A long strip of memory


what is an algorithm?


my first algorithm


Input: Dirty Hair
  1. Lather
  2. Rinse
  3. Repeat
Output: Clean Hair

an algorithm is...


An input
A defined set of steps
An output

Every line, every function, every program
is an algorithm of algorithms

Design an Algorithm

Define the outcome

Qualitative goal, probably only understandable by human


Mario Needs to Get Everyone Across

define the constraints

Limitations the system must obey


Yoshi Will Eat Koopa, Koopa Will Hurt Peach
Yoshi, Koopa, and Peach May Not Jump
Mario can only carry one person at a time

define the capabilities

The operations the system can initiate


Mario may carry one person

define the capabilities

The operations the system can initiate


Mario may carry one person to the right
Mario may carry one person to the left

constraints vs capabilities

Strategies are Built From Perspective




what is a 

program?



Execute 

a program













Rob & The Dragon













2 & 3













4 & 5













6 & 7













8 & 9













10 & 11













12 & 13













14 & 15













16 & 17



what is code?

code is...a story!


Code explains something 

In such detail, any idiot can do it!
(And other humans can understand it)

Requires creativity, syntax, and strategy

Separated into pieces to enable reuse & maintenance

Lines = sentences


An operation, or small set of operations, in the current context

Actions like...

Declare a new variable
Read a variable
Write a variable
Apply an operand between variables
Call a function

Blocks = paragraphs


Function

A reusable set of instructions

Conditionals

Block modifier that may cause a block to be skipped

Loops

Block modifier that may cause a block to repeat

write a 

program


Goldilocks and the Three Bears

top-down design


Start from a goal and break into steps

Hierarchical means of documenting steps

Dig down until the computer can understand you

#1 Cognitive tool for mapping human thought to computers


What is a programming language?


// Languages are the same right?
var thing = "language?";
console.log(thing);

HAI 1.2  
BTW Languages are the same? I HAS A thing ITZ "language?" VISIBLE thing KTHXBYE

PARSING

Pulling apart text with an algorithm to determine purpose


syntax

Rules governing the valid symbols 
and their valid combinations in a language
(The rules for parsing)





Compiling a hard language (English)


compilED

Parsing all of the code into a new type of executable file
C, C++, C#, Java

InterpretED

Parsing code, usually just-in-time, to manipulate a runtime
JavaScript, Python, Ruby

What is a programming language?


A tool for expression

A structure for telling a story

With its own unique habits (Idioms)

Crafted by a human 
because every other language just wasn't good enough









control flow








Logic

Predicates

Statements that evaluate to true or false


boolean 

True & False


Boolean logic

Relationships between boolean values

Not
And, Or
XOR, NAND

build a 

truth

table


Neither cars nor trucks

No cars and no trucks

Conditionals


Back in my day we had "GoTo"

If
If-Else
Switch

BE a conditional

iteration

Repeating Yourself with Repetition

While, Do-While, For, Foreach

AKA Looping


Be a loop

Recursion


To understand recursion
One must first understand recursion

A function calling itself

Better than iteration for traversing trees, etc

iteration vs recursion


Evaluating algorithms


Space
(Memory)

Time
(Operations per element)

Memory Access Constraints
(Random vs. Sequential)

design a 

programming 

language

Language must include

The "Cashier" programming language
For basic point-of-sale change making

  1. Creating a variable
  2. Reading a variable
  3. Printing output
  4. Writing to a variable
  5. Receiving input
  6. Subtraction between variables

Next time on dragonball z...


theme & variation

in your brain

How to Think Like a Programmer: Algorithms

By Erik Ralston

How to Think Like a Programmer: Algorithms

Part 3, how to conceptualize instructions into steps of a programming by defining problems correctly and thinking top-down

  • 1,940