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
A Computer Is...
A "Turing Machine"
- A set of instructions
- A long strip of memory
my first algorithm
Input: Dirty Hair
- Lather
- Rinse
- 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
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
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
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
iteration
Repeating Yourself with Repetition
While, Do-While, For, Foreach
AKA Looping
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
- Creating a variable
- Reading a variable
- Printing output
- Writing to a variable
- Receiving input
- Subtraction between variables
Next time on dragonball z...
theme & variation
in your brain