Udacity CS101 Ch. 2.5 - How to Solve Problems
Goal: Draw general lessons about how to solve problems.
How do you get started? Whatās the first step?
Make sure you understand the problem. What does it mean to understand computational problems? All computational problems have inputs and a desired output in common.
All computational problems have these things in common:
The solution is to find a procedure that takes any input from the possible set and correctly produces the desired output.
So, the first step in understanding the problem is to know what the possible inputs are.
TheĀ āzerothā rule is: DONāT PANIC. (Too late! Coding kicks my butt!)
The first rule is: WHAT ARE THE INPUTS? What is the set of valid inputs? Also, to be a ādefensive programmerā, your procedure should check to make sure the inputs are valid. How are the inputs represented?
The second rule is: WHAT ARE THE OUTPUTS?
The third rule is: SOLVE THE PROBLEM! (Easy, right?) Understand the rules. Work out some examples by hand. Write some pseudocode. How would a human solve this?
The fourth rule is: FIND A SIMPLE MECHANICAL SOLUTION. Try to find a simple mechanical solution, but donāt worry about optimizing right away. How would a machine solve this? (You can useĀ ābrute forceā methods, initially, before optimizing.)
Hint: Break a complex problem into many smaller problems and write helper procedures to āeat the elephantā one bite at a time.
The fifth rule is: DEVELOP INCREMENTALLY AND SOLVE AS YOU GO. Use code stubs that you know are wrong, test, then change a bit and re-test.
This exercise killed me. The goal was to calculate the number of days between two dates, factoring in leap years and all that. It was really complicated, and we had to write a bunch ofĀ āhelper procedures/functionsā that figured things out before putting it all together. I got stuck on trying to solve this from a humanās perspective instead of a machineās. It was rough, but overall a good learning experience.