3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in...

104
Introduction to Programming Problem Solving Problem Solving using Python Week 2 1 / 57

Transcript of 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in...

Page 1: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Introduction toProgramming

Problem SolvingProblem Solving using Python ­ Week 2

1 / 57

Page 2: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Homework and Last WeekQ&A

problemsolving.io     Introduction to Programming Problem Solving 2 / 57

Page 3: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Programming Problem Solving

problemsolving.io     Introduction to Programming Problem Solving 3 / 57

Page 4: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Programming Problem SolvingWhy?

problemsolving.io     Introduction to Programming Problem Solving 3 / 57

Page 5: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Programming Problem SolvingWhy?Programming requires multiple steps and it is challenging for novice programmers

problemsolving.io     Introduction to Programming Problem Solving 3 / 57

Page 6: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Programming Problem SolvingWhy?Programming requires multiple steps and it is challenging for novice programmers

What?

problemsolving.io     Introduction to Programming Problem Solving 3 / 57

Page 7: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Programming Problem SolvingWhy?Programming requires multiple steps and it is challenging for novice programmers

What?Learning to solve programming problems using Python

problemsolving.io     Introduction to Programming Problem Solving 3 / 57

Page 8: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Programming Problem SolvingWhy?Programming requires multiple steps and it is challenging for novice programmers

What?Learning to solve programming problems using Python

How?

problemsolving.io     Introduction to Programming Problem Solving 3 / 57

Page 9: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Programming Problem SolvingWhy?Programming requires multiple steps and it is challenging for novice programmers

What?Learning to solve programming problems using Python

How?With Programming Problem Solving model and tackling real problems

problemsolving.io     Introduction to Programming Problem Solving 3 / 57

Page 10: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Learning Objectives

problemsolving.io     Introduction to Programming Problem Solving 4 / 57

Page 11: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Learning ObjectivesAt the end of this lecture, you will...

problemsolving.io     Introduction to Programming Problem Solving 4 / 57

Page 12: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Learning ObjectivesAt the end of this lecture, you will...

1. describe the motivation for the Programming Problem Solving model, its structureand its phases.

problemsolving.io     Introduction to Programming Problem Solving 4 / 57

Page 13: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Learning ObjectivesAt the end of this lecture, you will...

1. describe the motivation for the Programming Problem Solving model, its structureand its phases.

2. apply the model to solve (simple) programming problems.

problemsolving.io     Introduction to Programming Problem Solving 4 / 57

Page 14: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

The Plan for Today1. Introducing the model2. Demonstrating the model on a tiny­very­easy programming problem3. Solving a real programming problem in groups using the model

problemsolving.io     Introduction to Programming Problem Solving 5 / 57

Page 15: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Programming Problem Solving Model

problemsolving.io     Introduction to Programming Problem Solving 6 / 57

Page 16: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Programming Problem Solving Model1. Reinterpret the Problem2. Design a Solution3. Code4. Test5. Debug6. Evaluate & Reflect

problemsolving.io     Introduction to Programming Problem Solving 6 / 57

Page 17: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Basic Example Write a program that will compute the area of a circle. Prompt the user to enter the radius and print a nice message back to the user with the answer.

problemsolving.io     Introduction to Programming Problem Solving 7 / 57

Page 18: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

1. Reinterpret the ProblemUnderstanding, Interpreting, and Clarifying the Problem

What should be the Output for every Input?

Strategy:

Reading three times, explaining to myself (aloud)

problemsolving.io     Introduction to Programming Problem Solving 8 / 57

Page 19: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

The criterion of success Write a program that will compute the area of a circle. Prompt the user to enter the radius and print a nice message back to the user with the answer.

Input / Output

Input: 1Output: 3.14

Input: 2.5Output: 19.625

Input: 0Output: 0.

problemsolving.io     Introduction to Programming Problem Solving 9 / 57

Page 20: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

2. Design a Solution

problemsolving.io     Introduction to Programming Problem Solving 10 / 57

Page 21: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

2. Design a SolutionBreaking the problem into sub­problems

problemsolving.io     Introduction to Programming Problem Solving 10 / 57

Page 22: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

2. Design a SolutionBreaking the problem into sub­problems

Strategies:

1. Working at least 3 examples by hand2. Writing the sub­problems as normal english as comments3. Drawing the sub­problems on a paper

problemsolving.io     Introduction to Programming Problem Solving 10 / 57

Page 23: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Writing the sub­problems as normal english as comments

