BrightPath
Back to Course
Year 10 Coding

Problem Solving with Python

Develop a systematic approach to breaking down problems, writing pseudocode, and translating solutions into working Python programs.

Breaking Down Problems

The most important skill in programming is not memorising syntax -- it is problem decomposition: breaking a large, complex problem into smaller, manageable parts. Each small part can then be solved independently and combined into a complete solution.

Follow this four-step process:

1
Understand -- Read the problem carefully. What are the inputs? What output is expected? What are the constraints?
2
Plan -- Write pseudocode or draw a flowchart before coding. Think about the algorithm step by step.
3
Code -- Translate your plan into Python, one small piece at a time. Test each piece as you go.
4
Test & Debug -- Run your code with different inputs, including edge cases. Fix any bugs you find.

From Pseudocode to Python

Pseudocode is an informal, plain-English description of what your program should do. It lets you focus on the logic without worrying about syntax. Once your pseudocode is clear, translating it to Python is straightforward.

Pseudocode:

SET total to 0
SET count to 0
FOR each number in the list:
    ADD number to total
    ADD 1 to count
SET average to total / count
PRINT average

Python:

total = 0
count = 0
for number in numbers:
    total += number
    count += 1
average = total / count
print(average)

Notice how each line of pseudocode maps almost directly to a line of Python. Good pseudocode is language-independent -- anyone should be able to understand it, even if they do not know Python.

A Complete Example: Password Validator

Problem: Write a program that checks if a password meets these rules: at least 8 characters long, contains at least one digit, and contains at least one uppercase letter.

PSEUDOCODE:
GET password from user
SET is_long to length of password >= 8
SET has_digit to False
SET has_upper to False
FOR each character in password:
    IF character is a digit: SET has_digit to True
    IF character is uppercase: SET has_upper to True
IF is_long AND has_digit AND has_upper:
    PRINT "Password is strong"
ELSE:
    PRINT which rules are not met
def validate_password(password):
    is_long = len(password) >= 8
    has_digit = False
    has_upper = False

    for char in password:
        if char.isdigit():
            has_digit = True
        if char.isupper():
            has_upper = True

    if is_long and has_digit and has_upper:
        return "Password is strong!"
    else:
        issues = []
        if not is_long:
            issues.append("at least 8 characters")
        if not has_digit:
            issues.append("at least one digit")
        if not has_upper:
            issues.append("at least one uppercase letter")
        return "Password needs: " + ", ".join(issues)

# Testing
print(validate_password("Hello123"))   # Strong!
print(validate_password("abc"))        # Needs: 8 chars, digit, uppercase

Key Vocabulary

Decomposition

Breaking a complex problem into smaller, more manageable sub-problems that can be solved independently.

Pseudocode

An informal, plain-English description of an algorithm's steps, written before actual code.

Algorithm

A step-by-step procedure for solving a problem or performing a computation.

Edge Case

An unusual or extreme input that might cause a program to fail, such as empty input, zero, or very large numbers.

Worked Examples

1

Find the largest number in a list without using the built-in max().

# Pseudocode: SET largest to first item, loop through rest, compare
def find_largest(numbers):
    largest = numbers[0]
    for num in numbers[1:]:
        if num > largest:
            largest = num
    return largest

print(find_largest([34, 67, 12, 89, 45]))  # Output: 89

Step 1: Assume the first element is the largest.

Step 2: Compare each remaining element; update if larger.

Step 3: After the loop, largest holds the answer.

2

Count how many vowels are in a given string.

def count_vowels(text):
    vowels = "aeiouAEIOU"
    count = 0
    for char in text:
        if char in vowels:
            count += 1
    return count

print(count_vowels("BrightPath AI Tutoring"))  # Output: 7

Step 1: Define a string containing all vowels (upper and lowercase).

Step 2: Loop through each character and check if it is in the vowels string.

Step 3: Increment the counter for each match and return the total.

3

Reverse a list without using the built-in reverse() or slicing.

def reverse_list(items):
    reversed_items = []
    for i in range(len(items) - 1, -1, -1):
        reversed_items.append(items[i])
    return reversed_items

print(reverse_list([1, 2, 3, 4, 5]))  # Output: [5, 4, 3, 2, 1]

Step 1: Create an empty list for the result.

Step 2: Loop from the last index down to 0 using range(len-1, -1, -1).

Step 3: Append each element to the new list in reverse order.

Knowledge Check

Select the correct answer for each question. Click "Check Answer" to see if you are right.

Question 1

What is the first step in the problem-solving process?

Question 2

What is pseudocode?

Question 3

What is an "edge case"?

Question 4

What does "decomposition" mean in programming?

Question 5

In the four-step process, what comes after "Plan"?

Key Concepts Summary

Year 10: File Handling Back to All Lessons