1 of 142

Problem Solving and Algorithms

Day 3, Session 1

Welcome back!

2 of 142

We are here

3 of 142

Thank you!

useful ~ on my computer !

4 of 142

5 of 142

6 of 142

7 of 142

"Algorithm" ?

8 of 142

"Algorithm" ?

Algorithms are what computer scientists "do" ...

9 of 142

Algorithm... ?

An algorithm is a step-by-step process for solving problems

enough detail that a even a computer could do it!

not solving just one problem -- but a whole set of problems

10 of 142

Problem-solving processes

Here's a familiar problem.

How would you solve it?

11 of 142

A problem-solving processes

(1) experiment

    •  
    •  
    •  
    •  

(2) describe a plan

    •  
    •  
    •  
    •  

(3) test the plan (execute it)

    •  

(4) reflect/evaluate

    •  
    •  
    •  
    •  

A process is much more useful than one solution!  

solving one vs. solving many

12 of 142

A problem-solving process

(1) experiment

    • how to start?
    • what's the goal?
    • try a strategy... and/or another
    • try smaller pieces

(2) describe a plan

    • first step
    • next step...
    • are there choices? 
    • if so, how do you handle them

(3) test the plan (execute it)

    • go through the steps

(4) reflect / evaluate

    • does it work in this case?
    • would it always work?
    • what are hard or easy cases?
    • what are other applications?

13 of 142

A new problem-solving process

What is the path from the ring to the G?

"rook-jumping maze"

14 of 142

A problem-solving process

What is the path?

What is the process?

(1) experiment

    •  
    •  
    •  
    •  

(2) describe a plan

    •  
    •  
    •  
    •  

(3) test the plan (execute it)

    •  

(4) reflect/evaluate

    •  
    •  
    •  
    •  

15 of 142

Mazes ~ problem-solving

What is the path from start to finish?

Travel is only allowed along the arrows ~ no u-turns allowed!

16 of 142

No-left-turn maze

How about here?

Travel is only allowed going straight or taking right turns!

No left turns!

17 of 142

"Tilt" maze

Try to "roll" the circle to the star, but it can only roll...

Once you choose a direction, the circle must "roll" that way until it is stopped by a wall!

18 of 142

"Tilt" maze, part 2

Try to "roll" the circle to the star.

However, once you choose a direction, the circle must roll that way until it is stopped by a wall!

19 of 142

Rolling die maze

Try to "roll" a die from start to finish.

At each move, the die's upward side must match the maze square!

20 of 142

"Tilt" maze, part 3

Try to "roll" the circle to the star.

However, once you choose a direction, the circle must roll that way until it is stopped by a wall!

21 of 142

Algorithm... ?

An algorithm is a step-by-step process for solving problems

enough detail that a even a computer could do it!

not solving just one problem -- but a whole set of problems

22 of 142

Let's create an algorithm...

23 of 142

Let's create an algorithm...

(1) ________________

(2) ________________

Below, write an algorithm - a step-by-step process - for making a peanut butter and jelly sandwich.

How to make a PB&J sandwich...

You have...

  • a loaf of bread
  • a jar of PB
  • a jar of jelly
  • a knife

(3) ________________

(4) ________________

(5) ________________

(6) ________________

(7) ________________

(8) ________________

(9) ________________

(10) ________________

(11) ________________

(12) ________________

24 of 142

Thought experiment: three algorithms

How does Netflix predict how much you'll like a movie/show?

How does your brain know if it's seeing a face? How does it know how far away things are?

How do you put a set of objects in order (sort them)

42

17

25

25 of 142

Thought experiment: three algorithms

How do you put a set of objects in order (sort them)

42

17

25

This seems pretty easy!

26 of 142

Netflix Algorithm

predicting movie ratings

27 of 142

How does Netflix predict how much you'll like a movie/show?

Mary Elise's Ratings:

The Hobbit:

Dexter:

Zach's Ratings:

The Hobbit:

Dexter:

SUGGESTED FOR

