unit 2: programming - david-abel.github.io · 1001… 0011… 0101 ... romeo, a young man with a...

69
1 February 12th, 2016 Dave Abel Unit 2: Programming

Upload: phamliem

Post on 07-Jul-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

1

February 12th, 2016

Dave Abel

Unit 2: Programming

Page 2: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

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

Page 3: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Clicker Question!

3

Q: Which loop will move the cat more steps?

[A] [B]

[C]

Page 4: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Clicker Answer!

4

Q: Which loop will move the cat more steps?

[A] [B]

[C]

Page 5: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Clicker Question!

5

Why not [C]

[A] [B]

[C]

Page 6: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Clicker Question!

6

Why not [C]

[C]Forever:

1) if counter is 10, move

2) add 1 to counter

Page 7: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

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!

Page 8: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

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…

Page 9: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

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…

Page 10: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Block Types

10

Event/Trigger (e.g. when clicked)

Ending StatementBoolean ValueNumeric Value

Container (loops, if statements)

Statement (e.g. move cat)

String Value

Page 11: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

More Scratch Things: Lists

11

Idea: a collection of things.

Page 12: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

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

Page 13: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Lists

13

Index

Page 14: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Lists

14

Index Contents

Page 15: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Lists

15

Index Contents

Let’s take a look!

Page 16: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Clicker Question:

16

Q: What will “numbers” look like after running this block?

Page 17: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Clicker Question:

17

Q: What will “numbers” look like after running this block?

[A] [B] [C]

Page 18: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Clicker Question:

18

Q: What will “numbers” look like after running this block?

[A] [B] [C] Hint: Think about the basket!

Page 19: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Clicker Question:

19

Q: What will “numbers” look like after running this block?

[A] [B] [C] Hint: Think about the basket!

Page 20: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Clicker Question:

20

Q: What will “numbers” look like after running this block?

Hint: Think about the basket!

Index Contents

Page 21: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

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

Page 22: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Coin Flipping!

22

And more… Let’s take a look!

Page 23: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Clicker Question!

23

Q: What does the mystery block do?

Page 24: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

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.

Page 25: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

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.

Page 26: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Recall: Unit 2 Takeaway

26

Programming lets us reconfigure what a computer does!

Page 27: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Recall: Unit 2 Takeaway

27

Programming lets us reconfigure what a computer does!

Seen some examples:

Page 28: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Recall: Unit 2 Takeaway

28

Programming lets us reconfigure what a computer does!

So how does this work?

Page 29: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

The Computer

29

‣ Internal states

‣ Complex logic relating bits

‣ Mechanism for setting bits (input)

‣ Mechanism for displaying bits (output)

Memory

CPU

Mouse

Keyboard

Monitor

Page 30: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

30

‣ Internal states

‣ Complex logic relating bits

‣ Mechanism for setting bits (input)

‣ Mechanism for displaying bits (output)

Memory

CPU

Mouse

Keyboard

Monitor

The Computer

Page 31: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Memory

The Computer

….

Page 32: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Memory

The Computer

….100010101010010101000111111001010010100

Page 33: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

32 bits32 bits32 bits32 bits32 bits

Memory

The Computer

….100010101010010101000111111001010010100

1001… 0011… 0101… 1110… 0001… 1001…

32 bits

32 bit operating system

Page 34: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

64 bits64 bits64 bits64 bits64 bits

Memory

The Computer

….100010101010010101000111111001010010100

1001… 0011… 0101… 1110… 0001… 1001…

64 bits

64 bit operating system

Page 35: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Memory

The Computer

….100010101010010101000111111001010010100

1001… 0011… 0101… 1110… 0001… 1001…

10KB = 10 Kilobytes

Page 36: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Memory

The Computer

….100010101010010101000111111001010010100

1001… 0011… 0101… 1110… 0001… 1001…

10KB = 10 Kilobytes

1 Kilobyte = 1024 bytes

Page 37: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Memory

The Computer

….100010101010010101000111111001010010100

1001… 0011… 0101… 1110… 0001… 1001…

10KB = 10 Kilobytes

1 Kilobyte = 1024 bytes

(power of 2!!!)

Page 38: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

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

Page 39: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Memory

The Computer

….100010101010010101000111111001010010100

1001… 0011… 0101… 1110… 0001… 1001…

10KB = 10 Kilobytes

1 Kilobyte = 1024 bytes

= 10 * 1024 bytes

Page 40: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Memory

The Computer

….100010101010010101000111111001010010100

1001… 0011… 0101… 1110… 0001… 1001…

= 10 * 1024 bytes

Page 41: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Memory

The Computer

….100010101010010101000111111001010010100

1001… 0011… 0101… 1110… 0001… 1001…

=

Page 42: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Memory

The Computer

….100010101010010101000111111001010010100

1001… 0011… 0101… 1110… 0001… 1001…

Page 43: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Memory

The Computer

….100010101010010101000111111001010010100

1001… 0011… 0101… 1110… 0001… 1001…

Page 44: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Memory

The Computer

….100010101010010101000111111001010010100

1001… 0011… … 1101…

Page 45: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Memory

The Computer

….100010101010010101000111111001010010100

1001… 0011… … 1101…

Commands themselves are also binary

Page 46: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Memory

The Computer

….100010101010010101000111111001010010100

1001… 0011… … 1101…

CPU

Page 47: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

The Computer

1101…CPU

Page 48: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

The Computer

1101…CPU

Page 49: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

The Computer

1101…CPU

gets translated into Machine Code:

Page 50: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Memory

The Computer

….100010101010010101000111111001010010100

1001… 0011… … 1101…

CPU

Page 51: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Memory

The Computer

….100010101010010101000111111001010010100

1001… 0011… … 1101…

CPU

Page 52: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Memory

The Computer

….100010101010010101000111111001010010100

1001… 0011… … 1101…

CPU

Page 53: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Memory

The Computer

….100010101010010101000111111001010010100

1001… 0011… … 1101…

CPU

Page 54: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Recap!Becomes

machine code that carries out the “When flag clicked” block, in binary and

logic

Page 55: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Recap!

Becomes machine code that carries out

that block in binary and logic

Page 56: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Abstraction

Page 57: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Abstraction

Page 58: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Abstraction

x,y,size

Page 59: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Abstraction

x,y,size

Page 60: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Abstraction

x,y,size

Page 61: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

“Natural Language Programming”

“Cat, draw a triangle” x,y,size

Page 62: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

“Natural Language Programming”

“Cat, draw a triangle” x,y,size

Problem:

Page 63: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Other Languages

63

Page 64: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Other Languages: Prolog

64

Page 65: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

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.

...

Page 66: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

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

Page 67: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

Unit 2 Reflection

67

‣ Scratch!

- Drawing Squares

- Loops, Conditions

- Making Blocks

- Variables

- Lists

- Randomness

‣ How do programs reconfigure a computer?

Page 68: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

68

Unit 2: Takeaway1. Physical gates are inflexible.

2. Programming lets us reconfigure what a computer does!

Page 69: Unit 2: Programming - david-abel.github.io · 1001… 0011… 0101 ... Romeo, a young man with a remarkable patience. ... ‣ We’ve seen that programs can solve certain problems,

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.