unit 2: programming - david-abel.github.io · 1001… 0011… 0101 ... romeo, a young man with a...
TRANSCRIPT
1
February 12th, 2016
Dave Abel
Unit 2: Programming
Outline for Today
2
‣ Revisit clicker question from Wednesday
‣ Finish Scratch Overview (Lists, Randomness)
‣ Demystify how programming can reconfigure a computer
‣ Close the unit with a quick look at other languages
Clicker Question!
3
Q: Which loop will move the cat more steps?
[A] [B]
[C]
Clicker Answer!
4
Q: Which loop will move the cat more steps?
[A] [B]
[C]
Clicker Question!
5
Why not [C]
[A] [B]
[C]
Clicker Question!
6
Why not [C]
[C]Forever:
1) if counter is 10, move
2) add 1 to counter
Clicker Question!
7
Why not [C]
[C]Forever:
1) if counter is 10, move
2) add 1 to counter
Counter will only be set to 10 one time!
Things You’ll Do in Scratch
8
‣ Machine Learning: Writer a classifier, similar to how your email determines what is “Spam” and what is “Not Spam”!
‣ Programming: A simple game
‣ Vision + NLP: Model Roald Dahl’s style of writing!
‣ Recursion: Draw pretty pictures
‣ And more…
Things You’ll Do in Scratch
9
‣ Machine Learning: Writer a classifier, similar to how your email determines what is “Spam” and what is “Not Spam”!
‣ Programming: A simple game
‣ Vision + NLP: Model Roald Dahl’s style of writing!
‣ Recursion: Draw pretty pictures
‣ And more…
Block Types
10
Event/Trigger (e.g. when clicked)
Ending StatementBoolean ValueNumeric Value
Container (loops, if statements)
Statement (e.g. move cat)
String Value
More Scratch Things: Lists
11
Idea: a collection of things.
12
Idea: a collection of things.
Q: What’s the biggest number in “numbers”?Q: Is there a kiwi in the “basket”?
More Scratch Things: Lists
Lists
13
Index
Lists
14
Index Contents
Clicker Question:
16
Q: What will “numbers” look like after running this block?
Clicker Question:
17
Q: What will “numbers” look like after running this block?
[A] [B] [C]
Clicker Question:
18
Q: What will “numbers” look like after running this block?
[A] [B] [C] Hint: Think about the basket!
Clicker Question:
19
Q: What will “numbers” look like after running this block?
[A] [B] [C] Hint: Think about the basket!
Clicker Question:
20
Q: What will “numbers” look like after running this block?
Hint: Think about the basket!
Index Contents
Clicker Question:
21
Q: What will “numbers” look like after running this block?
Hint: Think about the basket!
Index
We will always grab thing from
lists by their index
Clicker Question!
23
Q: What does the mystery block do?
Clicker Question!
24
Q: What does the mystery block do?
(A) Counts the number of total items in the list “basket”
(B) Finds the longest fruit name in “basket”
(C) Looks through basket, counting the number of items with the name parameter
(D) Finds the the index of the item with the name of parameter
(E) Looks through basket to determine if an item with the name parameter is in it.
Clicker Answer!
25
Q: What does the mystery block do?
(A) Counts the number of total items in the list “basket”
(B) Finds the longest fruit name in “basket”
(C) Looks through basket, counting the number of items with the name parameter
(D) Finds the the index of the item with the name of parameter
(E) Looks through basket to determine if an item with the name parameter is in it.
Recall: Unit 2 Takeaway
26
Programming lets us reconfigure what a computer does!
Recall: Unit 2 Takeaway
27
Programming lets us reconfigure what a computer does!
Seen some examples:
Recall: Unit 2 Takeaway
28
Programming lets us reconfigure what a computer does!
So how does this work?
The Computer
29
‣ Internal states
‣ Complex logic relating bits
‣ Mechanism for setting bits (input)
‣ Mechanism for displaying bits (output)
Memory
CPU
Mouse
Keyboard
Monitor
30
‣ Internal states
‣ Complex logic relating bits
‣ Mechanism for setting bits (input)
‣ Mechanism for displaying bits (output)
Memory
CPU
Mouse
Keyboard
Monitor
The Computer
Memory
The Computer
….
Memory
The Computer
….100010101010010101000111111001010010100
32 bits32 bits32 bits32 bits32 bits
Memory
The Computer
….100010101010010101000111111001010010100
1001… 0011… 0101… 1110… 0001… 1001…
32 bits
32 bit operating system
64 bits64 bits64 bits64 bits64 bits
Memory
The Computer
….100010101010010101000111111001010010100
1001… 0011… 0101… 1110… 0001… 1001…
64 bits
64 bit operating system
Memory
The Computer
….100010101010010101000111111001010010100
1001… 0011… 0101… 1110… 0001… 1001…
10KB = 10 Kilobytes
Memory
The Computer
….100010101010010101000111111001010010100
1001… 0011… 0101… 1110… 0001… 1001…
10KB = 10 Kilobytes
1 Kilobyte = 1024 bytes
Memory
The Computer
….100010101010010101000111111001010010100
1001… 0011… 0101… 1110… 0001… 1001…
10KB = 10 Kilobytes
1 Kilobyte = 1024 bytes
(power of 2!!!)
A Quick TangentNumber of Bytes Name
2^10 =1,024 1 Kilobyte
2^20 = 1,048,576 1 Megabyte
2^30 = 1,073,741,824 1 Gigabyte
… …
2^100 = 1,267,650,600,228,229,401,496,703,205,376 ???
Approximate # atoms in the known universe: 10^80
Memory
The Computer
….100010101010010101000111111001010010100
1001… 0011… 0101… 1110… 0001… 1001…
10KB = 10 Kilobytes
1 Kilobyte = 1024 bytes
= 10 * 1024 bytes
Memory
The Computer
….100010101010010101000111111001010010100
1001… 0011… 0101… 1110… 0001… 1001…
= 10 * 1024 bytes
Memory
The Computer
….100010101010010101000111111001010010100
1001… 0011… 0101… 1110… 0001… 1001…
=
Memory
The Computer
….100010101010010101000111111001010010100
1001… 0011… 0101… 1110… 0001… 1001…
Memory
The Computer
….100010101010010101000111111001010010100
1001… 0011… 0101… 1110… 0001… 1001…
Memory
The Computer
….100010101010010101000111111001010010100
1001… 0011… … 1101…
Memory
The Computer
….100010101010010101000111111001010010100
1001… 0011… … 1101…
Commands themselves are also binary
Memory
The Computer
….100010101010010101000111111001010010100
1001… 0011… … 1101…
CPU
The Computer
1101…CPU
The Computer
1101…CPU
The Computer
1101…CPU
gets translated into Machine Code:
Memory
The Computer
….100010101010010101000111111001010010100
1001… 0011… … 1101…
CPU
Memory
The Computer
….100010101010010101000111111001010010100
1001… 0011… … 1101…
CPU
Memory
The Computer
….100010101010010101000111111001010010100
1001… 0011… … 1101…
CPU
Memory
The Computer
….100010101010010101000111111001010010100
1001… 0011… … 1101…
CPU
Recap!Becomes
machine code that carries out the “When flag clicked” block, in binary and
logic
Recap!
Becomes machine code that carries out
that block in binary and logic
Abstraction
Abstraction
Abstraction
x,y,size
Abstraction
x,y,size
Abstraction
x,y,size
“Natural Language Programming”
“Cat, draw a triangle” x,y,size
“Natural Language Programming”
“Cat, draw a triangle” x,y,size
Problem:
Other Languages
63
Other Languages: Prolog
64
Esoteric Languages: CHEF
65
Lobsters with Fruit and Nuts.
Ingredients.
72 g hazelnuts
101 eggs
108 g lobsters
…
Method.
Put lemon juice into the mixing bowl.
Put passion fruit into the mixing bowl.
Put durian into the mixing bowl.
Put lobsters into the mixing bowl.
...
66
Romeo, a young man with a remarkable patience.
Juliet, a likewise young woman of remarkable grace.
Ophelia, a remarkable woman much in dispute with Hamlet.
Hamlet, the flatterer of Andersen Insulting A/S.
…
Act I: Hamlet's insults and flattery.
Scene I: The insulting of Romeo.
[Enter Hamlet and Romeo]
Hamlet:
You lying stupid fatherless big smelly half-witted coward! You are as
stupid as the difference between a handsome rich brave hero and thyself!
Speak your mind!
…
Esoteric Languages: Shakespeare
Unit 2 Reflection
67
‣ Scratch!
- Drawing Squares
- Loops, Conditions
- Making Blocks
- Variables
- Lists
- Randomness
‣ How do programs reconfigure a computer?
68
Unit 2: Takeaway1. Physical gates are inflexible.
2. Programming lets us reconfigure what a computer does!
69
Up Next: Algorithms!‣ We’ve seen that programs can solve certain problems, e.g. “What’s the smallest number in my list?”
‣ What other problems are solvable with programs?
‣ What ones are easy? What ones are hard?
‣ The study of algorithms is the formal study of solving problems using computational tools!
‣ Central idea of computer science.