Computational Problem Solving
Learn the four pillars of computational thinking and apply them to solve real problems with code!
The Four Pillars of Computational Thinking
Computational thinking is a way of approaching problems that makes them easier to solve -- with or without a computer. It has four key parts:
Decomposition
Breaking a big problem into smaller, manageable parts. Like building LEGO -- you don't try to build the whole thing at once.
Pattern Recognition
Finding similarities or patterns among problems. If you've solved something similar before, you can reuse that solution.
Abstraction
Focusing on what's important and ignoring unnecessary details. A map is an abstraction of a city -- it shows roads but not every tree.
Algorithm Design
Creating a step-by-step solution that can be followed to solve the problem every time.
Challenge 1: FizzBuzz
FizzBuzz is a classic programming challenge. The rules: count from 1 to a number, but replace multiples of 3 with "Fizz", multiples of 5 with "Buzz", and multiples of both with "FizzBuzz".
Decomposition
function fizzBuzz(n) {
let results = [];
for (let i = 1; i <= n; i++) {
if (i % 3 === 0 && i % 5 === 0) {
results.push("FizzBuzz");
} else if (i % 3 === 0) {
results.push("Fizz");
} else if (i % 5 === 0) {
results.push("Buzz");
} else {
results.push(i);
}
}
return results;
}
Challenge 2: Temperature Converter
Convert between Celsius and Fahrenheit using the formulas from Maths:
F = (C x 9/5) + 32 | C = (F - 32) x 5/9
function celsiusToFahrenheit(celsius) {
return (celsius * 9 / 5) + 32;
}
function fahrenheitToCelsius(fahrenheit) {
return (fahrenheit - 32) * 5 / 9;
}
Challenge 3: Simple Calculator
Build a calculator using decomposition: separate the problem into getting inputs, identifying the operation, and performing the calculation.
function calculate(num1, operator, num2) {
if (operator === "+") return num1 + num2;
if (operator === "-") return num1 - num2;
if (operator === "*") return num1 * num2;
if (operator === "/") {
if (num2 === 0) return "Error: division by zero!";
return num1 / num2;
}
return "Unknown operator";
}
Key Vocabulary
Decomposition
Breaking a complex problem into smaller, more manageable sub-problems.
Pattern Recognition
Identifying similarities between problems to reuse existing solutions.
Abstraction
Focusing on the essential details and removing unnecessary complexity.
Algorithm Design
Creating a step-by-step solution that can be implemented in code.
Modulo (%)
The remainder after division. 7 % 3 = 1 because 7 / 3 = 2 remainder 1.
Edge Case
An unusual input that might cause problems. E.g., dividing by zero.
Worked Examples
Decompose: "Make a program that checks if a password is strong"
Sub-problems:
- Check if it's at least 8 characters long
- Check if it contains a number
- Check if it contains an uppercase letter
- Combine checks and give feedback
Each sub-problem can be solved independently, then combined. That's decomposition!
Trace through FizzBuzz for numbers 13, 14, 15
13: 13 % 3 = 1 (not 0), 13 % 5 = 3 (not 0). Output: 13
14: 14 % 3 = 2 (not 0), 14 % 5 = 4 (not 0). Output: 14
15: 15 % 3 = 0 AND 15 % 5 = 0. Both conditions true! Output: "FizzBuzz"
Why does the calculator check for division by zero?
Answer: Division by zero is undefined in mathematics -- it has no answer.
In code, dividing by zero can cause errors or return Infinity.
A good programmer thinks about edge cases -- unusual inputs that might cause problems -- and handles them with an if statement. This is an example of abstraction: understanding which details matter.
Knowledge Check
Select the correct answer for each question.
Question 1
What is "decomposition" in computational thinking?
Question 2
What does FizzBuzz output for the number 9?
Question 3
What is 100 degrees Celsius in Fahrenheit? (F = C x 9/5 + 32)
Question 4
What is an "edge case"?
Question 5
What is 15 % 4 (15 modulo 4)?
Key Concepts Summary
- ●Computational thinking has four pillars: Decomposition, Pattern Recognition, Abstraction, and Algorithm Design.
- ●Decompose big problems into smaller parts before trying to code.
- ●The modulo operator (%) gives the remainder and is useful for checking divisibility.
- ●Good programmers think about edge cases and handle them gracefully.
- ●Coding + Maths = powerful tools for solving real-world problems: conversions, calculations, analysis, and automation.