VIEWING

28 of 142

Bob Bell, winner of the "Netflix prize"

Napoleon Dynamite =

Batman Begins =

The Netflix Prize

Finding Nemo =

Lord of the Rings =

(I don't know this guy)

??

??

??

??

Some films are difficult to predict… and others are easier!

29 of 142

Bob Bell, winner of the "Netflix prize"

Prediction errors...

(I don't know this guy)

Napoleon Dynamite =

Batman Begins =

Finding Nemo =

Lord of the Rings =

1.22

.75

.67

.42

These scores are the average error (in units of stars) for each film

30 of 142

How does your brain know if it's seeing a face?

And, how does it know how far away things are?

31 of 142

How does your brain know if it's seeing a face?

And, how does it know how far away things are?

We don't fully know!

but it is doing a lot of computing...

32 of 142

Problem-solving patterns

Brick-breaking challenge

The first thing computer scientists do to create an algorithm is explore -- and look for patterns...

33 of 142

Problem-solving patterns

Brick-breaking challenge

Q:  How many breaks are needed to fully separate these pieces?

34 of 142

Break it up...

# of breaks

# of pieces

1

2

3

12

B

N

4

35 of 142

Q:  How many handshakes do you need in order to greet everyone?

# in group

# of h.shakes needed

You are at a party with a total of 2, 3, 4, 10, or N people (including you)...

Q:  How many handshakes are needed so everyone greets everyone?

2

3

10

N

# in group

# of h.shakes needed

2

3

10

N

36 of 142

Take-home message

Understanding problems is the key to solving them -- and it's much more important than solving a particular one!

Computer Science is the science of understanding information-based problems, i.e., those based on data.

could these puzzles work with middle-schoolers?

37 of 142

Sorting and Searching

Day 3, Session 2

38 of 142

"Algorithm"

An algorithm is a process for solving problems.

Simply put, algorithms are what CS is about.

39 of 142

"Algorithm"

An algorithm is a process for solving problems.

The Nine Algorithms/Chapters:

(1) Ideas Computers Use Every Day

(2) Search Engine Indexing: Finding Needles in the World's Biggest Haystack

(3) PageRank: The algorithm that made Google

(4) Public Key Cryptography: Secrets on a Postcard

(5) Error-Correcting Codes: Mistakes That Fix Themselves

(6) Pattern Recognition: Learning from Experience

(7) Data Compression: Something for Nothing

(8) Databases: The Quest for Consistency

(9) Digital Signatures: Who Really Wrote This Software?

(10) What Is Computable?

(11) Conclusion: More Genius at Your Fingertips?

40 of 142

"Algorithm"

An algorithm is a process for solving problems.

The Nine Algorithms/Chapters:

(1) Ideas Computers Use Every Day

(2) Search Engine Indexing: Finding Needles in the World's Biggest Haystack

(3) PageRank: The algorithm that made Google

(4) Public Key Cryptography: Secrets on a Postcard

(5) Error-Correcting Codes: Mistakes That Fix Themselves

(6) Pattern Recognition: Learning from Experience

(7) Data Compression: Something for Nothing

(8) Databases: The Quest for Consistency

(9) Digital Signatures: Who Really Wrote This Software?

(10) What Is Computable?

(11) Conclusion: More Genius at Your Fingertips?

Searching

Sorting

41 of 142

Searching...

Webster's dictionary - in order

Wubster's dictionary - not in order!

Seeking "spam"

What algorithms can we use to find "spam"?

42 of 142

Linear Search (unsorted data)

  • Start at the first word.
  • "Is this the word we want?"
  • If it's not, go to the next word.

In the worst-case scenario, how many unsorted words would we have to look at before we get to the right one?

the algorithm

An example of a linear search in Scratch can be found here!

43 of 142

Binary Search (sorted data)

Can we find words faster if they're sorted?

If so, how?!?

Webster's Dictionary

44 of 142

Binary Search (sorted book)

  • Check the word in the middle.
  • If it's after our word, repeat with the 1st half.
  • If it's before our word, repeat with the 2nd half.

the algorithm

mixplate

Seeking "spam" ...

must be in here some-where

45 of 142

Binary Search (sorted book)

  • Check the word in the middle.
  • If it's after our word, repeat with the 1st half.
  • If it's before our word, repeat with the 2nd half.

mixplate

wasabi

here some-where

the algorithm

Seeking "spam" ...

46 of 142

Binary Search (sorted book)

  • Check the word in the middle.
  • If it's after our word, repeat with the 1st half.
  • If it's before our word, repeat with the 2nd half.

mixplate

wasabi

here some-where

soy sauce

the algorithm

Seeking "spam" ...

47 of 142

Binary Search (sorted book)

  • Check the word in the middle.
  • If it's after our word, repeat with the 1st half.
  • If it's before our word, repeat with the 2nd half.

If our book has 32 words in it, how many do we look at before we definitely find the right one? 64? 1,000,000?

swordfish

the algorithm

Seeking "spam" ...

An example of a binary search in Scratch can be found here!

48 of 142

Linear vs. Binary Search

With a large amount of sorted data, binary search is almost always more efficient.

But how do we get our data sorted?

49 of 142

Comparisons

The basic building-block of sorting is

comparing two items.

Consider these two numbers:

and

Which number is larger?

How can you tell?

110111001100100100010100111110101

110111001100100100010101111110101

50 of 142

Let's sort!

milk

chocolate

sugar

latte

caffeine

coffee

Create 6 cards with these words, one per note...

51 of 142

Let's sort!

Goal: Sort your items (in pairs)!

Note: Computers can compare only two items at a time! The students will help by saying what's smaller/larger...

Then: Sort again, counting the number of pairwise comparisons you make in order to ensure your items are sorted.

Challenge: describe a sorting algorithm that works for all possible cases!

Trial #1 tally

Trial #2 tally

52 of 142

Your sorting algorithms:

53 of 142

Lego Sorting

http://www.youtube.com/watch?v=MtcrEhrt_K0

Watch this sorting algorithm... can you describe how it works?

We've made this same method of sorting in Scratch

54 of 142

Linear vs. Binary Sorting!

How do they compare?

1) "Min Sort"

2) "Merge Sort"