# 1. get the radius from the user# 2. calc the area using the formula: pi * (r**2)# 3. print the area to the user

problemsolving.io     Introduction to Programming Problem Solving 11 / 57

Page 24: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

3. Code# 1. get the radius from the user# 2. calc the area using the formula: pi * (r**2)# 3. print the area to the user

problemsolving.io     Introduction to Programming Problem Solving 12 / 57

Page 25: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

3. Coderadius = float(input("Radius: "))# 2. calc the area using the formula: pi * (r**2)# 3. print the area to the user

problemsolving.io     Introduction to Programming Problem Solving 13 / 57

Page 26: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

3. Coderadius = float(input("Radius: "))area = 3.14 * (radius ** 2)# 3. print the area to the user

problemsolving.io     Introduction to Programming Problem Solving 14 / 57

Page 27: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

3. Coderadius = float(input("Radius: "))area = 3.14 * (radius ** 2)print("The area of the circle is", area)

problemsolving.io     Introduction to Programming Problem Solving 15 / 57

Page 28: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

3. Coderadius = float(input("Radius: "))area = 3.14 * (radius ** 2)print("The area of the circle is", area)

Radius: 2.5The area of the circle is 19.625

problemsolving.io     Introduction to Programming Problem Solving 16 / 57

Page 29: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

3. CodeStyle1. Meaningful Names2. PEP8

problemsolving.io     Introduction to Programming Problem Solving 17 / 57

Page 30: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

1. Meaningful NamesProgrammers generally choose names for their variables that are meaningful to thehuman readers of the program

... they help the programmer document, or remember, what the variable is used for.

problemsolving.io     Introduction to Programming Problem Solving 18 / 57

Page 31: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

1. Meaningful NamesProgrammers generally choose names for their variables that are meaningful to thehuman readers of the program

... they help the programmer document, or remember, what the variable is used for.

Strategy

Convert English comments (from the design phase) to function names

problemsolving.io     Introduction to Programming Problem Solving 18 / 57

Page 32: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

2. PEP 8 ­ the Style Guide for Python CodeOne of Guido’s key insights: Code is read much more often than it is written heguidelines provided here are intended

to improve the readability of codeto make it consistent across the wide spectrum of Python code

"Readability counts"Consistency

problemsolving.io     Introduction to Programming Problem Solving 19 / 57

Page 33: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

2. PEP 8 ­ the Style Guide for Python CodeOne of Guido’s key insights: Code is read much more often than it is written heguidelines provided here are intended

to improve the readability of codeto make it consistent across the wide spectrum of Python code

"Readability counts"Consistency

PEP8 ­ Variables Names

lower_case_with_underscores

PEP8 ­ Constants Names

UPPER_CASE_WITH_UNDERSCORESproblemsolving.io     Introduction to Programming Problem Solving 19 / 57

Page 34: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

StylePI = 3.14radius = float(input("Radius: "))area = PI * (radius ** 2)print("The area of the circle is", area)

problemsolving.io     Introduction to Programming Problem Solving 20 / 57

Page 35: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

3. Test

problemsolving.io     Introduction to Programming Problem Solving 21 / 57

Page 36: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

3. TestHeavily depends on the Problem Reinterpreting and Design Phases

problemsolving.io     Introduction to Programming Problem Solving 21 / 57

Page 37: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

3. TestFirst

Input: 1Output: 3.14

SecondInput: 2.5Output: 19.625

ThirdInput: 0Output: 0.0

radius = float(input("Radius: "))area = 3.14 * (radius ** 2)print("The area of the circle is", area)

problemsolving.io     Introduction to Programming Problem Solving 22 / 57

Page 38: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

4. Debug

problemsolving.io     Introduction to Programming Problem Solving 23 / 57

Page 39: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

4. DebugThis time we didn't find a bug in our program, because:

problemsolving.io     Introduction to Programming Problem Solving 23 / 57

Page 40: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

4. DebugThis time we didn't find a bug in our program, because:

1. The program was quite simple2. We have followed the model phases

problemsolving.io     Introduction to Programming Problem Solving 23 / 57

Page 41: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

4. DebugThis time we didn't find a bug in our program, because:

1. The program was quite simple2. We have followed the model phases

But don't expect it to be like that, as you have already encountered in the lab.

problemsolving.io     Introduction to Programming Problem Solving 23 / 57

Page 42: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

4. DebugThis time we didn't find a bug in our program, because:

