Edexcel International Advanced Level Computer Science, point 17.2.1
In the Unit 4 practical exam you are given a problem you haven't seen. Computational thinking is how you turn it into a working program. The four techniques are covered in Topic 10 (10.1.1 to 10.1.4). Here you apply them together.
The techniques
| Technique | Question | In code |
|---|---|---|
| Decomposition (10.1.2) | What are the smaller parts? | Separate subprograms, comment-first outline |
| Abstraction (10.1.1) | What detail can I ignore or hide? | Subprograms and classes hide the how; choose the data to keep |
| Pattern recognition (10.1.3) | Have I solved something like this? | Reuse a known algorithm or structure |
| Generalisation (10.1.4) | How can it work for similar cases? | Parameters instead of fixed values |
| Algorithmic thinking | What exact steps give the answer? | Plan the order, trace it, handle special cases |
A method for a new problem
- Read the problem twice. Underline the inputs, outputs, rules and constraints.
- Decide the data (types and structures, 17.1.2 and 17.1.3).
- Decompose: list the steps as comments (10.1.2).
- Recognise patterns: is any part a search, a sort, a count, a running total, a validation loop?
- Write the subprograms, with parameters and return values (generalised).
- Test each part with normal, boundary and erroneous data (9.3.1).
- Assemble and test the whole.
- Review: does it meet every requirement?
Worked example
Problem: A school wants a program that stores the names and test scores of students in a dictionary, and displays the students in order of score (highest first) along with the class average. The scores must be between 0 and 100. The program should allow new students to be added, and a student to be looked up by name.
1. Understand
| Item | Detail |
|---|---|
| Inputs | Name and score (for adding), a name (for look-up) |
| Outputs | A ranked list, the average, a student's score |
| Rules | Score 0 to 100, names are unique |
| Data | A dictionary {name: score} (look up by name) |
2. Decompose (comment-first)
# 1. add_student(scores, name, score): validate, then store
# 2. find_student(scores, name): return the score or None
# 3. average(scores): the mean, safely for an empty dictionary
# 4. ranking(scores): list of (name, score), highest first
# 5. main loop: a menu that calls the above
3. Recognise patterns and generalise
- "Look up by key" is a dictionary (pattern).
- Ranking is a sort by a key (use
sortedwith akeyfunction). - The average is a running total divided by a count (guard against an empty set).
- Validation is a reusable subprogram with limits as parameters (generalisation).
4. Write the subprograms
def valid_score(value, low=0, high=100):
return isinstance(value, int) and low <= value <= high
def add_student(scores, name, score):
if not valid_score(score):
return False
scores[name] = score
return True
def find_student(scores, name):
return scores.get(name) # None if not found
def average(scores):
if not scores: # guard clause
return 0
return sum(scores.values()) / len(scores)
def ranking(scores):
return sorted(scores.items(), key=lambda p: p[1], reverse=True)
5. Test
scores = {}
add_student(scores, "Amy", 72)
add_student(scores, "Raj", 91)
add_student(scores, "Sam", 58)
print(add_student(scores, "Li", 150)) # False: invalid
print(ranking(scores)) # [('Raj', 91), ('Amy', 72), ('Sam', 58)]
print(average(scores)) # 73.66666666666667
# 72 None
print(find_student(scores, "Amy"), find_student(scores, "Zed"))
print(average({})) # 0: the empty case
6. Review against the requirements
| Requirement | Met by |
|---|---|
| Store names and scores in a dictionary | scores |
| Validate 0 to 100 | valid_score (boundary tests: -1, 0, 100, 101) |
| Add students | add_student |
| Look up by name | find_student |
| Order by score, highest first | ranking |
| Class average | average (safe when empty) |
Each technique was used: decomposition (five subprograms), abstraction (the main program doesn't need to know how ranking works), pattern recognition (dictionary look-up, sort by key, running total), generalisation (valid_score has limit parameters, and could check any range), and algorithmic thinking (the empty case and the invalid input were planned for).
Exam tip: plan before you type: a few comment lines per problem save time. Write small subprograms and test each one, instead of one long program. Think about the unusual cases (empty data, invalid input, boundaries). In the practical exam keep your code readable: names, comments, structure (Topic 19).
Check yourself
State the four computational thinking techniques
Abstraction, decomposition, pattern recognition and generalisation (and algorithmic thinking).
What is the first step in tackling an unfamiliar problem?
Read it carefully to identify the inputs, outputs, rules and constraints.
How does generalisation show up in code?
Fixed values become parameters, so the same subprogram works for many cases.
Why test each subprogram separately?
Faults are easier to find and fix when each part is checked on its own before they are combined.