How does this relate to dictionary-searching?

55 of 142

Sorting out sorting

Let's race three sorting algorithms

1) Act out minsort - count our comparisons: _____

2) Act out mergesort - count comparisons: _____

3) Outside, try a parallel sort - count: _____

Which one wins?

56 of 142

Min Sort

"Linear Sorting"

Idea: Search for the smallest element, then the second smallest, and so on...

More detail: Compare pairs of elements to find the smallest. Remove the smallest and place it at the end of the sorted list. Repeat until all elements are in the sorted list.

57 of 142

Merge Sort

"Binary Sorting"

Idea: Split repeatedly, then merge back!

More detail: Split in half repeatedly; when there are only two elements per list, they are easy to sort: just swap if needed!

Then, repeatedly merge two sorted lists by (1) comparing the first elements, and (2) placing the smaller element,

~ until all of the elements are sorted!

58 of 142

Parallel Sorting

Follow the arrows...

At each merge point,

the higher # goes right

the lower # goes left

Which group can sort themselves more quickly?

59 of 142

Is MergeSort for real? Let's see...

60 of 142

Is MergeSort for real? Let's see...

10 piles of 16

5 piles of 32

61 of 142

Is MergeSort for real? Let's see...

5 piles of 32

2 piles of 80

62 of 142

Is MergeSort for real? Let's see...

2 piles of 80

1 pile of 158

158?

63 of 142

Is MergeSort for real? Yes!

2 piles of 80

1 pile of 158

158?

Not sure who these people were....

64 of 142

Mergesort, in dance

look this up to see the many options...

http://www.youtube.com/watch?v=XaqR3G_NVoo

65 of 142

Sorting Activities from