1. The program was quite simple2. We have followed the model phases

But don't expect it to be like that, as you have already encountered in the lab.

EVERYONE has bugs in her/his code, no matter whether it is an expert or novice.

problemsolving.io     Introduction to Programming Problem Solving 23 / 57

Page 43: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

4. DebugThis time we didn't find a bug in our program, because:

1. The program was quite simple2. We have followed the model phases

But don't expect it to be like that, as you have already encountered in the lab.

EVERYONE has bugs in her/his code, no matter whether it is an expert or novice.

What makes the difference?

problemsolving.io     Introduction to Programming Problem Solving 23 / 57

Page 44: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

4. DebugThis time we didn't find a bug in our program, because:

1. The program was quite simple2. We have followed the model phases

But don't expect it to be like that, as you have already encountered in the lab.

EVERYONE has bugs in her/his code, no matter whether it is an expert or novice.

What makes the difference?

1. Following the model (Reinterpret the problem, Design a solution, Code, Test, ...)

problemsolving.io     Introduction to Programming Problem Solving 23 / 57

Page 45: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

4. DebugThis time we didn't find a bug in our program, because:

1. The program was quite simple2. We have followed the model phases

But don't expect it to be like that, as you have already encountered in the lab.

EVERYONE has bugs in her/his code, no matter whether it is an expert or novice.

What makes the difference?

1. Following the model (Reinterpret the problem, Design a solution, Code, Test, ...)

2. The way of debugging

problemsolving.io     Introduction to Programming Problem Solving 23 / 57

Page 46: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

6. Evaluate & Reflect

problemsolving.io     Introduction to Programming Problem Solving 24 / 57

Page 47: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Programming Problem Solving Model1. Reinterpret the Problem2. Design a Solution3. Code4. Test5. Debug6. Evaluate & Reflect

Questions?

problemsolving.io     Introduction to Programming Problem Solving 25 / 57

Page 48: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Activity ­ Counting Squares

problemsolving.io     Introduction to Programming Problem Solving 26 / 57

Page 49: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

1. Robot2. Grid of squares rooms3. Four directions: N, E, W, S

The Story

problemsolving.io     Introduction to Programming Problem Solving 27 / 57

Page 50: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Robot Programming LanguageCheck N (or Check E, or S or W)Go N (or Check E, or S or W)Basic math and remember numbers

problemsolving.io     Introduction to Programming Problem Solving 28 / 57

Page 51: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Your TaskTo program the robot to solve a specific taski.e. with its "language"

 

Using the Programming Problem Solving model

problemsolving.io     Introduction to Programming Problem Solving 29 / 57

Page 52: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

First Task ­ Simple SquareSuppose the robot is dropped into a square grid of rooms and starts in the north­westcorner of the grid. Come up with an program that the robot can use to figure out howmany rooms are in the grid.

Is there any size grid for which your program does not work? How about on a 1­by­1, or a 1000­by­1000?

In a 3­by­3 grid, how many times will the robot have to move through a door to run your program? Howabout a 4­by­4? An n­by­n?

Assume that we want to save on robot fuel. Can you make an program that uses fewer moves for thesame size grid?

Once you have an program you think is general (works for all size squares) andefficient (uses few moves), write it on a paper. Include a simple statement about howmany moves it takes, on an n­by­n grid the robot moves through 2n+5 doors orsomething like that.

problemsolving.io     Introduction to Programming Problem Solving 30 / 57

Page 53: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

First Task ­ Simple Square ­ Reflection1. What is not clear in the model?2. What is the input for testing? What is the output?3. Is it easy to see in the code your design (i.e. composition to sub­problems)?

problemsolving.io     Introduction to Programming Problem Solving 31 / 57

Page 54: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Second Task ­ Simple RectangularSuppose the robot is dropped into a rectangular (not necessarily square) grid ofrooms and starts in the north­west corner of the grid. Come up with an program thatthe robot can use to figure out how many rooms are in the grid.

Write on a paper a program for this case, including a description of the number ofmoves needed for an n­by­m grid.

problemsolving.io     Introduction to Programming Problem Solving 32 / 57

Page 55: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Second Task ­ Simple Rectangular ­Reflection1. What is not clear in the model?

2. Did your solution for the first task solve also the second task? Why yes/not?

3. Did you encounter bugs? How did you handle them?

problemsolving.io     Introduction to Programming Problem Solving 33 / 57

