AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Term 2 check-in

Week 21 · Review: Term 2 · 3 lessons
1

Library, chance and parts

Computing 8: Topics 1.6–2.2, pp. 137–229
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.

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.
Make a boxCount the booksCollect booksList the wishesPair them upMatch readersBook a roomTell everyoneSwap dayRun a book swap
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 = 130
3coaches = math.ceil(pupils / 40)
4print(coaches)
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)
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?
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.