66 of 142

The purpose of computing is insight, not numbers.

Algorithms are how we represent those problem-solving insights.

67 of 142

Lunch!

68 of 142

Scratch for ________

or

Python for ________

Day 3, Session 3

69 of 142

Scratch ~ tool for exploration...

(computation)

Scene ideas:

Shakepeare: Romeo + Juliet

Justin Carvalho: Farewell to Manzanar

Robin Herbig: Name animation

Drawing with Scratch

70 of 142

Another type of problem...

Recognize this game show?

71 of 142

Choose... Reveal... Switch or stay?

Let's Make a Deal...

Let's Make a Deal is in Scratch too!

72 of 142

Let's Make a bigger Deal...

A

B

C

D

E

F

G

1

2

3

4

5

6

7

8

9

10

73 of 142

Let's Make a bigger Deal...

A

B

C

D

E

F

G

1

2

3

4

5

6

7

8

9

10

74 of 142

Let's Make a bigger Deal...

A

B

C

D

E

F

G

1

2

3

4

5

6

7

8

9

10

75 of 142

Let's Make a bigger Deal...

A

B

C

D

E

F

G

1

2

3

4

5

6

7

8

9

10

76 of 142

Scratch ~ tool for exploration...

(computation)

Directing with Scratch

Scene ideas:

Shakepeare: Romeo + Juliet

Justin Carvalho: Farewell to Manzanar

Robin Herbig: Name animation

Drawing with Scratch

Binary activity

Flash cards / math practice

(computation)

Scene ideas:

Shakepeare: Romeo + Juliet

Justin Carvalho: Farewell to Manzanar

Robin Herbig: Name animation

Drawing with Scratch

77 of 142

An Infamous Equation

  • How long did it take you guys to learn the quadratic formula?

  • Did any of you learn songs?

  • Here's a way to use Scratch...

78 of 142

Algorithmic Scratch

(1) Create a Square

79 of 142

Algorithmic Scratch

(1) Create a Square

80 of 142

Algorithmic Scratch

(2) Create a Triangle

81 of 142

Algorithmic Scratch

(3) Create a Hexagon

82 of 142

Algorithmic Scratch

(4) Create a Square Algorithm

83 of 142

Algorithmic Scratch

(4) Create a Square Algorithm

84 of 142

Algorithmic Scratch

(5) Create a Square Algorithm

with side length as a variable

85 of 142

Algorithmic Scratch

(5) Create a Square Algorithm

with side length as a variable

86 of 142

Algorithmic Scratch

(7) Create a Wall with

Length and Width as

variables

87 of 142

Algorithmic Scratch

(7) Create a Wall with

Length and Width as

variables

88 of 142

Algorithmic Scratch

(8) Create Squares within

Squares

89 of 142

Algorithmic Scratch

(8) Create Squares within

Squares

90 of 142

Algorithmic Scratch

(9) Create Square-spiral,

a.k.a. "squiral"

91 of 142

Algorithmic Scratch

(9) Create Square-spiral,

a.k.a. "squiral"

92 of 142

Algorithmic Scratch

(10) Petal

93 of 142

Algorithmic Scratch

(10) Petal

94 of 142

Algorithmic Scratch

(11) Flower

95 of 142

Algorithmic Scratch

(11) Flower

96 of 142

(3pm today ~ Mike)

Planning for getting to Google...

97 of 142

Tomorrow

98 of 142

99 of 142

100 of 142

101 of 142

102 of 142

Rolling block maze

Try to "roll" a 2x1 block from start to finish.

As you solve the maze, the block may not overlap one of the "wall" squares

oos.moxiecode.com/flash8/rolling_blocks.html

www.hoodamath.com/games/bloxorz.html

Online versions:

103 of 142

Our game: Up, up, and away!

First, let's pair up -- so that each pair has both a teacher and an HMC student...

104 of 142

Our game: Up, up, and away!

(1) Choose a main character...

(something that you want to fly)

