GoalUse math.floor, math.ceil and random.randint correctly, and split a big problem into sub-problems.
New words
math.ceil · rounds a number up to the next whole number (a whole number stays the same)math.floor · rounds a number down to the whole number belowrandom.randint(a, b) · gives a whole number from a to b, and both ends are possibledecomposition · breaking a big problem into smaller sub-problems that are easier to solve
Explanation
A library is a set of ready-made sub-routines, and import lets a program use it. The math library has math.floor(), which rounds down, and math.ceil(), which rounds up, and math.pi, which has no brackets. Use ceil when a part-full box still needs a whole box, and floor when only complete groups count. The call random.randint(low, high) gives a whole number from low to high, and both ends can happen. A test must say what the possible values are, not one fixed output. Big problems are easier when they are decomposed: each sub-problem can be given to a different person, or written as its own sub-routine and reused. Honest programmers say which parts they borrowed.
Big problemsplit→Sub-problemswrite→Sub-routinesjoin→Whole program
Worked examples
Python
1import math
2guests = int(input("Guests? "))
3cakes = math.ceil(guests / 8)
4print("Cakes to buy:", cakes)
Output
Cakes to buy: 6
Cakes for a party. Each cake serves 8 guests. With 47 guests typed, 47 ÷ 8 = 5.875, so 5 cakes are not enough. math.ceil rounds up to 6. With exactly 48 guests the division gives 6.0, which stays 6. With 49 guests it gives 6.125 and ceil makes 7. In the same way math.floor(7.9) is 7 and math.ceil(7.1) is 8.
Python
1import random
2n = random.randint(2, 5)
3print(n * 5)
Random values. In this program n can be 2, 3, 4 or 5, and every one can happen. So the printed number can only be 10, 15, 20 or 25. It can never be 5, 30 or 12. Nobody can say which one the next run will print.
Decomposition of a big problem. A book swap is split into three sub-problems, and each of them into small tasks. Each sub-problem can be a sub-routine in a program, and a team can work on the parts at the same time.
Class activity
Coaches and dice (no computer). Part 1: choose three group sizes (for example 110 children and 30 seats on a coach). Calculate the exact answer with a pencil, then decide whether the situation needs floor (full coaches only) or ceil (coaches needed). Write one sentence for each. Part 2: roll a die and use the rule number = die + 2 and write the possible values before you roll. Roll 10 times: did any result fall outside your list? Part 3: with a partner, decompose a big job such as washing the family car into three sub-problems each, and compare. If you have a computer or tablet, an adult can help you run the cake program with different guests.
Practice
1
The program has pupils = 130 and 40 seats on a coach. What does it print? What would it print if math.floor was used, and which one answers the question How many coaches do we need?
Python
1import math
2pupils = 1303coaches = math.ceil(pupils / 40)
4print(coaches)
It prints 4, because 130 ÷ 40 = 3.25 and ceil rounds up. With floor it would print 3, which is only the number of full coaches. The question needs ceil, because the last 10 pupils still need a coach.
2
List all the values the program can print. Can it ever print 4? Explain.
Python
1import random
2n = random.randint(10, 13)
3print(n - 10)
n can be 10, 11, 12 or 13, so n − 10 can be 0, 1, 2 or 3. It can never print 4, because 14 is outside the range of randint(10, 13).
3
A pupil finds a dice program in a friend’s notebook, copies it, and writes it up as their own work. What should they have done instead?
They should ask the friend first and then say whose idea it was, for example with a comment at the top. They should also explain what each line does in their own words.
4
Decompose the problem Organise a class film afternoon into three sub-problems, each with two small tasks. Give one reason why a team finds this easier than one person doing everything at once.
For example: Room (book it, set out chairs); Film (choose it, check it works); Snacks (make a list, collect the money). A team can work on all the parts at the same time, and each person can do the part they are best at.