Upload
aleksandarpmau
View
242
Download
0
Embed Size (px)
Citation preview
8/19/2019 Solving Programming Problems
1/14
Solving ProgrammingProblems
A Seven-step Approach
8/19/2019 Solving Programming Problems
2/14
Solving Programming Problem
• Last lessons: Green screen example
• Specific problem that we wanted to solve
• Worked through step-by-step
• Problem statement! working code
• Now: A closer look at solving problems
• Seven step approach
8/19/2019 Solving Programming Problems
3/14
Solving Programming Problem
I want to be able totake a foreground
image and abackground image
and…
var fgImage = new SimpleImage("drewRobert.png");
var bgImage = new SimpleImage("dinos.png");var output = new SimpleImage(fgImage.getWidth(), fgImage.getHeight());for (var pixel of image.toArray()) { x = pixel.getX(); y = pixel.getY(); r = pixel.getRed(); g = pixel.getGreen();
b = pixel.getBlue(); if (g > r + b) { var pixel2 = bgImage.getPixel(x, y); output.setPixelAt(x, y, pixel2); } else {
output.setPixelAt(x, y, pixel); } }
print(output);
Problem Statement Working Code
Big Leap:
Break It Down
8/19/2019 Solving Programming Problems
4/14
Seven Steps
Work
Example ByHand
Write DownWhat You Did Find Patterns Check By
Hand
Translate ToCode
Run TestCases
Debug FailedTest Cases
1 2 3 4
5 6 7
8/19/2019 Solving Programming Problems
5/14
Step 1
• Solve small instance by hand
• Unclear problem?
• Need domain knowledge?
Work
Example ByHand
Write DownWhat You Did Find Patterns
Check ByHand
Translate ToCode
Run TestCases
Debug FailedTest Cases
1 2 3 4
5 6 7
8/19/2019 Solving Programming Problems
6/14
Step 2
• Write down exact steps
• Just that instance
• Tricky: do without thinking
Work
Example ByHand
Write DownWhat You Did Find Patterns
Check ByHand
Translate ToCode
Run TestCases
Debug FailedTest Cases
1 2 3 4
5 6 7
8/19/2019 Solving Programming Problems
7/14
Step 3
• Algorithm for any instance
• Find patterns
• Repetition, conditions, values
Work
Example ByHand
Write DownWhat You Did Find Patterns
Check ByHand
Translate ToCode
Run TestCases
Debug FailedTest Cases
1 2 3 4
5 6 7
8/19/2019 Solving Programming Problems
8/14
Step 3
• Difficulties?
• Try Step 1 + 2 again
• Different inputs
Work
Example ByHand
Write DownWhat You Did Find Patterns
Check ByHand
Translate ToCode
Run TestCases
Debug FailedTest Cases
1 2 3 4
5 6 7
8/19/2019 Solving Programming Problems
9/14
Step 4
• Incorrect pattern? Find now
• Check with different inputs
Work
Example ByHand
Write DownWhat You Did Find Patterns
Check By
Hand
Translate ToCode
Run TestCases
Debug FailedTest Cases
1 2 3 4
5 6 7
8/19/2019 Solving Programming Problems
10/14
Step 5
• Translate algorithm to code
• Programming language
Work
Example ByHand
Write DownWhat You Did Find Patterns
Check By
Hand
Translate To
Code
Run TestCases
Debug FailedTest Cases
1 2 3 4
5 6 7
8/19/2019 Solving Programming Problems
11/14
Step 6
• Run test cases on code
• Execute program
• Check answer
Work
Example ByHand
Write DownWhat You Did Find Patterns
Check By
Hand
Translate ToCode
Run TestCases
Debug FailedTest Cases
1 2 3 4
5 6 7
8/19/2019 Solving Programming Problems
12/14
Step 7
• Test failed? Debug
• Use scientific method
• Understand problem + fix
Work
Example ByHand
Write DownWhat You Did Find Patterns
Check By
Hand
Translate ToCode
Run TestCases
Debug FailedTest Cases
1 2 3 4
5 6 7
8/19/2019 Solving Programming Problems
13/14
Step 7
• Algorithmic problem?
• Implementation problem?
Work
Example ByHand
Write DownWhat You Did Find Patterns
Check By
Hand
Translate ToCode
Run TestCases
Debug FailedTest Cases
1 2 3 4
5 6 7
8/19/2019 Solving Programming Problems
14/14
Seven Steps
Work
Example ByHand
Write DownWhat You Did Find Patterns
Check By
Hand
Translate ToCode
Run TestCases
Debug FailedTest Cases
1 2 3 4
5 6 7