(2) Add (four) blocks like these so that you can move around with the arrows:

105 of 142

Our game: Up, up, and away!

(3) Make sure your "hero" can wrap around from right to left:

add other wrapping as you see fit!

106 of 142

Our game: Up, up, and away!

(4) Create a second sprite ~ the goal!

(something you want to reach)

(5) Create a third sprite ~ an "enemy"

(something to avoid)

(6) Have the enemy move continuously:

107 of 142

Our game: Up, up, and away!

(7) When the enemy hits the hero, teleport the hero back to the start:

it's good to start there at the beginning, too

108 of 142

Our game: Up, up, and away!

Now, add at least two of these features:

(a) Add a changing score variable

(b) Have the enemy move randomly

(c) Add more enemies

(d) Add "power-up" sprites that change your hero's size, score, or speed

(e) Add portals that teleport your hero randomly

(f) Have the game end when a timer runs out.

And... at least two of your own ideas... !

109 of 142

Game demonstrations...

...and code reviews

110 of 142

Our game: Fly, fly again...

Use these blocks to write a script that makes it so that if you press the UP arrow, the helicopter goes up, but if you DON'T press the up arrow, the helicopter goes down (like gravity!)

Make sure to save when done!

111 of 142

Butterfly Game

Create a two-toned background with a butterfly or a sprite of your choice "flying" in it. Use Scratch to change the costume of your sprite when it touches one of the colors.

112 of 142

Helicopter Game

Open Scratch and add the helicopter sprite.

It can be found in the "transportation" folder of sprites.

113 of 142

Helicopter Game

Change the background to have a start, an end, and some obstacles (color is key!).

Then, make sure that if the helicopter hits an obstacle, it resets back to the start.

You now have a playable game!

114 of 142

Scratch: Position

Day 3, Session 4

115 of 142

116 of 142

117 of 142

Want smooth motion?

118 of 142

Coordinate Practice

Review...

New...

119 of 142

120 of 142

121 of 142

122 of 142

Choose Your Challenges

Pick a challenge from the worksheets and implement it.

123 of 142

Scratch: Ifs and Elses

124 of 142

If Block

The "if" block can be found in the control tab.

The if block means, "IF this happens, THEN do this."

You can put blocks ON the if block and IN the if block.

125 of 142

Sensing Blocks

Most of the blocks that fit ON the if block are sensing blocks, which can be found in the sensing tab.

Different sensing blocks can detect if a key is pressed, if the sprite is touching something, or if the sprite moves to a certain position.

126 of 142

Forever If

Usually, you don't want to just run the if block once, you want it to always be running.

To make it run forever you can either put it in a forever block or use the forever if block.

127 of 142

Operators

In Scratch, operators let you connect other ideas together.

Many of these operators do things you have seen in math.

128 of 142

Common Operators

Some operators let you compare values (like numbers).

Other operators let you combine other blocks.

129 of 142

Will the sprite say "Hello!" when you run this script?

130 of 142

What will the variable "X" be after you run this script?

131 of 142

Predict and Test

To get comfortable with operators, partner with the person sitting next to you and go through the worksheet.

132 of 142

133 of 142

134 of 142

135 of 142

136 of 142

137 of 142

Broadcast Block

The broadcast block has a sprite send a signal.

You have to give each signal a name.

When another sprite hears that signal, it can start running a new script.

This can be helpful when you don't want one script to start until another script is finished.

138 of 142

Receiving a Broadcast Signal

To make a sprite listen for a signal, use the receive block, and choose which message to have it wait for.

139 of 142

140 of 142

Congratulations!

You've gone through an abridged version of the Scratch curriculum!

In the time remaining, team up with a partner and create a small project to show off your Scratch skills to everyone else.

141 of 142

Two envelopes...

Both envelopes have some money in them. You know that one envelope has double the amount in the other envelope.

Suppose you choose an envelope and you find M money in it. Say, $20.

Now you have the opportunity to trade. Should you?

142 of 142

More 1990's versions...