DECOMPOSTION
information and requirements. But the key to successfully tackling these challenges is to use decomposition and abstraction—two fundamental problem-solving strategies in programming. Let’s break down how you can apply these concepts to solve the given coding challenge.
On this page we will use the challange from the Feb 2007 22 paper from Cambridge Examinations as can be seen below:
On this page we will use the challange from the Feb 2007 22 paper from Cambridge Examinations as can be seen below:
An experiment has taken place which measures the reaction speeds of students. Each student is
aged from 12 to 16, and belongs to a school house (Saturn or Mars). A program is required to store
the age, school house and reaction time of each student in the school. There are 650 students in the
school. The program should be able to output the reaction times of different student groups.
Write and test a program for the school.
• Your program must include appropriate prompts for the entry of data.
• Error messages and other output need to be set out clearly.
• All variables, constants and other identifiers must have meaningful names.
You will need to complete these three tasks. Each task must be fully tested.
TASK 1 – Set up arrays and store records
Set up one-dimensional arrays for the whole school to store the following data:
• The age of each student in whole years
• The school house of each student
• The reaction time of each student
Input and store the records for an appropriate sample of students. Inputs must be validated on
entry and any invalid inputs rejected.
TASK 2 – Output school house based statistics
Using your sample, calculate and output the average reaction times for students in Saturn and
students in Mars.
TASK 3 – Output statistics based on user input
Extend your program to prompt users to input a specific age and school house. Using only records
that match the criteria input, the program should identify, calculate and output:
• The average reaction time
• The slowest reaction time
The output should include a suitable message for each of the reaction times identified.
aged from 12 to 16, and belongs to a school house (Saturn or Mars). A program is required to store
the age, school house and reaction time of each student in the school. There are 650 students in the
school. The program should be able to output the reaction times of different student groups.
Write and test a program for the school.
• Your program must include appropriate prompts for the entry of data.
• Error messages and other output need to be set out clearly.
• All variables, constants and other identifiers must have meaningful names.
You will need to complete these three tasks. Each task must be fully tested.
TASK 1 – Set up arrays and store records
Set up one-dimensional arrays for the whole school to store the following data:
• The age of each student in whole years
• The school house of each student
• The reaction time of each student
Input and store the records for an appropriate sample of students. Inputs must be validated on
entry and any invalid inputs rejected.
TASK 2 – Output school house based statistics
Using your sample, calculate and output the average reaction times for students in Saturn and
students in Mars.
TASK 3 – Output statistics based on user input
Extend your program to prompt users to input a specific age and school house. Using only records
that match the criteria input, the program should identify, calculate and output:
• The average reaction time
• The slowest reaction time
The output should include a suitable message for each of the reaction times identified.
DECOMPOSITION | BREAKING IT DOWN INTO SMALLER PARTS
Decomposition involves breaking a big problem into smaller, manageable pieces, and solving each piece individually. Let's decompose the given challenge into smaller tasks:
Step 1: Understand the TasksThe challenge asks you to write and test a program for the school with three main tasks:
TASK 1: Set up arrays and store records
TASK 2: Output school house-based statistics
TASK 3: Output statistics based on user input
Step 1: Understand the TasksThe challenge asks you to write and test a program for the school with three main tasks:
- Set up arrays and store records.
- Output school house-based statistics.
- Output statistics based on user input.
TASK 1: Set up arrays and store records
- Create one-dimensional arrays for storing:
- The ages of students.
- The school houses of students.
- The reaction times of students.
- Input and store data for a sample group of students.
- Use input validation to ensure ages are positive numbers, school houses are valid (e.g., "Mars" or "Saturn"), and reaction times are reasonable values.
- Create the arrays (lists).
- Write code to take inputs (e.g., using a loop).
- Write validation code to ensure the data entered is correct.
TASK 2: Output school house-based statistics
- Filter data to focus only on students in the Saturn and Mars houses.
- Calculate and output the average reaction times for students in each of these houses.
- Write a loop that filters the students based on their school house.
- Write functions that calculate average reaction times for the filtered data.
TASK 3: Output statistics based on user input
- Prompt the user for a specific age and school house.
- Filter records based on this user input.
- Calculate the average and slowest reaction times and display them.
- Write code to prompt the user for an age and school house.
- Write a loop to find all students that match the input.
- Write code to calculate the average and slowest reaction times for the filtered data.
ABSTRACTION | SIMPLIFYING COMPLEXITY
Abstraction is about focusing on the important details and ignoring everything else until it’s time to deal with it. In other words, abstracting helps you to think about what each piece of code needs to do without worrying about how it will do it until you’re ready.
For instance:
For instance:
- When you think about storing student data, imagine a list to hold ages, another list to hold school houses, and another list for reaction times. Don’t worry too much about the specific data yet—just focus on the idea of a list holding information.
- When considering input validation, think conceptually about rejecting "bad data." You don't need to get into the specifics of which inputs are invalid until later.
- Focus on Task 1: Create Arrays and Store Records
- Create an empty list for each piece of data: ages, school houses, reaction times.
- Write a loop to ask the user to input each student's details.
- Add input validation to ensure the data is correct. For example, reject ages that are negative or non-numeric values.
- Move to Task 2: Calculating School House Statistics
- Create a function to filter students by school house.
- Write a function to calculate average reaction time for a given list.
- Use these functions to calculate and display statistics for Saturn and Mars.
- Move to Task 3: User-Input-Based Statistics
- Prompt the user for a specific age and school house.
- Use if conditions and loops to filter students who match the user input.
- Calculate and display the average and slowest reaction times for those students.
Breaking it down into functions and steps can help make the problem less intimidating
TIPS TO REMEMBER
- Work incrementally: Solve one sub-task before moving to the next. Test each piece of code to make sure it works before continuing.
- Use functions: Functions help in breaking down repetitive tasks into reusable components.
- Test often: After completing each part, test it to make sure it works correctly. This will help catch errors early.
- Focus on simplicity: Keep each part of the solution simple and understandable. Complexity will grow naturally as you add more functionality.
NAVIGATION
GETTING STARTED | Downloading Python and creating Hello World
VARIABLES AND DATA TYPES | Storing data in variables
OPERATORS | Performing calculations and comparisons
SELECTION | Using IF statements
ITERATION | Using FOR Loops and WHILE Loops
ARRAYS | Getting started with Lists
FUNCTIONS AND PROCEDURES | Breaking your code into logical sections
FORMATTING DATA | Further data manipulation
ERROR CHECKING | Using Try and Except to catch user errors
OBJECT ORIENTED PROGRAMMING | Getting to grips with OOP
VARIABLES AND DATA TYPES | Storing data in variables
OPERATORS | Performing calculations and comparisons
SELECTION | Using IF statements
ITERATION | Using FOR Loops and WHILE Loops
ARRAYS | Getting started with Lists
FUNCTIONS AND PROCEDURES | Breaking your code into logical sections
FORMATTING DATA | Further data manipulation
ERROR CHECKING | Using Try and Except to catch user errors
OBJECT ORIENTED PROGRAMMING | Getting to grips with OOP