Page 56: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Third Task ­ RectangularSuppose the robot is dropped into a rectangular (not necessarily square) grid ofrooms and might start in any arbitrary room in the grid. Come up with an programthat the robot can use to figure out how many rooms are in the grid.

Write on a paper a program for this case, including a description of the number ofnumber of moves your robot will make for an n­by­m grid. Since this will probablydepend on the starting location of the robot, just tell us the biggest number you couldsee (assuming the robot started in the worst possible room).

problemsolving.io     Introduction to Programming Problem Solving 34 / 57

Page 57: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Third Task ­ Rectangular ­ Reflection1. How did you come up with the design?

2. Did your solution for the second task solved also the third task? Why yes/not?

3. Did you encounter bugs? How did you handle them?

problemsolving.io     Introduction to Programming Problem Solving 35 / 57

Page 58: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Wrap­up + Q&A 

Problem Solving using Python ­ Week 1Introduction to Programming Problem Solving

 

Credit for the Counting Squares activity:

Department of Computer Science at The University of Virginia

Luther Tychonievich, Mark Sherriff, and Ryan Layer

36 / 57

Page 59: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Text ReadabilityWorked Example

37 / 57

Page 60: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Text ReadabilityIntuitionWinnie the Pooh is easier to understand than a linguistics paper.

ApplicationsSelecting reading materials for school childrenNew York Times is designed to be read by ninth graders

problemsolving.io     Introduction to Programming Problem Solving 38 / 57

Page 61: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Demohttp://www.readabilityformulas.com/free­readability­formula­tests.php

problemsolving.io     Introduction to Programming Problem Solving 39 / 57

Page 62: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Published in Journal of AppliedPsychology in 1948 by RudolfFlesch

Flesch Reading Ease Score

problemsolving.io     Introduction to Programming Problem Solving 40 / 57

Page 63: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Published in Journal of AppliedPsychology in 1948 by RudolfFlesch

Flesch Reading Ease Score 

problemsolving.io     Introduction to Programming Problem Solving 40 / 57

Page 64: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Published in Journal of AppliedPsychology in 1948 by RudolfFlesch

\[ 206.835 ­ 84.6 \frac{total\, syllables}{total\, words } ­ 1.015 \frac{total\, words}{total\,sentences} \]

Flesch Reading Ease Score 

problemsolving.io     Introduction to Programming Problem Solving 40 / 57

Page 65: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Implementation: Where to Begin?... 

problemsolving.io     Introduction to Programming Problem Solving 41 / 57

Page 66: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Implementation: Where to Begin?... 

In:

Text: A sequence of sentences, each is a sequence of words.

problemsolving.io     Introduction to Programming Problem Solving 41 / 57

Page 67: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Implementation: Where to Begin?... 

In:

Text: A sequence of sentences, each is a sequence of words.

Out:

Score

problemsolving.io     Introduction to Programming Problem Solving 41 / 57

Page 68: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Now in Python?

problemsolving.io     Introduction to Programming Problem Solving 42 / 57

Page 69: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Now in Python?In:

[ ["Then", "he", "climbed", "a", "little", "further"], ["By" "that" "time" "he" "had" "thought" "of" "another" "song"]]

problemsolving.io     Introduction to Programming Problem Solving 42 / 57

Page 70: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Now in Python?In:

[ ["Then", "he", "climbed", "a", "little", "further"], ["By" "that" "time" "he" "had" "thought" "of" "another" "song"]]

Out:

88.72514705882355

problemsolving.io     Introduction to Programming Problem Solving 42 / 57

Page 71: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Let's Pretend!>>> text = [... ["Then", "he", "climbed", "a", "little", "further"],... ["By" "that" "time" "he" "had" "thought" "of" "another" "song"]... ]

>>> calculate_flesch_score(text)

problemsolving.io     Introduction to Programming Problem Solving 43 / 57

Page 72: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Let's Pretend!>>> text = [... ["Then", "he", "climbed", "a", "little", "further"],... ["By" "that" "time" "he" "had" "thought" "of" "another" "song"]... ]

>>> calculate_flesch_score(text)

What's the output?

problemsolving.io     Introduction to Programming Problem Solving 43 / 57

Page 73: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Let's Pretend!>>> text = [... ["Then", "he", "climbed", "a", "little", "further"],... ["By" "that" "time" "he" "had" "thought" "of" "another" "song"]... ]

>>> calculate_flesch_score(text)

What's the output?

88.72514705882355

problemsolving.io     Introduction to Programming Problem Solving 43 / 57

