AQA 7517, section 4.4.1.1
Computer science is about solving problems. Before you write any code, you need to be able to work out how to solve a problem, and then check that your solution really works. The problems here are logic problems: ones that can be solved by careful, step-by-step reasoning.
A method for solving problems
- Understand the problem. What are you given? What do you have to find? What are the rules or constraints?
- Break it down. Split it into smaller steps or sub-problems.
- Plan a solution. Work out a sequence of steps, perhaps on paper first.
- Try it on examples. Follow your steps by hand with simple test cases.
- Check it. Test it against all the rules, and include awkward cases such as the smallest, largest and empty cases.
- Improve it if it fails, or if a simpler way exists.
Developing a solution
Useful techniques include:
- Trial and error with a systematic order, so that no case is missed.
- Elimination: rule out options that break a rule.
- Working backwards from the answer.
- Drawing a diagram or table to keep track of the situation.
- Using logic, such as Boolean expressions and truth tables.
Checking a solution
A solution is only correct if it works in every case. To check one:
- follow it step by step, writing down the state after each step (a trace);
- test it with normal, boundary and invalid data;
- check that no rule is broken at any point, not just at the end.
Worked example: the river crossing
Problem: a farmer needs to take a wolf, a goat and a cabbage across a river in a boat that holds the farmer and only one item. If left alone together, the wolf eats the goat, and the goat eats the cabbage. How can everything cross safely?
Understand: the dangerous pairs are wolf and goat, and goat and cabbage. The farmer must never leave one of those pairs without him on the same bank.
Develop a solution: the goat is in both dangerous pairs, so it must go first.
- Take the goat across.
- Return alone.
- Take the wolf across.
- Bring the goat back with him.
- Take the cabbage across.
- Return alone.
- Take the goat across.
Check it by tracing every step and looking at what is left on the bank without the farmer:
| Step | Left bank | Right bank | Farmer is on | Unattended pair safe? |
|---|---|---|---|---|
| Start | wolf, goat, cabbage | (empty) | left | Yes, he is there |
| 1 | wolf, cabbage | goat | right | Left: wolf and cabbage, safe |
| 2 | wolf, cabbage | goat | left | Right: goat alone, safe |
| 3 | cabbage | goat, wolf | right | Left: cabbage alone, safe |
| 4 | cabbage, goat | wolf | left | Right: wolf alone, safe |
| 5 | goat | wolf, cabbage | right | Left: goat alone, safe |
| 6 | goat | wolf, cabbage | left | Right: wolf and cabbage, safe |
| 7 | (empty) | wolf, goat, cabbage | right | All across |
The key move is step 4: bringing the goat back avoids leaving the goat with the wolf or the cabbage. The check shows that at every step, whatever is left without the farmer is a safe combination.
Exam tip: when you're asked to check a solution, show a trace and state what you tested. "I followed each step and checked after each one that no unsafe pair was left together" is more convincing than "it works".
Check yourself
Give three techniques for developing a solution to a logic problem
Any three of: trying cases in a systematic order; eliminating options that break the rules; working backwards from the answer; drawing a diagram or table; using Boolean logic or truth tables.
Why is it not enough to test a solution with one example?
A solution must work in every case. One example might pass by luck, and the problems usually show up with boundary or unusual data.
What kinds of test data should you use when checking a solution?
Normal data, boundary data (the smallest and largest valid values), and invalid data.