Page 74: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Fulfilling PromisesWe owe ourselves a function! Let's write one!

problemsolving.io     Introduction to Programming Problem Solving 44 / 57

Page 75: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Fulfilling PromisesWe owe ourselves a function! Let's write one!

def calculate_flesch_score(text): pass

problemsolving.io     Introduction to Programming Problem Solving 44 / 57

Page 76: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Fulfilling PromisesWe owe ourselves a function! Let's write one!

def calculate_flesch_score(text): pass

We're in luck, there's a formula!

def calculate_flesch_score(text): return ( 206.835 - 84.6 * n_syllables / n_words - 1.015 * n_words / n_sents )

problemsolving.io     Introduction to Programming Problem Solving 44 / 57

Page 77: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Not so Fast...Where do we get n_syllables, n_words, n_sents from?

problemsolving.io     Introduction to Programming Problem Solving 45 / 57

Page 78: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Not so Fast...Where do we get n_syllables, n_words, n_sents from?

I'm impatient to get my code to run, let's set them all to constants!

def calculate_flesch_score(text): n_syllables = 0 n_words = 15 n_sents = 2

return ( 206.835 - 84.6 * n_syllables / n_words - 1.015 * n_words / n_sents )

problemsolving.io     Introduction to Programming Problem Solving 45 / 57

Page 79: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Avoiding the Hard StuffHow do you count syllables in text?

problemsolving.io     Introduction to Programming Problem Solving 46 / 57

Page 80: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Avoiding the Hard StuffHow do you count syllables in text?

Do multiple vowels count as one syllable?

problemsolving.io     Introduction to Programming Problem Solving 46 / 57

Page 81: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Avoiding the Hard StuffHow do you count syllables in text?

Do multiple vowels count as one syllable?What about word­final "e", like in "plane"?

problemsolving.io     Introduction to Programming Problem Solving 46 / 57

Page 82: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Avoiding the Hard StuffHow do you count syllables in text?

Do multiple vowels count as one syllable?What about word­final "e", like in "plane"?What if a word contains no vowels at all, but still has syllables?

problemsolving.io     Introduction to Programming Problem Solving 46 / 57

Page 83: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Avoiding the Hard StuffHow do you count syllables in text?

Do multiple vowels count as one syllable?What about word­final "e", like in "plane"?What if a word contains no vowels at all, but still has syllables?

My brain says "This is starting to sound complicated"...

problemsolving.io     Introduction to Programming Problem Solving 46 / 57

Page 84: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Avoiding the Hard StuffHow do you count syllables in text?

Do multiple vowels count as one syllable?What about word­final "e", like in "plane"?What if a word contains no vowels at all, but still has syllables?

My brain says "This is starting to sound complicated"...

... so let's skip it for now! We'll come back to it later :)

problemsolving.io     Introduction to Programming Problem Solving 46 / 57

Page 85: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Low­Hanging FruitIn fact, I'm so lazy that I'll skip counting words too...

problemsolving.io     Introduction to Programming Problem Solving 47 / 57

Page 86: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Low­Hanging FruitIn fact, I'm so lazy that I'll skip counting words too...

How many sentences?

>>> text = [... ["Then", "he", "climbed", "a", "little", "further"],... ["By" "that" "time" "he" "had" "thought" "of" "another" "song"]... ]

problemsolving.io     Introduction to Programming Problem Solving 47 / 57

Page 87: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Low­Hanging FruitIn fact, I'm so lazy that I'll skip counting words too...

How many sentences?

>>> text = [... ["Then", "he", "climbed", "a", "little", "further"],... ["By" "that" "time" "he" "had" "thought" "of" "another" "song"]... ]

How do we ask this in Python?

problemsolving.io     Introduction to Programming Problem Solving 47 / 57

Page 88: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Low­Hanging FruitIn fact, I'm so lazy that I'll skip counting words too...

How many sentences?

>>> text = [... ["Then", "he", "climbed", "a", "little", "further"],... ["By" "that" "time" "he" "had" "thought" "of" "another" "song"]... ]

How do we ask this in Python?

>>> len(text)2

problemsolving.io     Introduction to Programming Problem Solving 47 / 57

Page 89: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Tweak the Function!def calculate_flesch_score(text): n_syllables = 0 n_words = 15 n_sents = len(text)

return ( 206.835 - 84.6 * n_syllables / n_words - 1.015 * n_words / n_sents )

problemsolving.io     Introduction to Programming Problem Solving 48 / 57

Page 90: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Counting WordsHow many words?

>>> text = [... ["Then", "he", "climbed", "a", "little", "further"],... ["By" "that" "time" "he" "had" "thought" "of" "another" "song"]... ]

problemsolving.io     Introduction to Programming Problem Solving 49 / 57

Page 91: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Counting WordsHow many words?

>>> text = [... ["Then", "he", "climbed", "a", "little", "further"],... ["By" "that" "time" "he" "had" "thought" "of" "another" "song"]... ]

In Python?

problemsolving.io     Introduction to Programming Problem Solving 49 / 57

Page 92: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Counting WordsHow many words?

>>> text = [... ["Then", "he", "climbed", "a", "little", "further"],... ["By" "that" "time" "he" "had" "thought" "of" "another" "song"]... ]

In Python?

n_words = 0for sentence in text: for word in sentence: n_words = n_words + 1

problemsolving.io     Introduction to Programming Problem Solving 49 / 57

Page 93: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Tweak the Function!def calculate_flesch_score(text): n_syllables = 0

n_words = 0 for sentence in text: for word in sentence: n_words = n_words + 1

n_sents = len(text) return ( 206.835 - 84.6 * n_syllables / n_words - 1.015 * n_words / n_sents )

problemsolving.io     Introduction to Programming Problem Solving 50 / 57

Page 94: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Finally Ready to Face The MonsterKinda... What if we only count the number of vowels in a word?

problemsolving.io     Introduction to Programming Problem Solving 51 / 57

Page 95: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Finally Ready to Face The MonsterKinda... What if we only count the number of vowels in a word?

Input ­ Output

"abracadabra" - 5

problemsolving.io     Introduction to Programming Problem Solving 51 / 57

Page 96: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Finally Ready to Face The MonsterKinda... What if we only count the number of vowels in a word?

Input ­ Output

"abracadabra" - 5

Let's Pretend Again

>>> count_syllables("abracadabra")5

problemsolving.io     Introduction to Programming Problem Solving 51 / 57

Page 97: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

No Formula... What to do?

problemsolving.io     Introduction to Programming Problem Solving 52 / 57

Page 98: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

No Formula... What to do?In Plain English

For every character in a word, if it is a vowel, add 1 to a counterReturn the counter

problemsolving.io     Introduction to Programming Problem Solving 52 / 57

Page 99: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

No Formula... What to do?In Plain English

For every character in a word, if it is a vowel, add 1 to a counterReturn the counter

In Python

def count_syllables(word): vowels = "aeiou" n_vowels = 0 for char in word: if char in vowels: n_vowels = n_vowels + 1 return n_vowels

problemsolving.io     Introduction to Programming Problem Solving 52 / 57

Page 100: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Tweak the Function!def calculate_flesch_score(text): n_syllables = 0 for sentence in text: for word in sentence: n_syllables = n_syllables + count_syllables(word)

n_words = 0 for sentence in text: for word in sentence: n_words = n_words + 1

n_sents = len(text)

return ( 206.835 - 84.6 * n_syllables / n_words - 1.015 * n_words / n_sents )

problemsolving.io     Introduction to Programming Problem Solving 53 / 57

Page 101: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Let's Clean Updef calculate_flesch_score(text): n_syllables = 0 n_words = 0 for sentence in text: for word in sentence: n_syllables = n_syllables + count_syllables(word) n_words = n_words + 1

n_sents = len(text)

return ( 206.835 - 84.6 * n_syllables / n_words - 1.015 * n_words / n_sents )

problemsolving.io     Introduction to Programming Problem Solving 54 / 57

Page 102: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Play Time!!Try out our new function with different inputs.

Because I'm dumb, I will keep track of what I have tried.

Acknowledgements

Michelle Craig, University of Toronto

problemsolving.io     Introduction to Programming Problem Solving 55 / 57

Page 103: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Programming Problem Solving Model1. Reinterpret the Problem2. Design a Solution3. Code4. Test5. Debug6. Evaluate & Reflect

problemsolving.io     Introduction to Programming Problem Solving 56 / 57

Page 104: 3UREOHP6ROYLQJ 3URJUDPPLQJ ,QWURGXFWLRQWR - Problem Solving using Python · EVERYONE has bugs in her/his code, no matter whether it is an expert or novice. What makes the difference?

Wrap­up + Q&A 

Problem Solving using Python ­ Week 1Introduction to Programming Problem Solving

57 / 57