Computing Textbook
Computing Textbook
ii
Preface
This textbook is written in alignment with the Ministry of Education’s O-Level Computing syllabus.
The concepts are presented through the use of real-life examples and authentic contexts with the
aim of equipping students with the necessary knowledge in the subject, as well as opportunities
for the development of computational thinking skills, and the ethical use and handling of data.
Chapter Opener
• Full-page lead-in to the chapter with a defined problem context
• Framed as a question and supported with characters or illustrations to stimulate thinking
Computers have changed the way we work, live and play. Besides desktops and laptops,
today you can find computers in cars, mobile phones and even watches. To make
the most of the computers around us, we need to understand how they are designed
to function.
This chapter will introduce the major components of computer architecture and their
functions. You will also learn about how instructions are executed during program
operation and how the various components work together.
• Perform a dry run of a set of steps to determine its purpose and/or output. Learning Outcomes
• Produce trace tables to show values of variables at each stage in a set of steps.
• Locate logic errors in an algorithm, and correct or modify an algorithm to remove the
errors or for changes in task specification. • Provided at the beginning to
3.1 Variables
direct students towards the
We often give names to values when defining inputs and outputs or generalising problems and
solutions. For example, in the problem shown in Table 3.1, x and y are the named values.
focus of the chapter
Table 3.1 Defining the addition problem
Input Output
p Figure 3.1 Just like a box can store different kinds of items,
a variable can store different types of data
42
iii
Did you know?
Module 4: Programming
In summary, the syntax for a replacement field is as follows: • Provides interesting facts relevant to the topic
Syntax 5.3 Replacement field
Quick Check
{}
{index}
The str.format() method and replacement fields can also perform more complex tasks such
concepts through short-answer questions
as lengthening strs to a specified width and restricting the number of decimal places used when
converting a float to a str. Use of the str.format() method to perform such tasks will not
be covered in this textbook.
1. Fill in the blank in the program below so that it correctly outputs whether the first
character of name is an upper-case letter. Review Questions
name = input("Enter name: ")
1. A program is needed to solve the following word reversal problem:
print( )
Table 6.34 Defining the word reversal problem
p Figure 5.26 Program template
Input Output
2. Complete the program below by filling in the blank with one or more lines of code so
that it correctly outputs whether title contains upper-case letters and spaces only • input_text: phrase to • Given phrase with the order of the letters
(i.e., no lower-case letters, symbols or digits). reverse, should contain only of each word reversed but the words
letters and spaces themselves still in order, separated by one
title = input("Enter title: ") space between words
p Figure 5.27 Program template The draft program in Figure 6.42 is written for this purpose.
a) The main algorithm for the program consists of three parts. Read through the code and
state what each of the following parts of the program are intended to do:
i) Lines 13 to 25
ii) Lines 27 to 35
iii) Lines 37 to 42
b) When this program is run on the following test case, the actual output does not match the
72 173 expected output:
Review Questions
Result
Msgnitee on eetteuqit
• Provided at the end of the chapter to reinforce i) State the kind of condition (normal, boundary or error) covered by this test case.
ii) State the kind of error (syntax, run-time or logic) that has occurred and why.
and consolidate the concepts learnt in the iii) Find out where the error is located by disabling lines of code and testing the program
in chunks or parts. State the line on which the error is located and explain why the line
is incorrect.
chapter through structured questions iv) Rewrite the program to fix the error so that it passes the test case.
242 243
ANSWERS
IF...ELSE... CASE...OTHERWISE... WHILE... REPEAT... FOR... Chapter 1 How Do Computers Work?
ENDIF ENDCASE ENDWHILE UNTIL NEXT
Quick Check 1.1
1. True. However, “kilobyte” is still often confused with “kibibyte”, which is 1,024 bytes.
Selection Input/Output Sequence Iteration 2. a) 2,017 kB = 2,017 × (103 bytes) = 2,017,000 bytes
Constructs Constructs Constructs
b) 19 GB = 19 × (109 bytes) = 19,000,000,000 bytes
c) 65 MB = 65 × (106 bytes) = 65,000,000 bytes
1. The main differences between RAM and ROM are summarised below:
RAM ROM
FLOWCHARTS AND
PSEUDO-CODE Read and write: stored data can be Read only: stored data cannot be easily
easily changed changed
Volatile: loses data once power supply Non-volatile: retains data regardless of
to the computer is interrupted whether the power supply is switched
TRACE TABLES FLOWCHARTS on or off
Purpose: to store data and instructions Purpose: to store data and instructions
temporarily so that they can be quickly that would be needed for a computer to
Dry Runs
accessed by the processor when needed start up or before data can be loaded into
Symbols Structuring
RAM
Flowcharts
Producing a
Terminator Quick Check 1.4
Trace Table Sequence
1. True. Memory addresses are just numbers so they can also be treated as data.
Data
Selection
Correcting
an Algorithm Quick Check 1.6
Decision
Iteration
1. a) A hard disk. It is the only device with storage capacity available in terabytes.
Process b) A memory card. It is the only device small and flat enough to fit in a wallet. A memory
card would also be less vulnerable to mechanical shocks that could occur in a wallet.
68 69
A–PB A–1
Chapter Summary
Answers
• Provides an overview of the key concepts
in the chapter as well as links between • Provides answers and explanations to
them using a concept map Quick Check and Review Questions
iv
Contents
PREFACE
3.1 Variables 38
3.2 Flowcharts 40
3.3 Trace Tables 51
3.4 Pseudo-Code 57
v
Chapter 7 How Can I Be a Safe and Responsible Computer User? 239
Chapter 10 How are Spreadsheets Used to Process and Analyse Data? 337
10.1 Understanding Spreadsheets 338
10.2 Data Management 352
10.3 Functions 356
10.4 What-If Analysis 378
10.5 Conditional Formatting 382
Answers A–1
1 Module 2: Systems and Communications
Computers have changed the way we work, live and play. Besides desktops and laptops,
today you can find computers in cars, mobile phones and even watches. To make
the most of the computers around us, we need to understand how they are designed
to function.
This chapter will introduce the major components of computer architecture and their
functions. You will also learn about how instructions are executed during program
operation and how the various components work together.
1
How Do Computers Work?
A computer (or computer system) is a device that can receive data, process the data according
to a set of instructions and produce the processed data as a result. The computer architecture
used describes how a computer is designed and built to function, including how its parts are
designed, organised and connected.
There are many ways to build a computer. For instance, while most computers today run on
electricity, early computers did not use electricity at all. Instead, they worked based on mechanisms
such as cranks, gears and levers. These mechanical computers thus used a completely different
computer architecture from the computers we are familiar with today.
Data
Information that is used in a
computer program (singular: datum)
2
Module 2: Systems and Communications
A single bit would be too simple to represent complex data, so we usually talk about data being
represented as bytes instead. A byte is a unit of data made up of eight bits.
Table 1.1 summarises some units of measure of data in order of increasing size.
In the past, units of data such as “kilobyte”, “megabyte” and “gigabyte” were based on powers
of 1,024 (or 210) instead of the standard powers of 1,000 used in the International System of Units
(SI). However, since 1998, multiple standards organisations have agreed that SI prefixes (i.e., “kilo”,
“mega”, “giga”, etc.) should follow their standard meanings while new binary prefixes (i.e., “kibi”,
“mebi”, “gibi”, etc.) would be used to represent powers of 1,024.
Despite this change, you may still encounter the use of “kilobyte” and “megabyte” to represent
1,024 bytes and 1,0242 bytes respectively, instead of the correct units “kibibyte” and “mebibyte”.
Role Description
Processes data and follows instructions; consists of an
Processor
arithmetic logic unit and a control unit
Stores data, instructions and the results of processing for
Memory
immediate use
Stores large amounts of data that will not be lost when power
Secondary storage
supply is interrupted
3
How Do Computers Work?
Role Description
Data bus Transports data between memory and processor; bi-directional
Transports required memory location from processor to
Address bus
memory; uni-directional
Input Data or instructions that the computer receives
Intermediate or final results produced by the computer; usually
Output
in the form of processed data
Figure 1.1 shows where the parts performing these roles may be found inside the case of a
desktop computer.
Input/Output
Memory
Secondary
storage
Address bus
(green)
Data bus
(red)
Processor
4
Module 2: Systems and Communications
1.2 Processor
When we say that a processor runs or executes instructions, it means that the processor follows
or performs instructions. A series of instructions is called a program, or simply software.
Often, a processor’s speed is described by the number of instructions that the processor can
perform in one second. For instance, a 1 MHz (megahertz) processor can perform one million
instructions per second while a 1 GHz (gigahertz) processor can perform one billion instructions
per second. In general, the larger the number and unit, the faster and more powerful the processor.
5
How Do Computers Work?
There are also “multi-core” processors that contain multiple processing units inside a single
package. For instance, a “dual-core” package has two such processing units while a “quad-core”
package has four. These “multi-core” processors can perform more than one instruction at the
same time, and thus are more powerful than “single-core” processors.
1.3 Memory
In Figure 1.3, we used big physical switches to represent memory, but in most computers, memory
is actually made up of very small electronic switches that take up much less space.
Usually, the position of each byte is represented by a number called an address. This is a number
that allows a computer to quickly find those switches again if it needs to read or change the
data that is stored. This is similar to how a unit number is used to identify the location of each
residence in an HDB block.
6
Module 2: Systems and Communications
When we use the word “memory”, we are usually referring to RAM and not the other types
of memory. Most desktop computers have RAM located on removable cards so that the
amount of RAM in the computer can be upgraded. Other kinds of computers, however,
may have only a fixed amount of RAM that cannot be changed.
3. Read-only memory (ROM) is where data and instructions that rarely need to change or
would be needed for a computer to start up are stored. Data stored on ROM cannot be
easily changed and remains there regardless of whether the power supply is switched on
or off. This makes it ideal for storing instructions that are needed before data can be loaded
into RAM.
4. Secondary storage is where large amounts of data are stored, such as in a hard disk
or hard drive. To avoid confusion, we typically use the word “storage” for this kind of
storage space instead of the word “memory”. Different storage media are used for secondary
storage. The different kinds of storage media will be discussed in section 1.6.
7
How Do Computers Work?
Conductive lines
(including data and address buses)
8
Module 2: Systems and Communications
There are many other buses that transport data from one part of the computer to another. Some
examples are the control bus that transports instructions from the processor’s control unit, and
the expansion buses such as those used for connecting external devices.
For instance, to read data from memory, the processor requests the data, and the address bus
transports the requested data’s address to RAM. A copy of the requested data is then sent from
RAM back to the processor via the data bus. This is illustrated in Figure 1.7, which uses bits for
data and simple numbers for addresses.
Data bus
Address bus
p Figure 1.7 How the data bus and address bus are used to read from memory
When writing data to memory, the processor uses both the data bus and address bus at the
same time to transport the data for writing as well as the destination address to RAM. RAM then
sets the switches at the destination address according to the data received via the data bus, as
illustrated in Figure 1.8.
9
How Do Computers Work?
0 01001011 0 01001011
1 10100101 1 10100101
11111111
2 01100101 2 01100101
CPU
3 10110010 3 10110010
CPU Databus
Data Bus
4
4 01100100 4 11111111
Address bus
Address Bus
5 01110101 5 01110101
6 11001100 6 11001100
… …and so on... … …and so on...
p Figure 1.8 How the data bus and address bus are used to write to memory
As can be seen by the arrows in Figures 1.7 and 1.8, while the address bus is always used to
transport information from the processor to memory, the data bus can transport information in
either direction.
1. Memory addresses can also be read from or written to memory as data. True or false?
10
Module 2: Systems and Communications
Inputs Outputs
Scanner
Printer
Camera
CPU
Secondary storage is a way of storing large amounts of data that will not be lost when power supply
is interrupted. Compared to RAM, secondary storage is usually cheaper and able to store much
more data. It is also non-volatile, so the data that is stored
remains there even without a power supply. This makes Key Term
secondary storage ideal for physically transporting data from
one computer to another. On the other hand, secondary
Secondary storage
storage is usually much slower in speed compared to RAM.
Way of storing large
amounts of data that will not
The processor usually does not access data in secondary
be lost when power supply
storage directly. Instead, any data in secondary storage that
is interrupted
the processor needs might be copied to RAM first.
11
How Do Computers Work?
Factor Consideration
12
Module 2: Systems and Communications
• Vulnerable to
scratches and
fingerprints
13
How Do Computers Work?
Chapter Summary
COMPUTER
ARCHITECTURE
Solid-state
14
Module 2: Systems and Communications
Review Questions
3. Arrange the following storage media in order of increasing typical storage capacity.
4. Refer to the flyers that Alex collected earlier in the chapter. Tabulate the specifications of
the following items for each computer stated in the flyers:
• Processor
• Memory
• Storage media options (such as magnetic hard disks and optical disc writers)
6. Calculate the number of bits (not bytes) in the following amounts of data:
a) 8 TB
b) 0.125 MB
c) 0.125 MiB
15
How Do Computers Work?
8. Suppose A and B are two memory addresses and R is a processor register. One way to
copy data from address A to address B of memory is to perform the following two steps:
Step 1: Read the data at address A of memory and store it in register R.
Step 2: Write the data in register R to address B of memory.
In Step 2, the address bus is used to transport B from the to the ,
and the data bus is used to transport data from the to the .
c) Suppose R and S are two processor registers. Explain why the data and address buses
are not needed to copy data from R to S.
16
2 Module 3: Abstraction and Algorithms
Pines Primary
School
Siti's house
In the previous chapter, we learnt about the architecture of computers used today. One
feature of this architecture is that computers can follow step-by-step instructions stored
in memory. Whenever instructions to solve a problem can be represented as a number
of steps, this set of steps is known as an algorithm.
In this chapter, you will learn about algorithms and what they can be used for. You
will also learn how to specify inputs and outputs to problems so that algorithms can
be used to solve them. Finally, you will apply the techniques of decomposition and
generalisation to design your own algorithms.
17
How Can Algorithms Be Used to Solve Problems?
An algorithm is a solution that solves a problem through a set of clearly defined steps. It involves
the development of a set of instructions or rules.
Rubik's cube
Sudoku puzzle
Navigation device
18
Module 3: Abstraction and Algorithms
Step 1: Write the first number, then write the second number below it such that the digits
in the ones place are aligned vertically. Draw a horizontal line below the second
number, as in Figure 2.2.
First number
Second number
Step 2: Let the current column be the right-most column of digits in the ones place.
Step 3: Add all the digits (including any “carry-over” digits) in the current column. If a digit
is missing, treat it as 0. There is no “carry-over” digit for the right-most column.
Step 4: If the result is greater than 9, write “1” at the top of the column immediately to the
left of the current column as a “carry-over” digit.
Step 5: Write the last digit of the result at the bottom of the current column, below the line.
Step 6: If there are still digits (including “carry-over” digits) to the left of the current column,
redefine that column on the left as the new current column and go back to Step 3.
Otherwise, the final answer is formed by the row of digits below the line.
"Carry-over" digits
First number
Second number
Final answer
The advantage of writing out an algorithm step by step is that these steps can be followed precisely
by anyone who can add digits to obtain the correct answer. This particular algorithm is also useful
because it is general and works for adding any two positive whole numbers.
19
How Can Algorithms Be Used to Solve Problems?
2.1.2 Example: Algorithm for Calculating the Mean Subject Grade for
Seven Subjects
In some secondary schools, the Mean Subject Grade (MSG) is used to measure a student’s
performance in school. The MSG is a number between 1 (best) and 9 (worst) calculated from
the student’s rounded percentage scores in each subject.
The following steps provide an algorithm to calculate the MSG of a student taking seven subjects:
Step 1: Convert the rounded percentage scores into grade points using the table below.
75–100 A1 1
70–74 A2 2
65–69 B3 3
60–64 B4 4
55–59 C5 5
50–54 C6 6
45–49 D7 7
40–44 E8 8
Less than 40 F9 9
For instance, if the rounded percentage scores for the student’s seven subjects are as shown in
Table 2.2, the corresponding MSG would be 3.14.
20
Module 3: Abstraction and Algorithms
#1 63
#2 74
#3 73
#4 56
#5 80
#6 50
#7 72
This algorithm is useful as anyone can follow the steps precisely to obtain the MSG of any
student (as long as the student takes exactly seven subjects).
Just like the addition and MSG calculation algorithms, this algorithm is useful because anyone
can follow these steps precisely to bake a cake. However, this particular algorithm is not general
as it only works for baking one type of cake and for a fixed serving size.
We usually want algorithms to be as general as possible so that they can be used for different
problems. You will learn later about a technique for doing this, which is called generalisation.
21
How Can Algorithms Be Used to Solve Problems?
2. Modify the addition algorithm described in section 2.1.1 so that it works for three
positive whole numbers. Describe the changes that you made.
3. Write out the steps for an algorithm to find the top score of a class test.
(Hint: Make sure that the steps are written clearly so that anyone with just the steps
and a list of scores can obtain the correct answer.)
22
Module 3: Abstraction and Algorithms
It is also a good practice to give each input a name. For instance, we can specify the inputs to
the addition problem that can be solved using the algorithm in section 2.1.1 in this manner:
Table 2.3 Specifying the inputs to the addition problem for whole numbers (positive only)
Input
Similarly, the inputs to the problem of calculating the MSG for a student taking seven subjects
can be specified by treating each subject’s rounded percentage score as a separate input:
Table 2.4 Specifying the inputs to the problem of calculating the MSG for seven subjects
Input
23
How Can Algorithms Be Used to Solve Problems?
However, this way of specifying the inputs can be quite repetitive. Since the requirements for
each rounded percentage score are the same, we can shorten the specification as follows:
Table 2.5 Simplified way of specifying the inputs to the problem of calculating the MSG for seven subjects
Input
For instance, we can specify the output of the addition problem that can be solved using the
algorithm in section 2.1.1 in this manner:
Table 2.6 Specifying the output of the addition problem for whole numbers (positive only)
Output
Similarly, we can specify the output of the problem of calculating the MSG for a student taking
seven subjects in this manner:
Output
However, if we require the MSG values to be rounded to two decimal places, we can revise the
problem of calculating the MSG for seven subjects accordingly:
Output
24
Module 3: Abstraction and Algorithms
2. Alex wants to find the weight of the heaviest apple on display at the market. Specify
the input and output requirements for Alex’s problem.
3. Table 2.9 shows a possible set of input and output requirements for calculating the
average score of a class test. Suggest how it can be improved and why.
Table 2.9 Input and output requirements for the average-score problem
Input Output
2.3.1 Decomposition
Key Terms
Decomposition is a technique of breaking down
a complex problem or process into smaller parts Decomposition
(known as sub-problems) such that each part is Breaking down a complex problem
more manageable and easier to understand. The or process into smaller and more
parts are evaluated separately and the solutions manageable parts
to these parts are then combined to solve the
original problem. Sub-problem
A problem whose solution contributes
Decomposing a problem is used not just for to the solution of a bigger problem
computing, but for any problem that may appear
too big or complex to understand or solve. For
instance, to lift and install the 7,000-tonne Marina Bay Sands SkyPark, the structure was divided
into 14 different pieces which were built and assembled separately at ground level. Breaking
down the construction into 14 pieces made each piece small enough to be lifted up, piece
by piece, onto the 57th floor. This would not have been possible if this problem had not been
decomposed beforehand.
25
How Can Algorithms Be Used to Solve Problems?
1. Incremental approach:
Identify quantitative features of the input or output that are causing the problem to be
too large to handle. Sometimes, the solution to a small version of the problem with one
or more of these features reduced can be found and gradually extended to larger
versions of the problem. Each gradual extension of the solution is a separate
sub-problem.
2. Modular approach:
Solve simple examples of the problem manually and identify tasks that are of different
natures. Usually, these tasks can be separated from each other to become distinct
(and sometimes unrelated) sub-problems. This usually results in sub-problems that
are different from each other.
Table 2.10 Input and output requirements for the addition problem
for whole numbers (positive only)
Input Output
We know how to add single digits. The reason why this problem is more difficult to solve is that
both the inputs and the output can have multiple digits.
Using the incremental approach, we can identify the number of digits for the inputs as one of
the features that cause the problem to be too large to handle.
Incremental approach:
Identify quantitative features of the input or output that are causing the problem to be too
large to handle. Sometimes, the solution to a small version of the problem with one or
more of these features reduced can be found and gradually extended to larger versions of
the problem. Each gradual extension of the solution is a separate sub-problem.
26
Module 3: Abstraction and Algorithms
Suppose the inputs to the problem have multiple digits; we can then gradually extend the solution
for single digits to larger versions of the problem via the following sub-problems:
Table 2.11 Decomposing the addition problem for whole numbers (positive only)
Let us look at the revised problem of calculating the MSG for seven subjects:
Table 2.12 Input and output requirements for the problem of calculating the
MSG for seven subjects
Input Output
If we try to calculate the MSG for the example provided in Table 2.2, we find that the task of
converting rounded percentage scores to grade points is quite separate from the task of summing
up the grade points and dividing the result by 7. This motivates us to use the modular approach.
27
How Can Algorithms Be Used to Solve Problems?
Modular approach:
Solve simple examples of the problem manually and identify tasks that are of different
natures. Usually, these tasks can be separated from each other to become distinct (and
sometimes unrelated) sub-problems. This usually results in sub-problems that are different
from each other.
We can thus decompose the original problem into the following two sub-problems:
Table 2.13 Decomposing the problem of calculating the MSG for seven subjects
Each sub-problem is simpler and easier to solve on its own compared to the original problem.
28
Module 3: Abstraction and Algorithms
2. Identifying patterns among two or more solutions. These solutions may come from solving
the sub-problems obtained from decomposition, or from solving examples of a more general
problem. These patterns are clues that these solutions can be generalised to produce a
single solution that can solve multiple problems.
3. Identifying patterns among two or more steps in a solution. These patterns indicate that the
solution can be simplified or shortened using iteration structures that you will learn about
in the next chapter.
2.3.2.1 Example: Solving the Sub-Problem of Calculating the MSG from the Grade
Points of Seven Subjects
Let us look at sub-problem #2 we obtained from decomposing the calculation of MSG for
seven subjects:
Table 2.14 Input and output requirements for the problem of calculating
MSG from the grade points of seven subjects
Compare this with the problem of calculating the average of seven numbers:
Table 2.15 Input and output requirements for the problem of calculating
the average of seven numbers
Input Output
29
How Can Algorithms Be Used to Solve Problems?
The MSG is just an average of the grade points, so by using pattern recognition, we can see that
both problems are similar, with the only differences being that:
1. The numbers to be averaged are called “grade points” and must be whole numbers between
1 and 9 inclusive.
2. The average is called “MSG” and must be rounded to two decimal places.
With this information as a guide, we can easily modify the algorithm to calculate the average of
seven numbers so that it becomes a solution to the problem of calculating MSG from the grade
points of seven subjects:
Step 1: Add up all the grade points.
Step 2: Divide the result by 7.
Step 3: Round the result to two decimal places.
In this case, the only significant modifications needed are the renaming of “numbers” to “grade
points” and the addition of one more step.
2.3.3 Generalisation
Generalisation is a technique of replacing two or more similar items with a single, more general item.
This can be done with both problems and solutions. In particular, a general solution is extremely
useful because it allows us to solve a large number of problems
with just a single solution. The addition algorithm we learnt
Key Term
earlier is an example of a general solution.
The same idea can be used to “pull out” common elements or steps from multiple problems
or solutions into a more general problem or solution.
30
Module 3: Abstraction and Algorithms
Table 2.16 Input and output requirements for the problem of calculating the MSG for seven subjects
Input Output
• Score: rounded percentage score • MSG for the seven subjects, rounded
for subject; must be a whole to two decimal places
number between 0 and 100
inclusive (provided seven times,
once for each subject)
Table 2.17 Input and output requirements for the problem of calculating the MSG for eight subjects
Input Output
• Score: rounded percentage score • MSG for the eight subjects, rounded
for subject; must be a whole to two decimal places
number between 0 and 100
inclusive (provided eight times,
once for each subject)
Table 2.18 Input and output requirements for the problem of calculating the MSG for nine subjects
Input Output
• Score: rounded percentage score • MSG for the nine subjects, rounded
for subject; must be a whole to two decimal places
number between 0 and 100
inclusive (provided nine times,
once for each subject)
The only difference between the three problems is the number of subjects. Let us call this value
number and come up with a more general problem with this name:
Table 2.19 Input and output requirements for the problem of calculating
the MSG for a number of subjects
Input Output
31
How Can Algorithms Be Used to Solve Problems?
Table 2.19 shows the generalised version of the three problems because it can recreate those
problems by just providing different values for number.
The solutions to the three problems can also be generalised in this way. Suppose that the three
solutions are identical except for Step 3 in the algorithm:
• For the original problem of calculating the MSG for seven subjects:
Step 3: Divide the result by 7.
• For the problem of calculating the MSG for eight subjects:
Step 3: Divide the result by 8.
• For the problem of calculating the MSG for nine subjects:
Step 3: Divide the result by 9.
From this, we can see that the sum of grade points must be divided by the number of subjects to
obtain the correct MSG. If we use number to represent the number of subjects that the student
is offering, we have the following general solution:
Step 1: Convert the rounded percentage scores into grade points using the table below.
75–100 A1 1
70–74 A2 2
65–69 B3 3
60–64 B4 4
55–59 C5 5
50–54 C6 6
45–49 D7 7
40–44 E8 8
Less than 40 F9 9
32
Module 3: Abstraction and Algorithms
Table 2.21 Input and output requirements for the problem of calculating
the MSG for a number of subjects
Input Output
• Score: rounded
percentage score for
subject; must be a
whole number
between 0 and 100
inclusive (provided
number times, once for
each subject)
Based on the earlier examples, a basic algorithm to generalise two or more problems would be:
Step 1: Identify the parts that are different among the problems being considered and give
each part a name (e.g., number for the different number of subjects).
Step 2: Copy the identical parts into a new problem, replacing the parts that are different
using the names chosen in Step 1.
Step 3: Add the names chosen in Step 1 as required inputs to the problem. Think through
and define the range of valid or acceptable values for these new inputs (e.g., number
should be a positive whole number). The result is a generalised version of the problems
being considered.
33
How Can Algorithms Be Used to Solve Problems?
1. Choose the correct words for the passage below from the given choices:
If a complex problem is not broken down into parts, the time taken to solve
the problem may be too (short/long). The technique of breaking down the
problem into parts, called (decomposition/generalisation), allows the parts
to be evaluated separately. By making the parts (less/more) manageable, the
solutions to these parts can then be (combined/separated), and the original
problem can be solved.
3. Siti wants to find a general solution to calculate the absolute or positive difference
between any two positive whole numbers. For example, the absolute difference
between 17 and 20 is 3 (not –3) and the absolute difference between 20 and 17
is also 3.
a) Specify the input and output requirements for Siti’s problem.
b) Calculate the absolute difference between 2017 and 1965. Write out the algorithm
that you used. You may assume that the person following the algorithm can
subtract and compare numbers without help.
c) Calculate the absolute difference between 2017 and 2020. Write out the
algorithm that you used. You may assume that the person following the algorithm
can subtract and compare numbers without help.
d) Generalise the solutions used for the two problems in b) and c) above and write
out a general solution for Siti’s problem.
34
Module 3: Abstraction and Algorithms
Chapter Summary
DEFINITION EXAMPLES
ALGORITHMS
UNDERSTANDING PROBLEM-SOLVING
PROBLEMS TECHNIQUES
Pattern Recognition
35
How Can Algorithms Be Used to Solve Problems?
Review Questions
1. The following are the steps involved in making and drinking a cup of tea. Arrange the steps
in the appropriate order to describe the correct algorithm for this process.
– Drink the contents of the cup.
– Add tap water to the kettle.
– Wait for a few minutes to let the tea steep.
– Place a teabag in a cup and pour some water from the kettle into the cup.
– Boil the contents of the kettle.
2. Consider Siti’s problem of finding the shortest route from her home to her sister’s school,
which was shown at the beginning of this chapter.
Input Output
The following is a set of input and output requirements for sub-problem #2 after Siti’s
problem is decomposed into two sub-problems.
Suggest a possible set of input and output requirements for sub-problem #1.
36
3 Module 3: Abstraction and Algorithms
In the previous chapter, we learnt how to write out algorithms as a series of steps.
Sometimes, this may not be the best way to represent an algorithm.
In this chapter, you will learn how to give names to values and represent algorithms in
flowcharts and pseudo-code, as well as how to analyse and correct algorithms using
trace tables.
37
How Do I Use Flowcharts and Pseudo-Code?
• Perform a dry run of a set of steps to determine its purpose and/or output.
• Produce trace tables to show values of variables at each stage in a set of steps.
• Locate logic errors in an algorithm, and correct or modify an algorithm to remove the
errors or for changes in task specification.
3.1 Variables
We often give names to values when specifying inputs and outputs or generalising problems and
solutions. For example, in the problem shown in Table 3.1, x and y are the named values.
Table 3.1 Input and output requirements for the addition problem
Input Output
In flowcharts and pseudo-code, these named values are called variables. Imagine a variable as
a box that is labelled with its name. A variable can store different types of data just as a box can
store different kinds of items (see Figure 3.1). Data can come in many forms such as letters, lists,
numbers and words.
Key Term
38
Module 3: Abstraction and Algorithms
The left arrow symbol (←) is sometimes used instead of the equals sign (=) to store values in
variables. For example, x ← 5 has the same meaning as x = 5.
A variable is initialised when a value is first stored in it. It is usually a good practice to initialise
a variable as soon as there is a value to be assigned to it.
In flowcharts and pseudo-code, a variable can only store one value at a time. Assigning new data
to a variable that already contains data will overwrite the old data. In this way, the stored value
of a variable can be changed.
Variable names should be meaningful, short and consist of a single word only. They need to
be meaningful as variable names are often the only descriptions readers have to help them
determine what each variable represents.
At the same time, they need to be short as variable names are Key Term
likely to be used multiple times in an algorithm. Variable names
that are too long can result in algorithms that are tedious to both Initialise
write and read. For example, “name” or “age” would be good To store or assign a
variable names. If a variable is meant to store just a number or value to a variable for
numerical value, then single-letter names like “x” and “y” are the first time
also acceptable.
For example, if a variable named y is a list, then we can use the variables y[0], y[1] and y[2] to
access the first three values stored in y. To store the value 5 in the first position of the list, we
can say y[0] = 5.
39
How Do I Use Flowcharts and Pseudo-Code?
3.2 Flowcharts
This same algorithm can also be represented using the flowchart as shown in Figure 3.2.
Leave home
Check time
Yes No
Reach school
The earliest forms of flowcharts were introduced by engineer Frank Gilbreth in 1921 to visualise
processes used in industrial production, sales and finance.
40
Module 3: Abstraction and Algorithms
Symbol (flowchart)
Special shape used to
3.2.1 Standard Flowchart Symbols represent an action or step in
There are four standard symbols used in flowcharts: a flowchart
1. Terminator
2. Data
3. Decision
4. Process
It represents the beginning or end of a set of steps, and usually contains either the START or STOP
command as shown in Figure 3.4.
START
STOP
41
How Do I Use Flowcharts and Pseudo-Code?
It represents the step of either receiving input data from outside the algorithm using the INPUT
command or producing output from within the algorithm using the OUTPUT command.
It represents a step involving a question. The outgoing arrows represent the possible outcomes to
the question and are usually labelled “Yes” and “No”. There may be two or three outgoing arrows
depending on the number of possible outcomes. Only one of these outgoing arrows should be
followed when performing the algorithm.
Note that the question used in a decision symbol may involve checking if two values are equal
or not equal. We typically use the special symbols “==” and “!=” to represent such checks. For
example, to check if the variables x and y are equal, we would ask “Is x == y?” Conversely, to
check if the variables x and y are not equal, we would ask “Is x != y?”
42
Module 3: Abstraction and Algorithms
Yes No Yes No
It represents a step involving an action or operation. This usually involves changing the value of
a variable or performing more complex actions, as shown in Figure 3.10.
43
How Do I Use Flowcharts and Pseudo-Code?
START START
STOP
STOP STOP
p Figure 3.11 A flowchart must only have one start and one or more end terminator symbols.
2. Each flowchart symbol (except the terminator and decision symbols) must have exactly
one entry point and one exit point.
p Figure 3.12 Each flowchart symbol must have only one entry and one exit point.
44
Module 3: Abstraction and Algorithms
However, these rules are not enough. The flow lines should
also follow the sequence, selection and iteration constructs Key Terms
(which will be discussed in the following sections) to avoid
ending up with overly complicated flowcharts that are Construct
difficult to read or understand. Like symbols, each of these Algorithm structure with
constructs has exactly one entry point and one exit point. exactly one entry point and
one exit point
Sequence construct
3.2.2.1 Sequence Construct for performing
multiple instructions in a
The most straightforward way to connect multiple symbols
fixed order
or constructs is to link them into a chain with flow lines
pointed in the same direction so that there is only one entry
point and one exit point. This is called a sequence construct
and it is used to perform multiple instructions in a fixed order.
Entry point
…
First instruction
Second instruction
Third instruction
…
Exit point
45
How Do I Use Flowcharts and Pseudo-Code?
3.2.2.2 Selection
Whenever a decision symbol is used, it always has exactly one incoming flow line that serves as an
entry point but may have two or more outgoing flow lines exiting from it. These outgoing flow lines
lead to different branches or sequences of symbols and
constructs. In some flowcharts, these separate sequences Key Terms
merge back into a single flow line. This forms a selection
construct with one entry point at the decision symbol Branch
and one exit point where the multiple branches merge. Sequence of instructions that
serves as one option out of two
We call this a selection construct as the person or or more choices
computer running the flowchart has to select only one of
the different branches from the decision symbol to follow. Selection construct
For instance, Figure 3.15 shows a selection construct with Construct for choosing between
two branches (i.e., a “Yes” branch and a “No” branch). two or more branches based on
a particular condition
Entry point
…
Yes No
…
Exit point
3.2.2.3 Iteration
Key Terms
Sometimes, instead of having all the branches merge
together, one or more of the branches may lead back Iterate
to the original decision symbol, forming a closed loop. Repeat
In the flowchart in Figure 3.16, there is one entry point
at the decision symbol and one exit point at the branch Iteration construct
that does not form a loop. This is called an iteration Construct for repeating
construct and it is used to repeat instructions while a instructions while a particular
particular condition is true. This construct is often used condition is true
to iterate or repeat the instructions.
46
Module 3: Abstraction and Algorithms
Entry point
…
Instruction(s) to repeat
No
…
Exit point
Table 3.2 Input and output requirements for the problem of adding five numbers
Input Output
• x1: a number • The sum of x1, x2, x3, x4 and x5, calculated by
adding only two numbers at a time
• x2: a number
• x3: a number
• x4: a number
• x5: a number
47
How Do I Use Flowcharts and Pseudo-Code?
We can represent this algorithm as a flowchart by converting Steps 1 to 6 into process symbols
and Step 7 into a data symbol for output (see Figure 3.17). The symbols can then be arranged into
a sequence construct so that they are performed in order. Terminator symbols to start and stop
the flowchart, and a data symbol to receive input are then added accordingly.
START
result = 0
result = result + x1
result = result + x2
result = result + x3
result = result + x4
result = result + x5
OUTPUT result
STOP
Looking at the flowchart in Figure 3.17, we can see a pattern that repeats – many steps of the
algorithm are similar. This usually means that we can shorten the algorithm by generalising the
steps and using an iteration construct instead. To do this, instead of having all five inputs being
provided all at once, the inputs can be provided as a sequence that can be checked up to five
times for the next input. We can change how the problem is specified as shown in Table 3.3.
48
Module 3: Abstraction and Algorithms
Table 3.3 Input and output requirements for the problem of adding five numbers
Input Output
This can be directly translated to a flowchart by having each step converted into a symbol, as
shown in Figure 3.18.
START
result = 0
No
Is there more input?
Yes
p Figure 3.18 Solution to the problem of adding five numbers that uses iteration
49
How Do I Use Flowcharts and Pseudo-Code?
This solution shown in the flowchart is shorter than the previous solution as the repeated steps
have been replaced by an iteration construct. However, this flowchart does not tell us how to
check for any more input. To avoid leaving such details out, we can explicitly keep track of how
many inputs have been processed so far by using a variable named counter (see Figure 3.19).
START
result = 0
counter = 0
No
Is counter < 5?
Yes
counter = counter + 1
p Figure 3.19 Solution to the problem of adding five numbers that uses iteration and a counter variable
The level of detail that is needed in a flowchart depends on the amount of guidance that the
reader will need. However, as you will learn in the next chapter, computers can only understand
very simple instructions, so more guidance (such as including the additional counter variable
here) is usually needed.
50
Module 3: Abstraction and Algorithms
51
How Do I Use Flowcharts and Pseudo-Code?
START
result = 0
counter = 0
No
Is counter < 5?
Yes
counter = counter + 1
p Figure 3.20 Solution to the problem of adding five numbers that uses iteration and a counter variable
52
Module 3: Abstraction and Algorithms
Table 3.4 shows the completed trace table from the dry run.
10
10
20
30
30
60
40
100
50
150
150
A trace table always has one column for every variable used in the flowchart and an additional
OUTPUT column on the right. A new row is added to the trace table each time output is produced
or if the value in a variable is changed during the dry run. The corresponding output or new
variable value is then recorded under the appropriate column.
53
How Do I Use Flowcharts and Pseudo-Code?
For instance, by studying the completed trace table in the previous example, we can see that:
1. The values of x follow the sequential order of the test data. Thus, we can conclude that
the purpose of x is to store the current input for processing.
2. The value of result starts from 0 and each subsequent iteration increases the value of result
by the value of x. Thus, we can conclude that the purpose of result is to store the cumulative
sum of inputs.
3. The value of counter starts from 0 and increases by 1 each time result is updated. Thus,
we can conclude that the purpose of counter is to track the number of processed inputs.
4. The output is given by the last value of result. Thus, we can conclude that the overall
purpose of the algorithm is to calculate the sum of all the inputs.
For instance, suppose that the addition algorithm incorrectly initialises counter to 1 instead of 0:
START
result = 0
counter = 1
No
Is counter < 5?
Yes
counter = counter + 1
p Figure 3.21 Incorrect solution for the problem of adding five numbers
54
Module 3: Abstraction and Algorithms
When we perform a dry run with test data of 10, 20, 30, 40 and 50, we get the trace table as
shown in Table 3.5.
Table 3.5 Trace table using an incorrect algorithm for the problem of adding five numbers
10
10
20
30
30
60
40
100
100
From the trace table, we can see that only four of the five inputs are actually used and that the
algorithm ends earlier than expected to give the wrong output of 100 instead of 150.
Recall that counter is supposed to keep track of how many inputs have been processed so far.
Thus, we can easily determine that initialising counter to 1 is incorrect since no inputs have been
processed at the start of the algorithm.
You will learn more about other methods to correct algorithm errors in Chapter 6.
55
How Do I Use Flowcharts and Pseudo-Code?
It is also possible to change the instruction in the decision symbol from “Is counter < 5?” to “Is
counter < 6?” in order to correct the algorithm. However, this is not a good programming practice
as counter is supposed to keep count of the number of times the loop has been repeated. Changing
the instruction causes counter to be incremented from 1 to 6 instead of from 0 to 5. This does
not follow the original meaning of counter and makes it harder to describe in plain English what
the variable is keeping track of.
START
result = 1
INPUT x
No
Is x != 0?
Yes
x=x–1 STOP
a) Draw and complete a trace table for this algorithm using test data of 3.
b) Draw and complete a trace table for this algorithm using test data of 5.
c) Describe the purpose of this algorithm.
56
Module 3: Abstraction and Algorithms
3.4 Pseudo-Code
Besides flowcharts, algorithms can also be represented using pseudo-code. For instance, the
pseudo-code below is another way of representing our solution to the problem of adding five
numbers:
result = 0
counter = 0
WHILE counter < 5
INPUT x
result = result + x
counter = counter + 1
ENDWHILE
OUTPUT result
p Figure 3.23 Representing the solution to the problem of adding five numbers using pseudo-code
There are many ways to write pseudo-code. The pseudo-code presented in this textbook is one
way of writing it. In other books, the pseudo-code may appear differently.
3.4.1 Input/Output
You have learnt that the input and output requirements are critical factors in deriving an algorithm.
When using pseudo-code, the INPUT command is used to receive data from outside the algorithm
and the OUTPUT command is used to produce a result from within the algorithm. These statements
serve a similar purpose as the data symbol in a flowchart.
57
How Do I Use Flowcharts and Pseudo-Code?
For example, a user is prompted to enter his or her name before a greeting with the user’s name
is produced:
INPUT name
OUTPUT "Hello " + name
START
INPUT name
STOP
58
Module 3: Abstraction and Algorithms
For example, Figures 3.26 and 3.27 represent the same algorithm. In this case, the person or
computer following the algorithm chooses whether to output “Yes” or “No” depending on whether
text matches the special phrase “P@55w0rd”.
INPUT text
IF text == "P@55w0rd" THEN
OUTPUT "Yes"
ELSE
OUTPUT "No"
ENDIF
START
INPUT text
Is text == “P@55w0rd”?
Yes No
STOP
In pseudo-code, certain steps are indented to show that they are part of the selection construct.
Note that the word ELSE and the second set of steps are optional. If they are left out and the
answer to the question is “No”, then the steps after the ENDIF line (if any) are followed immediately.
59
How Do I Use Flowcharts and Pseudo-Code?
Sometimes the set of steps after the ELSE line is another IF...THEN...ELSE...ENDIF command.
In such cases, the two IF...THEN...ELSE...ENDIF commands can be combined into a single
IF...THEN...ELSEIF...ELSE...ENDIF command. For instance, Figures 3.28, 3.29 and 3.30
all represent the same algorithm where the output is "Blank" if text happens to be empty.
INPUT text
IF text == "" THEN
OUTPUT "Blank"
ELSE
IF text == "P@55w0rd" THEN
OUTPUT "Yes"
ELSE
OUTPUT "No"
ENDIF
ENDIF
p Figure 3.28 Example where the set of steps after ELSE is just another
IF...THEN...ELSE...ENDIF command
INPUT text
IF text == "" THEN
OUTPUT "Blank"
ELSEIF text == "P@55w0rd" THEN
OUTPUT "Yes"
ELSE
OUTPUT "No"
ENDIF
60
Module 3: Abstraction and Algorithms
START
INPUT text
Is text == “”?
Yes No
OUTPUT “Blank”
Is text == “P@55w0rd”?
Yes No
STOP
1. WHILE...ENDWHILE
2. FOR...NEXT
3.4.4.1 WHILE...ENDWHILE
The WHILE...ENDWHILE commands are used to repeat a set of steps as long as a condition that
is provided on the same line as the word WHILE is true. The condition is checked the first time
the WHILE command is encountered and each time the set of steps is completed.
The lines between WHILE and ENDWHILE are the set of steps to repeat, and if the condition is
found to be false, the steps after the ENDWHILE line (if any) are followed instead.
61
How Do I Use Flowcharts and Pseudo-Code?
For example, Figures 3.31 and 3.32 represent the same algorithm. In this case, we wish to keep
performing the INPUT command as long as name is a blank phrase to ensure that a non-blank
name is provided. The INPUT command is thus placed between the WHILE and ENDWHILE lines.
name = ""
WHILE name == ""
INPUT name
ENDWHILE
OUTPUT "Hello " + name
START
name = “”
Yes
INPUT name Is name == “”?
No
STOP
3.4.4.2 FOR...NEXT
The FOR...NEXT commands are used as a shortcut for repeating a set of steps using a counter
or when the number of repetitions is known beforehand. The FOR command goes through the
values in a sequence using a variable and repeats the set of steps between FOR and NEXT for
each value. After the set of steps has been repeated for each value in the sequence, the algorithm
continues with the steps that follow after the NEXT command (if any).
62
Module 3: Abstraction and Algorithms
For example, Figures 3.33 and 3.34 represent the same algorithm. In this case, we use the variable
x to repeat an OUTPUT command while x is incremented in whole numbers from 1 to 5. Each
time the NEXT command is encountered, the person or computer following this algorithm checks
whether there is a next value for x, and if so, updates x to the next value before repeating the
OUTPUT command again. Once there are no further values for x, the OUTPUT command is no
longer repeated.
FOR x = 1 to 5
OUTPUT x
NEXT x
START
x=1
x=x+1
Yes
Is x < 5? OUTPUT x
No
STOP
1. Choose an appropriate construct and write the pseudo-code using this construct to represent an
algorithm that takes in a whole number from 1 to 7 inclusive and produces the corresponding
number in words (for example, the input 5 produces the output “five”). Specify the input and
output requirements for the problem that this algorithm solves.
2. Choose an appropriate iteration construct and write the pseudo-code to represent an algorithm
that takes in 10 numbers and outputs the average of the 10 numbers. Specify input and output
requirements for the problem that this algorithm solves and complete a trace table using the
following test data: 20, 17, 19, 65, 20, 18, 19, 66, 20 and 19.
63
How Do I Use Flowcharts and Pseudo-Code?
Chapter Summary
WHILE... FOR...
IF...THEN... ENDWHILE NEXT
ELSEIF...
ELSE...ENDIF
PSEUDO-CODE
FLOWCHARTS AND
PSEUDO-CODE
Dry Runs
Symbols Structuring
Flowcharts
Producing a
Trace Table Terminator
Sequence
Data
Selection
Correcting
an Algorithm
Decision
Iteration
Process
64
Module 3: Abstraction and Algorithms
Review Questions
2. The temperature in a server room should be kept between 18 °C and 20 °C for the servers
to function optimally. This is done by a computer continually checking the temperature.
When the temperature exceeds 20 °C, the air conditioner is switched on. When the
temperature falls below 18 °C, the air conditioner is switched off. Otherwise, the air
conditioner’s state is unchanged.
Express the algorithm as a flowchart. (Note that unlike normal flowcharts, this flowchart
will not have an end terminator symbol.)
3. Table 3.6 shows the exchange rate between Singapore Dollars (SGD) and Malaysian Ringgit
(MYR) as well as between SGD and Chinese Yuan (CNY).
1 3 4.66
65
How Do I Use Flowcharts and Pseudo-Code?
4. The pseudo-code in Figure 3.35 takes in the type of co-curricular activity (“Club/Society”,
“Sport” or “Uniformed Group”) for five students, and then outputs the number of students
in each type of co-curricular activity.
1 Count = 0
2 ClubSociety_Count = 0
3 Sport_Count = 0
4 UniformedGroup_Count = 0
5
6 WHILE Count <= 5
7 INPUT Type
8 IF Type == "Club/Society" THEN
9 ClubSociety_Count = ClubSociety_Count + 1
10 Count = Count + 1
11 ELSEIF Type == "Sport" THEN
12 Sport_Count = Sport_Count + 2
13 Count = Count + 1
14 ELSEIF Type == "Uniformed Group" THEN
15 UniformedGroup_Count = UniformedGroup_Count + 1
16 Count = Count + 1
17 ELSE
18 OUTPUT "Invalid Option"
19 ENDIF
20 ENDWHILE
21
22 OUTPUT "Clubs and Societies: " + ClubSociety_Count
23 OUTPUT "Sports: " + Sport_Count
24 OUTPUT "Uniformed Groups: " + Sport_Count
Locate and correct three errors in the pseudo-code. Line numbers have been provided for
your reference. You may also wish to create some test data and use trace tables to find out
where these errors are located.
66
Module 3: Abstraction and Algorithms
5. The following pseudo-code in Figure 3.36 takes in seven numbers and calculates the sum
and average of these numbers as the outputs.
Count = 0
Sum = 0
WHILE Count != 7
INPUT Num
Sum = Sum + Num
Count = Count + 1
ENDWHILE
p Figure 3.36 Pseudo-code to calculate the sum and average of seven numbers
Given the following test data, draw and complete a trace table. (Provide values of up to 2
decimal places where necessary.)
67
How Do I Use Flowcharts and Pseudo-Code?
6. The following flowchart takes in a sequence of non-zero integers as input. The input ends
when a zero is detected.
START
i=0
INPUT numbers[i]
Is numbers[i] == 0? Yes
No
No
i=i-1
68
4 Module 4: Programming
When writing algorithms for computers, we need to consider that computers require
algorithms to be expressed more precisely than humans would. This chapter will introduce
the Python programming language and how we can implement algorithms using a
computer.
69
How Do I Write Programs?
• Use and justify the use of variables, constants and simple lists in different problem
contexts.
• Understand and use different data types (integers, floating-point numbers, strings,
Booleans, lists) and built-in functions in programs.
• Understand and use sequence, selection and iteration constructs to create a program.
Just as flowcharts present a set of activities and decisions for a person to understand, programs
list a set of instructions for a computer to follow. However, computers do not have the same
common sense and knowledge of natural languages (such as the English language) that humans
have, so without additional information, they can only understand a very limited set of instructions.
For instance, a flowchart meant for people to calculate the total score of a test may have the
following instruction:
Without more information, a computer would not be able to understand how to carry out this
instruction. Some examples of additional information needed would be:
• The meaning of “test scores”: What type of data is “test scores” and how are they stored
in (or received by) the computer?
• The meaning of “all”: How many test scores are there, or how do we know when the sum
is complete?
70
Module 4: Programming
For a computer to follow the instruction, it must be decomposed and re-written into steps that
the computer can understand. For instance, if the test scores are stored in processor registers 0,
1 and 2, and we want the computer to store the sum of the test scores back in processor register
0, we can write a set of instructions as shown in Figure 4.2.
71
How Do I Write Programs?
Machine
English Python
code
Instructions written in a programming language are called source code, source, or even just code
for short. The following is an example of Python source code:
Source code is easy for humans to understand. For instance, you can infer from Figure 4.5 that
total is the sum of test_score_1 and test_score_2.
72
Module 4: Programming
When source code does not follow the rules of the Syntax error
language, it results in a syntax error. This type of mistake Result of incorrect source code
is detected by the translator program and the incorrect that does not follow the rules of
source cannot be translated into machine code. Therefore the language
syntax rules must be followed when writing source code.
Besides Python, there are a variety of other programming languages. Table 4.1 shows some
examples of source code in other programming languages such as C, Pascal, Scratch and Lisp.
Programming
Source code
language
In this example, the C and Pascal source codes look very similar to Python source code but
Scratch and Lisp source codes are quite different. It is sometimes useful to recognise that the same
algorithm can be represented in a variety of ways depending on the programming language used.
Unlike most other programming languages, the source code in Scratch is not typed out but assembled
by dragging and dropping graphical blocks. Scratch is a project of the Lifelong Kindergarten Group
at the MIT Media Lab.
73
How Do I Write Programs?
We have learnt that only machine code can be run directly on a computer’s processor. When
we use a programming language, the source code must be translated into machine code before
it can be run.
p Figure 4.6 A translator translates the source code into machine code
C and Pascal are examples of programming languages that typically use a compiler. Scratch
and Python are examples of programming languages that typically use an interpreter. Lisp is an
example of a programming language where both compilers and interpreters are commonly used.
74
Module 4: Programming
Each translation method has its own advantages and disadvantages, which are summarised in
Table 4.2.
Compilers Interpreters
program is running.
• Compilers usually do not offer
an interactive mode. • The interpreter needs to be run
every time the program is
started.
Note that there are two slightly incompatible versions of Python that are commonly used today,
namely Python 2 and Python 3. In this textbook, we will only use Python 3.
75
How Do I Write Programs?
76
Module 4: Programming
IDLE is an example of an “integrated development environment” (IDE) that makes it easier to use
Python by providing an “all-in-one” program for editing, running and testing source code. The
name IDLE officially stands for “Integrated Development and Learning Environment”, but it is also
meant to honour Eric Idle, a member of the British comedy group Monty Python from which
Python got its name.
Python’s interactive mode is where you can enter source code, line by line, for the interpreter to
run immediately. This mode is useful for learning and experimenting with Python source code.
For instance, try entering the following line of Python source code in the shell window and
pressing Enter:
Note that for the interactive mode examples given in this textbook, anything that you may need
to type will be shown in bold. Any text that is not in bold is output from either your program
or the interpreter and you will not need to type them in, such as the >>> prompt displayed by
Python. In addition, you will usually need to press Enter after typing each line of code.
As Python uses an interpreter, no compilation step is needed and the source code is run
immediately. If there are no errors, you should see the phrase “Hello, World!” displayed on
the screen:
77
How Do I Write Programs?
If there is an error, Python will display an error message, such as the example in Figure 4.11.
Note that the Python programming language is case-sensitive and that “Print” (with an upper-case
“P”) is not the same as “print” (with a lower-case “p”), as shown by the error in Figure 4.11. It
is typical to encounter such errors when learning a new programming language. You will learn
more in Chapter 6 on what to do if a program does not work as intended.
Besides using the interactive mode, you can also save your Python programs as plain text files
with the .py file extension. You can write your Python programs using Notepad or any other
text editor.
To use IDLE as a text editor, go to the shell window, open the “File” menu, and select “New File”.
A new editor window should open for you to enter your Python program. To save your Python
program as a file, go to the editor window, open the “File” menu, and select “Save”. If you are
working on a school computer, your teacher will inform you of where your programs should be
saved. For this textbook, we will save our programs in a folder named C:\Examples.
1 print("Hello, World!")
To run the program, save the file by pressing Ctrl+S, then press the F5 key while the file is open
in IDLE. The phrase “Hello, World!” will be displayed in the shell window:
78
Module 4: Programming
Note that, unlike in interactive mode, the result for each line of source code is not automatically
displayed when running a Python program that is saved in a file. To display text on the screen
when running a Python program that is saved in a file, you must use the print() function (see
section 4.6.1 on functions).
Even with the addition of comments, this program still behaves exactly like the original program,
as shown in Figure 4.15:
The rest of this chapter covers the various types of instructions in Python that computers can use
to perform algorithms.
79
How Do I Write Programs?
variable_name = value
80
Module 4: Programming
Typically, identifiers in Python are written in lower case with words separated by underscores.
To make our source code easier to read, we should follow the same naming style.
Unlike flowcharts and pseudo-code which are meant for humans, syntax rules for Python
instructions must be followed strictly or else the computer will not be able to perform the
instruction. For example, the code in Figure 4.18 assigns the value 2017 to a variable with the
name year:
We can visualise what this does by imagining a sticky note with label “year” pasted on or attached
to the location or address where 2017 is located in memory:
45647424 2020
46547432 2019
46547456 2018
46547464 1965
81
How Do I Write Programs?
Unlike variables in flowcharts and pseudo-code that act like boxes, variables in Python act like
sticky notes attached to locations in memory. For most of the tasks in this textbook, you need not
take note of this difference. However, this will be important to understand when using advanced
features of Python not covered in this textbook. In general, you should be aware that different
programming languages will have slightly different rules for how variables work.
In most versions of Python, you can get the memory address that a variable is attached to by using
the id() function, as shown in Figure 4.20:
Of course, the memory address that is printed out will vary from computer to computer depending
on which memory addresses are available to Python. Do not be alarmed if your computer does
not produce the same output as your friend’s!
We call the name year a variable because we can change (i.e., vary) the value that is assigned
to it while the program is running.
82
Module 4: Programming
46547440 2017
46547448 −12
46547456 2018
46547464 1965
p Figure 4.22 Changing the assigned value of the variable year from 2017 to 2020
In assigning values, what comes after the equals sign is not limited to a number. It can include
calculations or even other variables. For example, the following source code creates a new value
(the sum of 1 and 2) and attaches a variable named total to its address:
>>> total = 1 + 2
Note that when one variable is assigned to another variable, both variables become attached to
the same address. You can imagine this as moving the sticky note for one variable onto the same
location as another variable. For instance, let us assign different values to two variables:
45647424 2020
46547432 2019
46547456 2018
year
_ 2 46547464 1965
46547472 "Hello, World!"
83
How Do I Write Programs?
If we take year_1 and assign it to year_2, we can imagine the sticky note for year_2 being
moved over to the location of the sticky note for year_1, so both year_1 and year_2 now refer
to the same value. This is illustrated in Figures 4.26 and 4.27:
45647424 2020
46547432 2019
year
_ 2 year_1 46547440 2017
46547448 −12
46547456 2018
46547464 1965
p Figure 4.27 year_1 and year_2 now refer to the same value
To display the value of a variable on the screen, use the print() function as follows:
>>> print(variable)
For instance, in interactive mode, entering print(year) would print out the value assigned to
the variable named year:
84
Module 4: Programming
However, if the name year does not correspond to any variable that was used or defined
previously, Python will produce an error message instead:
>>> print(year)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
print(year)
NameError: name 'year' is not defined
These instructions are translated by the Python interpreter into machine code. This machine
code takes care of finding where values are located in memory or if additional space in memory
is needed to store a value. The Python interpreter handles these tasks so that programmers can
focus on the algorithm.
We can also take the value from a variable, manipulate it, and then assign it back to the same
variable. This is because Python will always fully calculate the value of the right-hand side before
assigning it to the variable on the left-hand side:
p Figure 4.32 Using the value of year to assign a new value back to year
In the second line of the source code in Figure 4.32, Python calculates the value of year + 1 using
the original value of year before assigning the final result (2018) back to year. The old value of
2017 will no longer be referred to. This technique is often used to update the value assigned to a
variable without having to create additional variables to store the interim results.
85
How Do I Write Programs?
4.4.5 Constants
Programs often use one or more fixed values over and over again. For instance, let us consider
a school in which the maximum score for all tests is always 30 and the number of students in
each class is always 40. Suppose we want to find out the percentage score of one student and
the overall percentage score of a whole class. We can use the following source code to calculate
these scores:
The above source is valid, but using the raw values of 30 and 40 in this manner has two
disadvantages:
1. It is not clear what the values 30 or 40 mean from just the source code.
2. There is no clear indication that the value of 30 in both lines of source refers to the same
thing (that is, the maximum test score). If the maximum test score changes, someone
updating the code may change one line correctly but forget about the other line.
86
Module 4: Programming
For example, we can use constants to provide more readable names for the raw values of 30
and 40:
MAX_SCORE = 30
CLASS_SIZE = 40
Using these constants, the source code becomes more readable and easier to maintain should
there be any changes required in the future:
Many systems work because most people follow the same norms or “unwritten laws”. These are
called conventions. For instance, in Singapore, it is conventional to keep left on the escalator so
that there is a clear path on the right for those in a hurry.
In the same way, Python programs have a set of conventions that we should try to follow. An
example of these conventions is using upper-case letters to name constants.
87
How Do I Write Programs?
2. The source code in Figure 4.38 can be entered line by line, in the order given, using
Python’s interactive mode. For each line of source code, predict what will be displayed
on the screen (if any) or if an error will occur. Check your answers by entering the same
source code, line by line, in IDLE’s shell window and observing the results.
1 name= "Computing"
2 name name
3 Name = "Science"
4 print(name)
5 print(Name)
6 Real_Name = name
7 Real_Name
8 print(Real_Name)
9 Fake_Name =
88
Module 4: Programming
4. Identify three different values in the program in Figure 4.39 that should each be
represented by a constant. Suggest suitable names for each constant, and then modify
the program to use the constants with the names you have chosen. The modified
program should behave identically to the original program.
# Program 4.3 greetings_with_title.py
Data in a computer is stored as a sequence of bytes. The same sequence of bytes can represent
different types of data such as numbers or text, depending on how the sequence is interpreted.
However, we do not need to worry about how the bytes are arranged when assigning values to
or reading values from variables. Instead, Python manages the arrangement and interpretation of
the bytes in each variable. This is done by associating each value and variable with a data type.
Some built-in data types in Python are shown in Table 4.4:
89
How Do I Write Programs?
4.5.1 Integers
An integer (int for short) is a whole number. To represent an int value directly in Python source,
use its literal format, that is, the digits of the int written out:
>>> 2017
2017
Since many algorithms involve counting and manipulating whole numbers, ints are handled
as a special data type in Python to ensure accuracy. Python ints can handle almost any whole
number, positive or negative, large or small, without losing accuracy. This is different from some
programming languages where there is a limit to the most positive or most negative integer that
can be represented by an integer type. For instance, some programming languages would not be
able to perform the following calculation of 2 to the power of 210 correctly:
>>> 2 ** 2 ** 10
1797693134862315907729305190789024733617976978942306572734300811577
3267580550096313270847732240753602112011387987139335765878976881441
6622492847430639474124377767893424865485276302219601246094119453082
9520850057688381506823424628814739131105408272371633505106845862982
39947245938479716304835356329624224137216
The ** symbol here represents exponentiation. This and other mathematical operators will be
discussed in section 4.6.2.2.
90
Module 4: Programming
>>> 2017.0
2017.0
>>> 2e3
2000.0
Due to the format in which floating-point values are stored, floats are not especially accurate,
as shown in Figure 4.43:
There is also a limit to the most positive and most negative number that can be represented
using a float. In most versions of Python, the most positive number that can be represented
is approximately 1.79 × 10308 while the most negative number that can be represented is
approximately −1.79 × 10308. Beyond this limit, the float gets converted into the special value of
infinity (represented by inf or –inf):
>>> 1.79e308
1.79e+308
>>> 1.80e308
inf
>>> -1.79e308
-1.79e+308
>>> -1.80e308
-inf
4.5.3 Strings
Python uses the string (str for short) data type to represent
text. The name “string” is shorthand for the phrase “string of Key Term
characters/letters”. String literals consist of the text contents
enclosed by matching single-quotes (') or double-quotes String (str)
("). In addition, the enclosing quotes must be entered as Data type to represent text
straight quotes (like ' or ") and not curly quotes (like ’ or ”); as a sequence of characters
otherwise a syntax error will occur. or symbols
91
How Do I Write Programs?
Note that in Python’s interactive mode, if a str is entered directly as a literal, it is printed with
additional single-quotes around the content. To display only the content of the str without the
quotes, use the print() function instead, as shown in Figure 4.46:
p Figure 4.46 Using the print() function to display strs without additional single quotes
Often, strs may need to include characters that are either difficult to type out as part of a literal
or would cause a syntax error if not treated specially. To include such characters, we need to
input them using the backslash (\) key and special escape codes. Some common escape codes
for string literals are shown in Table 4.5:
Note that the escape code “\” represents a backslash that is the last character on a line of
source code. This trailing backslash means to ignore the line break and treat the following line
as if it continues on the current line.
92
Module 4: Programming
Figure 4.47 shows some examples of how escape codes can be used:
>>> print("123")
123
>>> print("123" + 1)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
print("123" + 1)
TypeError: Can't convert 'int' object to str implicitly
>>> print(type("123"))
<class 'str'>
>>> print(int("123"))
123
>>> print(int("123") + 1)
124
>>> print(type(int("123")))
<class 'int'>
p Figure 4.48 Using the int() function to convert a str into an int
93
How Do I Write Programs?
Similarly, the str() function can convert ints and floats to strs. In the following example,
an error occurs as the “+” operator expects both the value on its left and the value on its right
to be of the same type. To fix this error, we use the str() function to convert number into a
str so that both values on the left and right are strs.
p Figure 4.49 Using the str() function to convert an int into a str
The use of the “+” operator to join strings together will be discussed in section 4.6.2.3.
4.5.4 Booleans
Boolean (bool for short) is a special data type used by Python to represent logic-related
data. Unlike other data types, there are only two valid values that a bool variable can have
assigned to it: True or False.
94
Module 4: Programming
Booleans are named after George Boole, an English mathematician, philosopher and logician,
who introduced the use of Boolean logic, a form of algebra where values or variables are either
“true” or “false”, in 1847.
4.5.5 Lists
Programs often need to keep and manipulate multiple
values without prior knowledge of the total number Key Terms
of values. The list data type can be used to store a
sequence of values of any length. Array
Sequence where all the items
The format for list literals is a left square bracket ([), have the same size and are
followed by the items of the list (if any) separated by arranged consecutively in
commas, and ending with a right square bracket (]). The memory; informally also used
items in the list can be of any type, although mixing as a synonym for list
types in a list is not a good practice.
List (programming)
Figure 4.51 shows some examples of valid list literals, Data type for storing multiple
including an empty list. values in a sequence
>>> [1, 2, 3]
[1, 2, 3]
>>> ["Mixing types in a list is bad.", 2017, False]
['Mixing types in a list is bad.', 2017, False]
>>> [] This is an empty list.
[]
A simple list is sometimes called a one-dimensional array. However, a list can only be called
an array if all the items in the list have the same size and are arranged consecutively in memory.
When we use flowcharts or pseudo-code to work with multiple values, we need to either input
the total number of values at the start, or wait for a special “end value” to be provided so that
we know when to stop accepting values. While both these methods still work in Python, lists
allow for another way to process multiple values by having a single variable represent a collection
of values.
You will learn how to manipulate items in a list using the index and slice operators in
section 4.6.2.3.
95
How Do I Write Programs?
Being inconsistent with the data types can lead to programming mistakes because it becomes
difficult to predict the type of the value for each variable. It is thus good programming practice
to avoid changing the data type of a variable once it has been initialised with a value.
You can find out the data type of any value by using the type() function. Python will then display
the data type of any value that is placed between the parentheses. For example, the following
output means that the data type of 2017 is an int:
>>> print(type(2017))
<class 'int'>
p Figure 4.52 Using the type() function to display the data type
a) -2017
b) '"'
c) 'Computing'
d) 3.14159
e) "\""
f) "\"[]\""
g) True
h) 6.02e23
i) '\''
j) "2017"
k) "'"
l) [""]
m) 'False'
96
Module 4: Programming
2. For each line of source code in Figure 4.53, predict what will be displayed on the
screen (if any) or if an error will occur. Check your answers by entering the same
source code, line by line, in IDLE’s shell window and observing the results.
1 print(1e100)
2 print(103E1)
3 print(1EE1)
4 print(3.141)
5 print(3.1.3)
6 print("Hello,\
World")
7 print("Hello,\nWorld")
8 print("Hello,\tWorld")
9 print('Hello,\n\tWorld')
10 print('Hello,\t\nWorld')
11 print("\\\"")
12 print("\\\")
13 print(True)
14 print(true)
15 print(False)
16 print(false)
17 print(None)
18 print(none)
19 print([])
20 print([2, 3, 4])
21 print([[]])
3. For each line of source code in Figure 4.54, predict what will be displayed on the
screen (if any) or if an error will occur. Check your answers by entering the same
source code, line by line, in IDLE’s shell window and observing the results.
1 print(int("-3"))
2 print(float('-3'))
3 print(float("minus three"))
4 print(str(-3))
5 print(str(-3.0))
6 print(str(-3e3))
97
How Do I Write Programs?
Function
4.6.1 Functions Set of instructions assigned to a
name that can be used again later
Just as variables can store values such as numbers and
lists, functions can be thought of as a way of storing Function call
instructions to be used again later. Process of assigning arguments
to new variables and running the
You have already seen functions used in some previous instructions assigned to a function
examples. For example, in Figure 4.55, the print()
function takes in a str value, called an argument,
and displays it on the screen without quotes around
the content:
When using a function, type its name, followed by parentheses that can either be empty or
contain comma-separated arguments (as shown in Figure 4.56). When a function is used, the
Python interpreter will assign any arguments to new variables before running the instructions that
are stored in the function. This process is also known as a function call.
function_name()
function_name(argument_1)
function_name(argument_1, argument_2)
98
Module 4: Programming
Figure 4.57 shows some examples of function calls using the print() function.
>>> print()
The return value (or result) of one function call can be used as an argument for another function
call. This is called a nested function call.
99
How Do I Write Programs?
The code can get difficult to read if there are too many nested function calls. For such cases,
it is recommended to break the instruction into smaller pieces by assigning return values to
intermediate variables before performing the outermost function call. For instance, the code in
Figure 4.60 gives the same result as the nested function call in Figure 4.59.
p Figure 4.60 Breaking an instruction into smaller pieces instead of using a nested function call
Some common built-in functions in Python and their typical arguments are described in
Table 4.6. Note that some functions such as range() will behave differently depending on
the types of arguments they receive. Each valid combination of arguments is described in a
separate row.
input() A str to A str containing Used to get input from the user
use as a text that the user
>>> name = input("Enter name: ")
prompt enters in response
Enter name: Samantha
to the prompt
>>> print(name)
Samantha
100
Module 4: Programming
len() A sequence, An int equal to Used to get the length of a sequence, such as
such as a the length of the a list or str
list or str argument
>>> test_scores = [72, 68, 85]
>>> print(len(test_scores))
3
101
How Do I Write Programs?
If you ever need help with a built-in function, you can use the help() function to print out its
documentation. For instance, to learn more about the len() function, enter the following in
Python’s interactive mode, taking care to enter len without the trailing parentheses:
>>> help(len)
Note that some built-in functions may use advanced features that are not covered in this textbook.
If we run define_hello.py, nothing appears on the screen because hello() is never actually
called. In order to use a UDF, you need to call it. This process is known as a function call (see
section 4.6.1) and can only be performed after the UDF is defined.
102
Module 4: Programming
The program say_hello.py in Figure 4.63 defines the same function hello(), then calls the
hello() function three times. In this program, the indentation after line 1 indicates that lines 2
and 3 belong to the hello() UDF’s body while lines 5 to 7 do not. Also, if you place lines 5 to
7 before lines 1 to 3, running the program will produce an error.
1 def hello():
2 # Say hello
3 print('Hello, World!')
4
5 hello()
6 hello()
7 hello()
When you run this program, the phrase “Hello, World!” should appear on the screen three times,
as in Figure 4.64, once for each call of hello() from lines 5 to 7:
Hello, World!
Hello, World!
Hello, World!
For instance, the program quiz.py in Figure 4.65 has a Signature (programming)
UDF get_answer() that expects to receive two arguments First line of a UDF that
using the parameters prompt and reply. This means that specifies the UDF’s name
when the function is called (but before the function body and its parameters
is run), the first argument will be assigned to a new variable
named prompt and the second argument is assigned to a
new variable named reply.
103
How Do I Write Programs?
p Figure 4.65 Defining a UDF that accepts arguments and returns a value
From Lines 6 to 8 of quiz.py, get_answer() is called three times and the return value of each
call is assigned to variables answer1, answer2 and answer3 respectively.
Figure 4.66 illustrates the process of how the arguments of a function call are assigned to new
variables with names given by the function’s signature and how the return value is assigned to
answer1.
p Figure 4.66 How arguments are assigned to new variables during a function call
104
Module 4: Programming
Figure 4.67 summarises the syntax rules for a UDF that accepts either no arguments, one argument
or two arguments. This pattern can be extended to write UDFs that accept even more arguments.
def function_name():
commands to run when function_name is called
def function_name(parameter_1):
commands to run when function_name is called (needs 1 argument)
The syntax rules for return are summarised in Figure 4.68. The return value after the return
keyword is optional. If it is provided, the return value can be of any data type. If it is omitted, the
return value is treated as None.
return
return return_value
105
How Do I Write Programs?
For instance, the program area.py in Figure 4.69 has a UDF named area_of_circle() that,
when called, will assign its argument to a variable named radius. Inside the function, a new
variable named area is also created:
1 PI = 3.14159
2
3 def area_of_circle(radius):
4 area = PI * radius ** 2
5 # Both radius and area are valid variables here
6 return area
7
8 print(area_of_circle(2))
9 # However, radius and area are NOT valid variables here
In this case, both radius and area are local variables for
area_of_circle(). This means that outside the function
Key Term
body of area_of_circle(), radius and area are not
defined. For example, commands like print(radius) and Global variable
print(area) would run correctly inside the function body Variable that is created
(e.g., line 5), but the same code would produce an error outside of a UDF and is
message outside the function body (e.g., line 9). readable from the UDF’s
body if its name is not
Notice that area_of_circle() uses the constant PI hidden by a local variable
that is defined outside of the function body. Besides local
variables, the code inside a function body also has read
access to the global variables, constants and functions that are defined outside of the function.
The different local as well as global variables and constants found in area.py are illustrated in
Figure 4.70:
Global variables
and constants
PI = 3.14159 PI
print(area_of_circle(2))
Global variables
# However, radius and area are NOT valid variables here and constants
PI
106
Module 4: Programming
In a typical program, constants and functions are defined once and do not change. Hence, it is
usually predictable to use global constants and call global functions such as input(), print()
or other UDFs inside a function body.
Variables are different from constants and functions as they can change while a program is running.
A function that depends on global variables may produce completely different behaviour each
time it is called. To avoid this, UDFs should request for all the inputs that they need as arguments
instead of using global variables. For instance, the program add_function.py in Figure 4.71
shows two ways of writing a function to add two numbers. In general, the good_example()
approach is better than the bad_example() approach as all the inputs that can affect the return
value are clearly specified in the function’s signature and the function body does not use any
global variables.
For add_function.py, the variable names num1 and num2 are used in two different contexts:
1. On lines 1 and 2, they are local variables for the good_example() function.
2. On lines 5, 7 and 8, they are global variables.
While they have the same names, it is important to understand that the local variables named
num1 and num2 on lines 1 and 2 are completely separate variables from the global variables
named num1 and num2 on lines 5, 7 and 8.
When good_example() is called on line 9, two new local variables are created and given the
names num1 and num2 just before the function body on line 2 is run. This temporarily hides the
global variables that are also named num1 and num2 and prevents them from being accessed
inside the body of good_example().
107
How Do I Write Programs?
However, both global variables are merely hidden and not lost. After the call to good_example()
ends, the local versions of num1 and num2 are removed and the global variables that are named
num1 and num2 become accessible again with the same values that were assigned to them
previously.
Figure 4.72 illustrates the local and global variables in add_function.py and how having a
local variable can hide access to global variables with the same name.
Local Variables
variables Global variables
def good_example(num1, num2): radius
num1 num1 (hidden)
return num1 + num2 area
num2 num2 (hidden)
Notice how the global variables named num1 and num2 are hidden for good_example() but are
not hidden for bad_example().
• Code that is repeated in different locations of the program can be put in UDFs. The
repeated code can then be replaced with shorter function calls.
• The solution to each sub-problem can be placed in a separate UDF using the modular
method of decomposition (see Chapter 2). Each UDF will be smaller than the main program
and thus can be more easily understood.
• When working in a team, different programmers can be given responsibility for different
UDFs so they can all work concurrently.
• Each UDF can be tested separately and more thoroughly with different inputs compared
to a single large program with no UDFs.
108
Module 4: Programming
4.6.2 Operators
You have already seen operators used in many of our previous examples. For instance, the
addition operator (+) takes two values, one to the left and one to the right, then returns the sum
of the two values.
>>> 1 + 2
3
Similarly, whenever we assign a value to a variable, we are actually using the assignment operator
(=), which takes the value calculated on the right and stores it in the variable on the left:
Some operators work with values of almost any type in Python. For instance, it makes sense to
ask whether two variables have same or different values, regardless of their data types. Python
can make such comparisons using the equivalence and non-equivalence operators.
Table 4.7 Relational operators that work with multiple data types
109
How Do I Write Programs?
Note that the single equals sign is for assignment and double equals sign, or equivalence, is for
testing whether two values are equal. The two operators are not the same. For instance, the equals
sign (=) does not have a return value while the equivalence operator (==) always has a bool return
value of either True or False.
The non-equivalence operator is a combination of the exclamation mark and equals sign. To better
understand why it is written this way, read the exclamation mark as “not”. In some programming
languages, the exclamation mark is often used to represent the logical NOT operation.
When using the equivalence and non-equivalence operators, the two values must usually be the
same in both content and data type to be considered equivalent.
However, numbers are treated a little differently. If an int and a float are compared with each
other, the int is converted to a float before the comparison is made. If the conversion fails, the
two values are treated as non-equivalent. Furthermore, since floats are not particularly accurate,
testing for equivalence or non-equivalence using floats may give surprising results as seen in
Figure 4.76. To avoid such issues, if values can be easily represented using ints instead of floats
(such as by storing prices in cents instead of dollars), it is generally a good practice to do so.
p Figure 4.76 Using the equivalence operator with floats may give unexpected results
110
Module 4: Programming
Another operator that works with almost any data type is the assignment operator.
Table 4.8 Assignment operator that works with multiple data types
Mathematical operators are commonly used to operate on int and float data types. The most
familiar of these operators are those we use for performing calculations or arithmetic.
Table 4.9 Arithmetic operators that work with int and float data types
// Floor division Returns the value on the left >>> 2017 // 1e3
divided by the value on the 2.0
right, rounded down to the
nearest integer
111
How Do I Write Programs?
Note that, because float values are not as accurate as int values, any mathematical operation
that mixes the two types will force the calculation to use the one with lower accuracy and
thus produce a float result. For instance, all the examples in
Table 4.9 return float values because 1e3 is a float value. Key Term
The division operator (/) is an exception to this rule as the division Floor division
of whole numbers often does not result in a whole number. In Division operation that
Python, we can divide an int with another int and get back a rounds down to the
float. If we want to perform a division that rounds down to the nearest integer
nearest integer, we would use floor division (//) instead.
While the result of floor division is always a whole number, if any of the values used in the
calculation is a float, then the result will still be a float. In such cases, you can safely use type
casting to convert the result to an int if needed.
p Figure 4.78 The floor division operator may return a float value
A common programming task is to use the value in a variable for a calculation and then assign the
result back into the variable. For convenience, Python provides additional assignment operators
to help save some typing when performing this task.
Table 4.10 Assignment operators that work with int and float data types
-= x -= a x = x - a
*= x *= a x = x * a
/= x /= a x = x / a
//= x //= a x = x // a
%= x %= a x = x % a
**= x **= a x = x ** a
112
Module 4: Programming
Since numbers are naturally ordered, ints and floats can also be used with the following
comparison operators (also known as relational operators) that return a bool depending on
whether the value on the left is larger, smaller or equal to the value on the right:
Table 4.11 Relational operators that work with int and float data types
< Less than Returns the bool value True >>> 2017 < 2017
if the value on the left is less False
than the value on the right >>> 2017 < 2018
and False if it is not True
>>> 2017 < 20.17
False
<= Less than or Returns the bool value True >>> 2017 <= 2017
equal to if the value on the left is less True
than or equal to the value on >>> 2017 <= 2018
the right and False if it is not True
>>> 2017 <= 20.17
False
> Greater than Returns the bool value True >>> 2017 > 2017
if the value on the left is False
greater than the value on the >>> 2018 > 2017
right and False if it is not True
>>> 2017 > 20.17
True
>= Greater than or Returns the bool value True >>> 2017 >= 2017
equal to if the value on the left is True
greater than or equal to the >>> 2018 >= 2017
value on the right and False True
if it is not >>> 2017 >= 20.17
True
Just as int and float are similar in that they represent numbers,
str and list are similar in that they represent a sequence Key Term
of values. However, an important difference between the two
types is that a list can contain items of any and different types Sequence
but a str can only contain characters. Examples of characters Any data type for
include the upper-case and lower-case letters from A to Z, the storing multiple values
digits 0 to 9, as well as punctuation symbols such as commas in order; usually a
and exclamation points. You will learn more about characters list, str or range
in Chapter 5.
113
How Do I Write Programs?
Table 4.12 Mathematical operators that work with str and list data types
Note that the addition (+) and multiplication (*) operators are exceptions. In general, strs and
lists do not work with mathematical operators. For instance, trying to subtract one str from
another str results in an error:
p Figure 4.79 Not all arithmetic operators work with str and list
In the previous example, Python outputs that the subtraction operator (–) does not
know what to do with values of the provided data type str (that is, it cannot support the
task given).
To perform more complex operations such as accessing or rearranging the values stored inside
strs and lists, we will need to use the index operator and the slice operator.
114
Module 4: Programming
For example, in Figures 4.80 and 4.81, using the index Index operator
operator with an index of 5 on the str subject_name Operator for returning a value
will return the value of t, which holds the position number in a sequence
of 5 in the str.
subject_name[5]
0 1 2 3 4 5 6 7 8
115
How Do I Write Programs?
subject_name[3:6]
0 1 2 3 4 5 6 7 8
subject_
na m e 'C' 'o' 'm' 'p' 'u' 't' 'i' 'n' 'g'
If the start index is left out, it is treated as 0. If the stop index is left out, it is treated as the length
of the str or list. Leaving out the stop index usually has the same meaning as making sure all
remaining values of the str or list are included in the return value.
Although not often used, the slice operator can also accept an optional third int value in the
form sequence_name[a:b:c], where a and b are the start and stop indices while c is called
the step. This extracts the sequence of values or characters positioned from the start index up to
but not including the stop index in increments of the step.
>>> print("Computing"[2:8:2])
mui
subject_name[2:8:2]
+2 +2
0 1 2 3 4 5 6 7 8
subject_
na m e 'C' 'o' 'm' 'p' 'u' 't' 'i' 'n' 'g'
As before, if the start index is left out, it is treated as 0. If the stop index is left out, it is treated as
the length of the str or list. If the step is left out, it is treated as 1.
>>> print("Computing"[:8:2])
Cmui
>>> print("Computing"[2::2])
muig
>>> print("Computing"[2:8:])
mputin
p Figure 4.86 Using the slice operator with the start index, stop index or step left out
116
Module 4: Programming
By using the index operator on the left-hand side of an assignment, we can also change the value
that is stored in a list at a particular index.
117
How Do I Write Programs?
However, trying to change the characters in a str will result in an error. This is because str is
an immutable type, meaning that once a str is created, its contents cannot be changed. For
instance, suppose we wish to change the first character of “Computing” to a K so that the string
becomes “Komputing”:
p Figure 4.89 Creating a new str to change the first letter of subject_name
The second line in Figure 4.89 creates a new str value of “Komputing” and assigns it back to
subject_name. The previous str value of “Computing” is no longer referred to. Note that a new
str has been created. This is not the same as changing characters in an existing str.
Another common task in solving problems is accessing the last value in a str or list. This can
be done with some simple index calculation by using the len() function, as in Figure 4.90.
p Figure 4.90 Accessing the last value by using the len() function
118
Module 4: Programming
If the index for an index operator is negative, Python treats it as a position counting from the
opposite end of the sequence, treating the last value as position −1, the second-to-last value
as position −2, and so on. This provides us with a shorter way to access the last value in a str
or list.
Finally, Python also provides a membership operator to test if a value is included in a str or
list. Unlike the other operators you have learnt so far, the membership operator looks like a
word (in) instead of a symbol, but it otherwise behaves the same way as other binary operators.
The membership operator returns a True result if the value on the left exists in the str or list
on the right, otherwise the result is False.
119
How Do I Write Programs?
Many operators in Python inform you of their results by returning a bool. Table 4.15 shows the
logical operators used for making decisions using this information.
and Conjunction Returns the bool value >>> True and True
True if both values True
are True and False >>> True and False
if either one or both False
values are False
p Figure 4.94 General logic statement to decide whether we can leave home based
on the weather outside and whether an umbrella is available
This statement translates very naturally into English as “we can leave home if it is not raining or
if it is raining and we have an umbrella”. Suppose it is raining and there is no umbrella available.
In this case, is_raining is True and have_umbrella is False:
120
Module 4: Programming
Python gives a result of False for leave_home, which leads us to the conclusion that we cannot
leave home yet. This is consistent with our English translation of the statement.
1. For each line of source code in Figure 4.96, predict what will be displayed on the
screen (if any) or if an error will occur. Check your answers by entering the same
source code, line by line, in IDLE’s shell window and observing the results.
1 print(len(range(3)))
2 print(len(3))
3 phrase = "Hello, World!"
4 print(len(phrase))
5 print(len([phrase]))
6 print(list(range(7)))
7 print(list(range(len("2017"))))
8 x = len(range(-2, 3))
9 print(x, x)
2. Write a Python program that asks the user to input a str phrase and outputs the
number of characters in the str that was entered.
3. For each line of source code in Figure 4.97, predict what will be displayed on the
screen (if any) or if an error will occur. Check your answers by entering the same
source code, line by line, in IDLE’s shell window and observing the results.
1 x = 2
2 x **= 3
3 x / 10
4 print(x)
5 print(1 + 1 <= "2")
6 print(3 * "2")
7 quotient = 55 // 7
8 remainder = 55 % 7
9 print(quotient * 7 + remainder == 55)
10 print(remainder + quotient * 7)
11 print((remainder + quotient) * 7)
12 55 = x
13 2.0 ** 3 == x
14 print(-55 // 10)
121
How Do I Write Programs?
4. Write a UDF with the function name area_of_triangle that takes in the base
and height of a triangle as input and gives the triangle's area as its return value.
5. Suggest what should be written in the box in the program in Figure 4.98 so that it
correctly outputs whether the number of characters in phrase is a multiple of 3.
phrase = input("Enter phrase: ")
print( )
6. Suggest what should be written in the box in the program in Figure 4.99 so that it
outputs True when both the following conditions are met (and False if either is
not met):
• The first character of name is the same as its last character.
• name does not contain a space character.
7. For each line of source code in Figure 4.100, predict what will be displayed on the
screen (if any) or if an error will occur. Check your answers by entering the same
source code, line by line, in IDLE’s shell window and observing the results.
1 data = [7, 9, 3, 5]
2 print(data[3:])
3 print(data[:1])
4 print(data + [2])
5 print(data + 2)
6 print(data[-3:-2])
7 print(data[len(data)- 1])
8 print(data[len(data)])
9 print(['A', 'B', 'C'][1])
10 ['F', 'G'][0] = 'A'
11 "FG"[0] = 'A'
122
Module 4: Programming
8. For each line of source code in Figure 4.101, predict what will be displayed on the
screen (if any) or if an error will occur. Check your answers by entering the same
source code, line by line, in IDLE’s shell window and observing the results.
1 x = 5
2 print(x > -5 and x < 5)
3 print(x >= -5 and x <= 5)
4 print(x > -5 or x < 5)
5 print(not not True)
6 print(not True and False)
7 print(not (True and False))
9. The following program uses UDFs to a prompt for a message, then prints out the
message in a box made up of a repeated symbol:
1 SYMBOL = '*'
2
3 def print_border(length):
4 border = SYMBOL * length
5 print(border)
6
7 def print_box(message):
8 left_side = SYMBOL + ' '
9 right_side = ' ' + SYMBOL
10 middle_line = left_side + message + right_side
11 print_border(len(middle_line))
12 print(middle_line)
13 print_border(len(middle_line))
14
15 message = input("Enter a message: ")
16 print()
17 print_box(message)
For each of the following appearances of variables and constants in the program,
identify whether the variable or constant being used is local or global:
a) SYMBOL on line 4
b) length on line 4
123
How Do I Write Programs?
c) border on line 5
d) SYMBOL on line 8
e) left_side on line 10
f) message on line 10
g) right_side on line 10
h) middle_line on line 12
i) message on line 17
So far, we have covered all the basic Python commands for storing, reading and manipulating
values of different data types. With these tools, we can use Python to perform some interesting
tasks. For instance, use a text editor (or Python’s built-in IDLE editor) to enter the program in
Figure 4.103 into a file and save it as message_box.py:
If you are using Python’s built-in IDLE editor, press the F5 key while the file is open. The F5 key
is used to run the Python file that is currently open.
When the program runs, it should prompt you to enter a message. Type in a short message and
press Enter. The program will then print out your message in a box made of asterisks:
*****************
* Hello, World! *
*****************
124
Module 4: Programming
START
INPUT text
Is text == “P@55w0rd“?
Yes No
STOP
125
How Do I Write Programs?
The flowchart in Figure 4.105 will output “Yes” if the input text is the same as the special phrase
“P@55w0rd”, otherwise it will output “No”. We can get the same result using the if-else
statement in Python:
Suppose that now we want to perform more commands in each branch of password.py and
for the program to output “Goodbye” just before it ends (whether or not the input text matches
the password).
126
Module 4: Programming
The amended flowchart and Python source may look like this:
START
INPUT text
Is text == “P@55w0rd“?
Yes No
OUTPUT “Goodbye“
STOP
127
How Do I Write Programs?
In Figure 4.108, we see that the line that outputs “Goodbye” is not indented. This is how Python
knows that this line is not part of the if-else statement and that it should be run after the
if-else statement is completed, no matter which branch was previously followed.
It is also possible to nest an if-else statement inside another if-else statement. For instance,
we might want to provide a separate error message if the text entered is blank. One possible
solution would be for the flowchart and Python source to look like this:
START
INPUT text
Yes
Is text == “”?
No
Yes No
OUTPUT “Goodbye”
STOP
128
Module 4: Programming
p Figure 4.110 Password checker program with detection of blank input added
Note that the if-else statements from line 5 to line 10 are indented by one additional level
compared to the rest of the program. Hence, line 5 and line 8 have one level of indentation, while
lines 6, 7, 9 and 10 have two levels of indentation. In Python, it is important to indicate which
branch each line of code belongs to.
Nested if-else statements are common. However, excessive indentation can make the code
difficult to read. To avoid this, Python provides a way to combine nested if-else statements
into a single statement so that there is no need to increase the level of indentation. For example,
password_3.py can be re-written as:
p Figure 4.111 Password checker with additional indentation avoided by using elif
129
How Do I Write Programs?
The elif keyword in line 4 of password_3_elif.py replaces the else and if in lines 4 and 5
of the original password_3.py and avoids additional indentation of the remaining code. When
reading the source code, treat elif as an abbreviation for “else if”. Besides these cosmetic
changes, this program otherwise behaves exactly the same as the original password_3.py.
Some algorithms do not require any instructions to be followed for the else portion of an
if-else statement. The else keyword should then be omitted entirely. For instance, the following
program tries to censor the word “Evil” if it is entered as part of a name:
START
INPUT name
Is “Evil” in name?
Yes
No
STOP
130
Module 4: Programming
In Figure 4.113, name has its contents replaced by a censorship notice if “Evil” is found in the
name entered by the user. Otherwise, the program continues normally by outputting the name
with no modification to its contents.
The syntax rules in Figure 4.114 summarise how the words and indentation for an
if-elif-else statement can be arranged:
if condition:
commands when condition is True
if condition:
commands when condition is True
else:
commands when condition is False
if condition_1:
commands when condition_1 is True
elif condition_2:
commands when condition_1 is False and condition_2 is True
else:
commands when condition_1 is False and condition_2 is False
131
How Do I Write Programs?
In Figure 4.115, the algorithm will keep asking for a new value of name as long as name
remains blank.
START
name = “”
Yes
INPUT name Is name == “”?
No
OUTPUT “Hello” +
name
STOP
1 name = ""
2 while name == "":
3 name = input("Enter name: ")
4 print("Hello " + name)
132
Module 4: Programming
Note that, exactly like the flowchart, Python checks the condition after the while keyword
at least once while the program is running and every time after the commands in the loop are
complete. This means that if we set name to anything other than an empty string, Python will
skip the contents of the loop.
1 name = "Computing"
2 while name == "":
3 name = input("Enter name: ")
4 print("Hello " + name)
p Figure 4.117 Demonstration that while condition is always checked at least once
133
How Do I Write Programs?
Additional lines with the same indentation belong to the same while loop. For instance, the
flowchart and program in Figures 4.118 and 4.119 go through (or iterate) the contents of a list
and output each item on a separate line.
START
index = 0
index = index + 1
Yes
Is index < length of items? OUTPUT items[index]
No
STOP
134
Module 4: Programming
while condition:
commands to repeat while condition is True
The continue keyword causes Python to skip the remaining commands in the loop and go straight
to deciding whether the loop should run again by testing the condition after the while keyword.
For example, suppose we have a loop that (by default) outputs “C is for word” for every word in
a list. However, if a word does not start with C, we can decide that the default behaviour does
not apply and that the program should advance to the next word instead.
135
How Do I Write Programs?
The flowchart and source code in Figures 4.121 and 4.122 demonstrate how such an algorithm
can be implemented using the continue keyword:
START
index = 0
Yes
No
Is first letter of
index = index + 1
words[index] “C”?
No Yes
OUTPUT “C is for” +
index = index + 1
words[index]
STOP
136
Module 4: Programming
The break keyword, on the other hand, causes Python to immediately skip the remaining
commands and exit the loop completely. For example, suppose we once again have a loop that
(by default) outputs “C is for word” for every word in a list. However, this time we want the loop
to end once a word that does not start with C is detected.
137
How Do I Write Programs?
The flowchart and source code in Figures 4.123 and 4.124 demonstrate how such an algorithm
can be implemented using the break keyword:
START
index = 0
No
Yes
Is first letter of
words[index] “C”?
No
Yes
STOP
138
Module 4: Programming
The program in Figure 4.125 demonstrates how the continue and break keywords work by
playing a simple “Simon Says” game.
1 # Constants
2 MAGIC_WORDS = "Simon says "
3 QUIT_COMMAND = "Quit"
4
5 print("Enter \"" + QUIT_COMMAND + "\" to end game.")
6 while True:
7 command = input("Enter command: ")
8 if command == QUIT_COMMAND:
9 break
10 elif command[:len(MAGIC_WORDS)] != MAGIC_WORDS:
11 print("We ignore the command")
12 continue
13 print("We " + command[len(MAGIC_WORDS):])
14 print("Thanks for playing!")
139
How Do I Write Programs?
In this game, the computer will repeat what the user enters as long as the input starts with the
words “Simon says”. This is the default behaviour that is repeated using a while loop that seems
to repeat forever as its condition is always True (line 9). However, if the input matches the special
word “Quit”, the loop ends immediately using the break keyword and the game ends with the
thank-you message. On the other hand, if the input does not start with “Simon says”, the default
behaviour is skipped and we proceed to ask for the next input instead. Try playing the game a
few times and see if the new keywords behave the way you expect them to.
Iterating through the items of a sequence is such a common task that Python provides a shortcut for
doing so: the for-in statement. When we use the for-in statement, each item in the sequence
is assigned to a variable automatically before the loop is run. For instance, we can simplify the
code shown in Figure 4.126 to become the code in Figure 4.127:
p Figure 4.127 Iterating through a list without a counter using a for-in loop
140
Module 4: Programming
Note that this new code no longer has an int counter (index). Instead it has a variable item
that is assigned a new item from the list each time the loop repeats.
The for-in loop also works with the range() function that you learnt earlier. As with lists, the
loop is run for each item in the range. By using the length of a list as the argument for range(),
we have an efficient way to iterate through a list with a counter variable.
p Figure 4.129 Iterating through a list with a counter using a for-in loop and range()
Notice that there is no longer a need to manually increase index by 1 as the for-in loop will
automatically update index with the next item in the provided sequence.
The continue and break keywords work with for-in loops as well.
141
How Do I Write Programs?
1. Write a program that lets the user input a list of strs by asking for each item of the
list in order. Receiving an empty str from the user would indicate the end of the
list. The program would then output the list on the screen.
Table 4.16 Input and output requirements for the list input problem
Input Output
2. Write a program that lets the user input a password. To make sure that the password
has been entered correctly, the program should ask for it twice. If the two entered
passwords are different, the program should output “Invalid” and repeat the input
process all over again. The program should output “Valid” and end when the two
entered passwords match.
Table 4.17 Input and output requirements for the password input problem
Input Output
142
Module 4: Programming
Chapter Summary
PROGRAMS
Variables
Functions PYTHON
and Constants
Built-In User-Defined
Functions Functions
int Multi-Type
Operators
if-elif-else
float
Mathematical
Operators
while for-in
str
Boolean
list Operators
143
How Do I Write Programs?
Review Questions
1. Let us revisit the situation at the beginning of this chapter where Alex is responsible for
finding out which loaned books are overdue in his school library
a) Alex studies the problem and decides to use int values with the digits arranged as
YYYYMMDD to represent day DD, month MM and year YYYY. Table 4.18 shows some
examples of how dates would be represented in Alex’s program.
Explain why Alex’s method of representation will simplify his program later when he
needs to compare dates.
b) The input and output requirements for Alex’s problem are provided in Table 4.19. Write
a program that processes the provided inputs and correctly prints out the titles of all
the overdue books. In your program, write and use a UDF that takes in the due date
of a book and today’s date as input arguments, then returns True if the due date has
passed and False otherwise. Assume that the input data will always be valid.
Table 4.19 Input and output requirements for the problem of overdue books using lists
Input Output
• duedates: corresponding
int representation of due dates
(via text file)
144
Module 4: Programming
2. Write a program to extract the hour, minute and second values (as ints) from a time
string that is provided in the format “HH:MM:SS”. Assume that the input data will always
be valid.
Table 4.20 Input and output requirements for the problem of reading a time string
Input Output
145
How Do I Write Programs?
3. Write a program to calculate the number of seconds between a start time and an end
time, which are both provided in the format “HH:MM:SS”. Assume that the input data will
always be valid, the end time is after the start time, and both times occur on the same day.
Table 4.21 Input and output requirements for the time interval problem
Input Output
146
5 Module 4: Programming
In the previous chapters, we learnt how to specify the required inputs and outputs for
a problem, how to develop a possible solution in the form of an algorithm, and how to
implement such algorithms using a computer by writing programs in Python.
For some problems and programs, it may not be easy to specify the inputs and outputs
or the correct algorithm to use. Hence, it can be useful to follow a more structured
approach.
In this chapter, you will learn the stages in developing a program, some built-in functions
for manipulating numbers and strings, as well as a catalogue of common problems
and solutions.
147
How Can Programs Be Used to Solve Problems?
• Understand, use and justify the use of built-in mathematical and string functions
in programs.
148
Module 4: Programming
Consider the situation in which Siti is required to develop a program to manage the loan records
and check the availability of books at the school library. She interviews the librarians in her school
to understand why a program is needed and what the program is expected to do. They give the
following responses:
• “When someone asks me if a book is available, I have to go check the bookshelf. This can
take a long time. It would be faster if I could find out the answer from the computer.”
• “This is a very small library so no two books have the same title.”
• “We only have one computer connected to a keyboard and screen. The computer has a
list of book titles and their availability in separate text files.”
From the information that she has gathered, Siti concludes that the program would need the
following inputs and output shown in Table 5.1. She has also given each input a descriptive name
to work with.
Table 5.1 Input and output requirements for the book availability problem
Input Output
• titles: list of book titles (via text file) • Whether the book in
search is available
• availability: list of availability of
each corresponding book in titles
(via text file)
149
How Can Programs Be Used to Solve Problems?
Siti notices that she can decompose the problem into three distinct tasks of obtaining input,
searching through the list of book titles and, finally, generating output of whether the requested
book is available. She illustrates this using a simple flowchart shown in Figure 5.3:
START
OUTPUT result
STOP
She notices that the task of searching for the book title is similar to the task of iterating through
the items in a list (see Figure 4.118 in Chapter 4). From there, she can adapt the algorithm that
she already knows to complete this new task.
150
Module 4: Programming
START
index = 0
Yes
Is titles[index] == search?
result = availability[index]
No
No
index = index + 1
STOP
Siti decides that this planned solution is feasible and moves on to the next stage.
151
How Can Programs Be Used to Solve Problems?
Some non-traditional approaches to program development do not have five distinct stages. For
instance, in “agile” software development, code may be continuously written and refined while
the gathering of requirements is still taking place.
One possible way for Siti to write her code would be:
1 # Input
2 titles = [
3 # The following data is copied from the given text file.
4 "How to Solve a Mystery", "Vacant Memories",
5 "The Cybersnake Chronicles", "Music History",
6 "Like Tears in Rain", "Out of the Abyss"
7 ]
8 availability = [
9 # The following data is copied from the given text file.
10 "Not Available", "Available", "Available", "Not Available",
11 "Not Available", "Available"
12 ]
13 search = input("Enter title of book to check: ")
14
15 # Process
16 index = 0
17 while index < len(titles):
18 if titles[index] == search:
19 result = availability[index]
20 index = index + 1
21
22 # Output
23 print(result)
152
Module 4: Programming
Testing is the process by which these and other imperfections are uncovered. This is done by
running the code through one or more test cases to evaluate whether the written code adequately
satisfies the gathered requirements and is ready to be used. A test case usually consists of a set
of inputs and the corresponding set of expected outputs. By feeding these inputs into a program
and comparing its actual output with the expected output, we can evaluate whether the program
is working as intended.
The effectiveness of testing depends entirely on how comprehensive and well-designed the test
cases are. The process of designing good test cases is discussed further in Chapter 6.
Testing is so important in making sure code is written correctly that, instead of five distinct stages,
some programmers use an alternative approach to program development – test cases are written
first and code is written incrementally to pass each test. This alternative process is known as
Test-Driven Development (TDD).
153
How Can Programs Be Used to Solve Problems?
Siti has developed a number of test cases to make sure that her code is functioning as intended.
She runs the program through the test cases one by one, comparing the program’s output with
the expected output:
Available
Not Available
154
Module 4: Programming
So far, the output of Siti’s program has matched the expected output, so the program seems to be
functioning as intended. However, she decides to include a test case that searches for the name
of a book which is not in the library collection.
Now, Siti notices an imperfection in her program. When the user enters the name of a book that
does not exist in the library, her program generates an error instead of producing the expected
output “Book Not Found”.
155
How Can Programs Be Used to Solve Problems?
Siti studies her program carefully and realises that she did not consider this special case. Fortunately,
she can resolve this by initialising the value of result to “Book Not Found”. Her refined code
is shown in Figure 5.6:
1 # Input
2 titles = [
3 # The following data is copied from the given text file.
4 "How to Solve a Mystery", "Vacant Memories",
5 "The Cybersnake Chronicles", "Music History",
6 "Like Tears in Rain", "Out of the Abyss"
7 ]
8 availability = [
9 # The following data is copied from the given text file.
10 "Not Available", "Available", "Available", "Not Available",
11 "Not Available", "Available"
12 ]
13 search = input("Enter title of book to check: ")
14
15 # Process
16 result = "Book Not Found"
17 index = 0
18 while index < len(titles):
19 if titles[index] == search:
20 result = availability[index]
21 index = index + 1
22
23 # Output
24 print(result)
156
Module 4: Programming
Siti deploys her code by installing Python in the library’s computer and copying the program
over. She organises a training session to teach the librarians how to use the program and gathers
their feedback after the first day of use. She receives the following comments:
• “Your program makes it much easier to check if a book is available. Thank you!”
• “Copying data from text files into the program is very tiresome. Can this be automated?”
• “The program is good, but can it be faster?”
• “Some book titles take very long to type. However, it’s still faster than checking
the bookshelves!”
It seems that while Siti’s program meets the requirements, it can still be improved in terms of
usability and speed. To make some of these improvements, however, she may need to perform
more complex processing of numbers and strings. The next section introduces some of the more
common built-in functions that Python already provides for such tasks.
157
How Can Programs Be Used to Solve Problems?
158
Module 4: Programming
import name_of_module
After importing the math module, the following functions will become available:
159
How Can Programs Be Used to Solve Problems?
Using any of these functions without importing the math module will result in an error. In
the example below, Python does not understand what the name “math” means as it does not
correspond to an existing variable or loaded module:
>>> math.ceil(20.17)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
math.ceil(20.17)
NameError: name 'math' is not defined
p Figure 5.8 Using the math module before it is imported will result in an error
To start using a module’s contents, we need to import it once each time we start a Python session
in interactive mode or create a Python program:
The period (.) in math.ceil() actually has the meaning of searching inside the value on its
left and looking for a function with the name given on its right. As you will see later, besides
searching inside modules, this operator is also used to access functions that are stored inside str
values (called methods).
160
Module 4: Programming
For round(), the argument is always rounded to the nearest whole number. However, an argument
that is exactly halfway between two integers (such as 0.5) may be rounded either up or down.
Figure 5.10 demonstrates how round() can appear to give unpredictable results – notice that
0.5 is rounded down while 1.5 is rounded up.
Table 5.8 Behaviour of the round() function for numbers around zero
x round(x) Explanation
−1.0 −1 Exact
0.0 0 Exact
1.0 1 Exact
>>> round(-1.0)
-1
>>> round(0.5)
0
>>> round(0.75)
1
>>> round(1.5)
2
161
How Can Programs Be Used to Solve Problems?
The round() function only appears to give unpredictable results. In reality, round() will always
return the even integer if its argument is exactly halfway between two integers. For example,
round(0.5) will return 0 and round(1.5) will return 2.
On the other hand, math.ceil() always rounds up. Take note that this means a negative
non-integer argument will end up becoming less negative.
Table 5.9 Behaviour of the math.ceil() function for numbers around zero
x math.ceil(x) Explanation
−1.0 −1 Exact
−0.75 0 Rounded up
−0.5 0 Rounded up
−0.25 0 Rounded up
0.0 0 Exact
0.25 1 Rounded up
0.5 1 Rounded up
0.75 1 Rounded up
1.0 1 Exact
162
Module 4: Programming
Conversely, math.floor() always rounds down. Take note that this means a negative
non-integer argument will end up becoming more negative.
Table 5.10 Behaviour of the math.floor() function for numbers around zero
x math.floor(x) Explanation
-1.0 -1 Exact
0.0 0 Exact
1.0 1 Exact
163
How Can Programs Be Used to Solve Problems?
Finally, math.trunc() always rounds towards zero. This is equivalent to dropping the digits of
the argument after the decimal point (i.e., truncating the argument).
Table 5.11 Behaviour of the math.trunc() function for numbers around zero
x math.trunc(x) Explanation
-1.0 -1 Exact
0.0 0 Exact
1.0 1 Exact
164
Module 4: Programming
1. For each value of x below, predict the result of the respective rounding functions.
Check your answers by printing the actual return values in Python.
math.floor(x)
round(x)
math.trunc(x)
2. When two six-sided dice are rolled, the resulting sum can range from 2 to 12 (inclusive).
Write a program to simulate rolling two six-sided dice 1000 times and output how
often the resulting sum is 2, 3, 4 and so on up to 12.
165
How Can Programs Be Used to Solve Problems?
2: 29
3: 59
4: 110
5: 102
6: 146
7: 160
8: 122
9: 90
10: 85
11: 72
12: 25
Table 5.14 Some ASCII characters and the numbers used to represent them
32 (space) 64 @ 96 `
33 ! 65 A 97 a
34 " 66 B 98 b
35 # 67 C 99 c
36 $ 68 D 100 d
166
Module 4: Programming
Table 5.14 Some ASCII characters and the numbers used to represent them (continued)
37 % 69 E 101 e
38 & 70 F 102 f
39 ’ 71 G 103 g
40 ( 72 H 104 h
41 ) 73 I 105 i
42 * 74 J 106 j
43 + 75 K 107 k
44 , 76 L 108 l
45 - 77 M 109 m
46 . 78 N 110 n
47 / 79 O 111 o
48 0 80 P 112 p
49 1 81 Q 113 q
50 2 82 R 114 r
51 3 83 S 115 s
52 4 84 T 116 t
53 5 85 U 117 u
54 6 86 V 118 v
55 7 87 W 119 w
56 8 88 X 120 x
57 9 89 Y 121 y
58 : 90 Z 122 z
59 ; 91 [ 123 {
60 < 92 \ 124 |
61 = 93 ] 125 }
62 > 94 ^ 126 ~
63 ? 95 _
167
How Can Programs Be Used to Solve Problems?
To convert between characters and the numbers that represent them, Python provides the following
two built-in functions:
To represent characters in other written languages such as Tamil and Mandarin, Python uses an
expanded standard called Unicode. Like ASCII, the Unicode standard uses numbers to represent
characters. The Unicode standard is compatible with ASCII as the first 128 numbers represent
the same characters in both ASCII and Unicode. However, unlike ASCII, up to 1,114,112 distinct
characters can be represented in Unicode. Some examples using non-English characters are
shown in Figure 5.15:
>>> ord('அ')
2949
>>> chr(2949)
'அ'
>>> ord('中')
20013
>>> chr(20013)
'中'
168
Module 4: Programming
value.name_of_method()
value.name_of_method(argument_1)
value.name_of_method(argument_1, argument_2)
Table 5.16 shows some common string functions that can be used as methods.
169
How Can Programs Be Used to Solve Problems?
170
Module 4: Programming
171
How Can Programs Be Used to Solve Problems?
p Figure 5.17 Using type casting and the concatenation operator to format strs
However, Python can also produce the same output using the str.format() method. Using
the str.format() avoids concatenating strs manually and results in code that is easier to read.
Just like indices for lists and strs, indices for arguments start from 0 and not 1. Hence,
the field of {0} specifies an index of 0 and is replaced by the first argument student. The
field of {1} specifies an index of 1 and is replaced by the second argument year.
172
Module 4: Programming
Note that multiple fields may specify the same index and that it is not compulsory for all arguments
to be included in the output.
Also note that fields with specified indices and fields without specified indices cannot be
used together.
p Figure 5.22 Fields with and without specified indices cannot be used together
To “escape” the curly braces so that they are not treated as a field, use double braces so that it
looks like “{{“ or ”}}”. The str.format() method will convert the double curly braces back
into single curly braces and will not treat the curly braces as a field.
p Figure 5.23 Using double curly braces to prevent them from being treated as a field
173
How Can Programs Be Used to Solve Problems?
{}
{index}
The str.format() method and replacement fields can also perform more complex tasks such
as lengthening strs to a specified width and restricting the number of decimal places used when
converting a float to a str. Use of the str.format() method to perform such tasks will not
be covered in this textbook.
1. Fill in the blank in the program below so that it correctly outputs whether the first
character of name is an upper-case letter.
2. Complete the program below by filling in the blank with one or more lines of code so
that it correctly outputs whether title contains upper-case letters and spaces only
(i.e., no lower-case letters, symbols or digits).
174
Module 4: Programming
It is useful to have a catalogue or “toolbox” of common solutions which can be adapted to solve
problems that cannot be solved easily by a built-in function. Through pattern recognition and
generalisation, we can identify a previously used solution that can be applied to a new or seemingly
unfamiliar problem. In this section, we will look at some common problems and their solutions.
A common extension to this problem is to find out where this minimum value is located in the
list. Suppose the list of heights is for a class of students and that the heights are arranged in order
of registration number. Finding the registration number of the shortest student in the class is thus
equivalent to determining the index of the minimum value in the list.
Depending on the situation, we may also need to check if a minimum value even exists. Suppose
the list of heights is for students who sign up for a basketball game. If it turns out that no students
sign up for the game, the list of heights would be empty and no minimum value would exist. The
solution would need to be able to handle this case as well.
Table 5.17 Input and output requirements for the minimum value problem
Input Output
175
How Can Programs Be Used to Solve Problems?
1 # Input
2 values = [16, 64, 4, 128, 8, 2, 1, 32]
3
4 # Process
5 if len(values) > 0:
6 minimum_value = values[0]
7 minimum_index = 0
8 for index in range(len(values)):
9 if values[index] < minimum_value:
10 minimum_value = values[index]
11 minimum_index = index
12 else:
13 minimum_value = None
14 minimum_index = None
15
16 # Output
17 print(minimum_value)
18 print(minimum_index)
p Figure 5.27 Finding the minimum value in a list and its index
The solution starts by checking whether values has at least one value (line 5). If not, we conclude
that values is empty and simply set the outputs to None (lines 13 and 14).
We then gradually extend this sub-problem by considering the rest of the values in values,
one by one (line 8). The solution is extended accordingly by checking if the new value being
considered is less than minimum_value (line 9). If so, we update minimum_value and minimum_
index with this new value (lines 10 and 11). This ensures that minimum_value and minimum_index
are always keeping track of the minimum value encountered so far.
When the loop ends, we would have considered all the values in values and thus can conclude
that the resulting minimum_value and minimum_index are correct for the entire list.
176
Module 4: Programming
On the other hand, if it is not necessary to find the index of the minimum value, we can simplify
the program by removing the variables index and minimum_index completely.
1 # Input
2 values = [16, 64, 4, 128, 8, 2, 1, 32]
3
4 # Process
5 if len(values) > 0:
6 minimum_value = values[0]
7 for value in values:
8 if value < minimum_value:
9 minimum_value = value
10 else:
11 minimum_value = None
12
13 # Output
14 print(minimum_value)
Python also provides a built-in min() function that can further simplify the solution if finding
the index is not necessary:
1 # Input
2 values = [16, 64, 4, 128, 8, 2, 1, 32]
3
4 # Process
5 if len(values) > 0:
6 minimum_value = min(values)
7 else:
8 minimum_value = None
9
10 # Output
11 print(minimum_value)
p Figure 5.29 Finding the minimum value of a list using the built-in min() function
177
How Can Programs Be Used to Solve Problems?
Even though this solution is much simpler than the previous ones, it is still useful to understand
the algorithm in Figure 5.27 as the min() function does not provide the index of the minimum
value and cannot be customised for more complex situations, such as finding the second or
third smallest value in a list.
Table 5.18 Input and output requirements for the maximum value problem
Input Output
• maximum_index: index of
maximum_value in values; None
if values is empty
By using pattern recognition, we can easily modify the program in Figure 5.27 so that it finds the
maximum value in a list instead:
1 # Input
2 values = [16, 64, 4, 128, 8, 2, 1, 32]
3
4 # Process
5 if len(values) > 0:
6 maximum_value = values[0]
7 maximum_index = 0
8 for index in range(len(values)):
9 if values[index] > maximum_value:
10 maximum_value = values[index]
11 maximum_index = index
12 else:
13 maximum_value = None
14 maximum_index = None
15
16 # Output
17 print(maximum_value)
18 print(maximum_index)
p Figure 5.30 Finding the maximum value in a list and its index
178
Module 4: Programming
The resulting program is largely the same, except that the greater-than operator (>) is used
on line 9 instead of the less-than operator (<), and the output variables are renamed to
maximum_value and maximum_index.
Just as with the previous problem, if it is not necessary to find the index of the maximum value,
we can simplify the program by removing the variables index and maximum_index completely.
1 # Input
2 values = [16, 64, 4, 128, 8, 2, 1, 32]
3
4 # Process
5 if len(values) > 0:
6 maximum_value = values[0]
7 for value in values:
8 if value > maximum_value:
9 maximum_value = value
10 else:
11 maximum_value = None
12
13 # Output
14 print(maximum_value)
Similarly, Python provides a built-in max() function that can further simplify the solution if finding
the index is not necessary:
1 # Input
2 values = [16, 64, 4, 128, 8, 2, 1, 32]
3
4 # Process
5 if len(values) > 0:
6 maximum_value = max(values)
7 else:
8 maximum_value = None
9
10 # Output
11 print(maximum_value)
p Figure 5.32 Finding the maximum value in a list using the built-in max() function
179
How Can Programs Be Used to Solve Problems?
Depending on the situation, we may also need to check if an average value even exists. Suppose
the list of scores is for an optional test. If it turns out that no students choose to take the test, the
list of scores would be empty and no average value would exist.
Table 5.19 Input and output requirements for the average problem
Input Output
1 # Input
2 values = [16, 64, 4, 128, 8, 2, 1, 32]
3
4 # Process
5 if len(values) > 0:
6 sum_values = 0
7 for value in values:
8 sum_values += value
9 average = sum_values / len(values)
10 else:
11 average = None
12
13 # Output
14 print(average)
The solution starts by checking whether values has at least one value (line 5). If not, we conclude
that values is empty and simply set the output to None (line 14).
Otherwise, we calculate the sum of the values in values using a for-in loop (lines 7 to 8) and
divide this sum by the length of values to obtain the required average (line 15).
180
Module 4: Programming
Python also provides a built-in sum() function that simplifies the task of calculating the sum of
the values in values without the need for a loop:
1 # Input
2 values = [16, 64, 4, 128, 8, 2, 1, 32]
3
4 # Process
5 if len(values) > 0:
6 sum_values = sum(values)
7 average = sum_values / len(values)
8 else:
9 average = None
10
11 # Output
12 print(average)
p Figure 5.34 Finding the average value in a list using the built-in sum() function
While both the programs in Figures 5.33 and 5.34 are correct, the program in Figure 5.34 is
preferred as it is simpler.
Depending on the situation, the search may not be able to find the value. Suppose the name that
we input for searching is misspelled. The program would not be able to find this name in the list
and must be able to handle this situation as well.
181
How Can Programs Be Used to Solve Problems?
Table 5.20 Input and output requirements for the searching problem
Input Output
1 # Input
2 values = ["Alex", "Bala", "Siti", "Zee"]
3 search = input("Enter name: ")
4
5 # Process
6 search_index = None
7 for index in range(len(values)):
8 if values[index] == search:
9 search_index = index
10 break
11
12 # Output
13 print(search_index)
The solution first initialises the output search_index to None (line 6). This ensures that if no
match is found or if values is empty, search_index correctly remains as None.
Using an incremental approach, we then check each value in the list in order (line 7) to see if a
match can be found (line 8). Once a match is found, we set search_index as the index of the
matching value and stop the search immediately using a break command (lines 9 to 10).
182
Module 4: Programming
After the digits have been manually entered, the computer will calculate whether the expected
mathematical relationship is true. If the relationship is still true, it is likely that the numbers were
entered correctly. Otherwise, the computer can ask for the numbers to be re-entered and checked
again.
For instance, most products sold in shops are labelled with a barcode that also contains a number.
This number is also known as a product code, which is used to identify the product being
purchased. A product code is obtained when the barcode is decoded by a barcode reader or
when the number is entered manually at the cashier. The last digit of this product code is actually
a check digit that helps to prevent errors in the decoding or entry process.
One popular standard for check digits that are used in product barcodes is the Universal Product
Code (UPC-A) standard. Figure 5.36 shows an example of a 12-digit UPC-A product code. The
12th digit is the check digit.
To calculate the check digit, the first 11 digits need to be processed using the following algorithm:
1. Add the digits in the odd-numbered positions (i.e., 1st, 3rd … 11th).
2. Multiply the result by three.
3. Add the digits in the even-numbered positions (i.e., 2nd, 4th … 10th) to the result.
4. If the last digit of the result is 0, the check digit should be 0. Otherwise, subtract the last
digit of the result from 10. The check digit should be the same as the resulting answer.
183
How Can Programs Be Used to Solve Problems?
The program in Figure 5.37 demonstrates how to calculate the check digit from the first 11 digits
of a UPC-A code. There are also many other possible check digit algorithms.
However, more complex string problems will usually need string functions or methods such as
those found in section 5.3 to determine whether each character should be extracted. For instance,
extracting all the letters and numbers from a string is equivalent to extracting all the characters
for which the str.isalnum() method returns True.
184
Module 4: Programming
Table 5.21 Input and output requirements for the extracting problem
Input Output
A solution for this problem, using the str.isalnum() method as an example input for test(),
is as follows:
1 # Input
2 original = input("Enter a string of characters: ")
3
4 # Process
5 extract = ""
6 for character in original:
7 # In this example, str.isalnum() is used for test()
8 if character.isalnum():
9 extract += character
10
11 # Output
12 print(extract)
The solution starts by initialising the output variable extract to an empty string (line 5). Using
an incremental approach, we then consider the characters of the original string, one by one and
in order (line 6), to see if each character meets our requirements for extraction (line 8). Each
character that meets our requirements is appended to our output (line 9).
When the loop ends, we would have considered all the characters in the original string and thus
can conclude that extract has all the characters that meet our requirements.
185
How Can Programs Be Used to Solve Problems?
Chapter Summary
COMMON PROBLEMS
AND SOLUTIONS
Gather
Requirements ADDITIONAL BUILT-IN
FUNCTIONS
Plan Solutions
Mathematical String
Functions Functions
Write Code
math String
Module Methods
Test and
Refine Code
Rounding
Functions
Formatting
Strings
Deploy
Code random
Module
186
Module 4: Programming
Review Questions
1. Siti has developed a program for checking the availability of library books. Based on the
feedback she has received, Siti revisits the solution-planning stage and comes up with a
new algorithm that she thinks would be faster, shown in Figure 5.39 below.
START
index = 0
index = index + 1
Yes
No No
Is titles[index] = search?
Yes
STOP
a) Explain why this new solution may be faster than the solution in
availability_refined.py shown in Figure 5.6.
b) Write a program that corresponds to Siti’s new algorithm. You may wish to use
availability_refined.py as a starting point.
187
How Can Programs Be Used to Solve Problems?
2. Write a program that lets the user input a list of ints by asking for each item of the
list in order. Receiving an empty input from the user would indicate the end of the
list. The program would then output the smallest int in the list, followed by a
space, followed by the second smallest int in the list. However, if the list has fewer
than two items, it should output the word “None” instead.
Assume that the input data will always be valid and none of the ints are repeated.
Two test cases for how the program should behave with different inputs are as follows:
3. Write a program that lets the user input a list of floats by first asking for the number of
items in the list, then asking for each item of the list in order. The program would then
output the average of only the positive floats in the list, rounded to two decimal places.
However, if the list has no positive floats, it should output the word “None” instead.
Two test cases for how the program should behave with different inputs are as follows:
Enter length: 4
Enter item: 20.17
Enter item: -19.65
Enter item: 201.8
Enter item: 2.020
74.66
Enter length: 0
None
188
6 Module 4: Programming
How Do I Ensure
That a Program Works
as Intended?
Bala has written a program to automate the process of shortening common phrases
into acronyms. However, it does not seem to be working properly. He decides to ask
Siti for help. How should Siti go about helping Bala to fix his program and ensure that
it always functions as expected?
In the previous chapter, we learnt that testing and refining code is one of the five stages
in developing a program. In this chapter, we will learn about the process of testing and
refining code to identify and fix errors in greater detail, whether these errors are caused
by invalid input from the user or mistakes made by the programmer.
189
How Do I Ensure That a Program Works as Intended?
• Design appropriate test cases to cover normal, error and boundary conditions, and
specify what is being tested for each test case.
• Understand and describe types of program errors and explain why they occur:
– Syntax errors
− Run-time errors
− Logic errors
For example, consider the program we used in section 5.4.3 to find the average value in a list
of numbers.
Table 6.1 Input and output requirements for the problem of calculating the
average value in a list of numbers
Input Output
190
Module 4: Programming
1 # Input
2 values = [16, 64, 4, 128, 8, 2, 1, 32]
3
4 # Process
5 if len(values) > 0:
6 sum_values = sum(values)
7 average = sum_values / len(values)
8 else:
9 average = None
10
11 # Output
12 print(average)
However, what would happen if values was initialised as a list of strs instead?
1 # Input
2 values = ["16", "64", "4", "128", "8", "2", "1", "32"]
3
4 # Process
5 if len(values) > 0:
6 sum_values = sum(values)
7 average = sum_values / len(values)
8 else:
9 average = None
10
11 # Output
12 print(average)
p Figure 6.3 Error caused by invalid input data (highlighted in the program)
191
How Do I Ensure That a Program Works as Intended?
The following sections will cover the four common checks that are performed in data validation:
1. Length check
2. Range check
3. Presence check
4. Format check
Input Output
192
Module 4: Programming
For the input to be valid, every guess must have a length of 3 exactly. The program below
performs this length check using the len() function on line 16.
Note that this program uses two infinite while loops. To make the player keep guessing until a
correct guess is made, the program enters an outer while loop on line 11 that keeps repeating
until the correct word is guessed and a break statement is used to exit the outer loop on line 24.
To make the player re-enter guess until it passes input validation, the program enters an inner
while loop on line 14 that keeps repeating until the length check on line 16 is passed and a
break statement is used to exit the loop on line 17.
In general, length checks make use of the len() function and are needed to ensure that list
or str input data is not too short or too long.
193
How Do I Ensure That a Program Works as Intended?
75–100 A1
70–74 A2
65–69 B3
60–64 B4
55–59 C5
50–54 C6
45–49 D7
40–44 E8
Less than 40 F9
The inputs and outputs for this problem are specified below:
Input Output
194
Module 4: Programming
In this problem, the input is a rounded percentage and must be a whole number between 0 and
100. The following program shows how this range check might be performed:
195
How Do I Ensure That a Program Works as Intended?
Like the previous example, this program keeps trying if data validation fails by asking the user to
re-enter the score. In general, range checks make use of these operators:
• less than (<)
• less than or equal to (<=)
• greater than (>)
• greater than or equal to (>=)
They are needed to ensure that int or float input data is within the required range of values.
For some problems, portions of the input are required (or mandatory) while other parts may be
optional. If it is possible to leave out any optional inputs, the program should perform a presence
check to ensure that all the required inputs are provided.
For instance, let us look again at the word-guessing game where all guesses must have a length of 3.
Input Output
The program in Figure 6.6 shows how a presence check for Presence check
guess might be performed by checking whether the given input Process of ensuring that
for guess is blank on line 16. all the required inputs
are provided
In general, presence checks use special functions or compare
inputs to values such as None or an empty string ("") to ensure
that all required inputs are provided.
196
Module 4: Programming
Sometimes a problem requires the input to satisfy additional complex requirements such as
following a particular pattern. This is known as a format check.
For instance, consider a program that prints an appropriate greeting for a given time of day.
Input Output
• time: time in the • An appropriate greeting for the given time; "Good
24-hour clock in Morning" from 05:00 to 11:59, "Good Afternoon" from
HH:MM format 12:00 to 17:59, "Good Evening" from 18:00 to 21:59,
and "Good Night" from 22:00 to 04:59
197
How Do I Ensure That a Program Works as Intended?
198
Module 4: Programming
Depending on the problem to be solved, more complex format checks, such as requiring that the
input string uses a particular data format, may also be needed.
Performing format checks is a very common task in programming. However, the code for format
checks can get very long. For instance, the following code is one possible way to check whether
every letter in a str named sleep is ‘z’ using an output bool named is_valid:
is_valid = True
for letter in sleep:
if letter != 'z':
is_valid = False
break
print(is_valid) # Outputs True only if every letter in sleep is 'z'
p Figure 6.8 Format check for whether every letter in sleep is ‘z’
To make the code for format checks shorter to write and read, programmers can use a specialised
“regular expression language” that is designed for this purpose. For instance, the following code
performs the same check as above but is much shorter:
import re
is_valid = bool(re.fullmatch("z*", sleep))
print(is_valid) # Outputs True only if every letter in sleep is 'z'
p Figure 6.9 Equivalent format check using the regular expression "z*"
You can find out more about regular expressions and how to use Python’s re module (short for
regular expressions) in this website:
https://docs.python.org/3/library/re.html
199
How Do I Ensure That a Program Works as Intended?
while True:
s = input("Enter s: ")
if len(s) == 2 and s[0] in "ABCDEF" and s[1].isdigit():
break
print("Data validation failed!")
...
b) Extract 2:
while True:
first = input("Enter first name: ")
middle = input("Enter middle name (optional): ")
last = input("Enter last name: ")
if first == "" or last == "":
print("Data validation failed!")
else:
break
...
c) Extract 3:
p = float(input("Enter p:"))
while p < 0.0 or p > 1.0:
print("Data validation failed!")
p = float(input("Enter p:"))
...
200
Module 4: Programming
d) Extract 4:
n = int(input("Enter n:"))
while True:
integers = []
while True:
input_text = input("Enter integer, blank to end: ")
if input_text == "":
break
integers += [int(input_text)]
if len(integers) == n:
break
print("Data validation failed!")
...
2. Write the input entry and validation code for a program that needs to accept a postal
code. If the input entered via the keyboard is invalid, your input validation code should
keep trying by asking for the input to be entered again. The specification for the problem’s
input is provided below. (The specification for the output is not needed to solve
this problem.)
Table 6.7 Input and output requirements for a problem that uses a postal code for input
Input Output
After data validation, we can be sure that all the inputs provided fulfil the requirements and do
not have errors. However, there may still be errors and imperfections within our code. To detect
these defects, it is necessary to perform testing using well-designed test cases that cover a wide
variety of conditions.
Designing good test cases is important because it is usually impossible to test every possible
input combination. We should focus on situations where errors are most likely to occur.
Before learning about designing test cases, we need to understand the types of program errors
that are possible and which types can be detected by test cases.
201
How Do I Ensure That a Program Works as Intended?
Note that the three types of program errors are not mutually
Hang
exclusive. In other words, some run-time errors may also be
Unresponsiveness of a
considered logic errors.
program due to an error
What are Errors that are due Errors that are detected Errors that usually
they? to incorrect source while a program is do not cause the
code that does not running, usually causing program to crash or
follow the rules of the program to crash or hang immediately;
the language hang instead the program
does not give the
expected output
When When the compiler While the program is While the program
are they or interpreter tries to being run is being run
detected? translate the source
code to machine
code
202
Module 4: Programming
Syntax errors are usually the easiest to detect as they are “caught” at the initial code translation
stage. It is usually not necessary to design test cases to find syntax errors.
Let us look again at the program we used previously to find the average value in a list of numbers
(see section 6.1.1). In the program in Figure 6.14, we introduced a syntax error on line 5 by using
the assignment operator (=) instead of the equivalence operator (==).
1 # Input
2 values = []
3 while True:
4 input_str = input("Enter integer, blank to end: ")
5 if input_str = "":
6 break
7 values += [int(input_str)]
8
9 # Process
10 if len(values) > 0:
11 sum_values = sum(values)
12 average = sum_values / len(values)
13 else:
14 average = None
15
16 # Output
17 print(average)
p Figure 6.15 The program gives an error message that specifies the location of the error
203
How Do I Ensure That a Program Works as Intended?
1 # Input
2 values = []
3 while True:
4 input_str = input("Enter integer, blank to end: ")
5 if input_str == "":
6 break
7 values += [int(input_str)]
8
9 # Process
10 if len(values) > 0:
11 sum_values = sum(values)
12 average = sum_values / len(values)
13 else:
14 average = None
15
16 # Output
17 print(average)
p Figure 6.16 The syntax error on line 5 is fixed by using the correct operator (highlighted)
For example, in Program 6.8, when the input is “seven” Run-time error
instead of the digit “7”, a run-time error occurs. This run-time Error that occurs while the
error is caused by the input data that has not been properly program is running
validated by a format check to ensure that it is a valid integer
(i.e., numeric digits only with a possible minus sign in front).
204
Module 4: Programming
In compiled languages such as C or Java, since source code is fully translated to machine code
before the program runs, it is not possible for a syntax error to cause a run-time error.
In interpreted languages such as Python, however, since source code is translated to machine code
while the program is running, syntax errors can also be considered as run-time errors.
In Program 6.9, we have removed any checks for an empty list before calculating the average.
When we run this program and immediately enter a blank, the program crashes with a division
by zero error. This is another example of a run-time error.
1 # Input
2 values = []
3 while True:
4 input_str = input("Enter integer, blank to end: ")
5 if input_str == "":
6 break
7 values += [int(input_str)]
8
9 # Process
10 sum_values = sum(values)
11 average = sum_values / len(values)
12
13 # Output
14 print(average)
205
How Do I Ensure That a Program Works as Intended?
While run-time errors are usually obvious when they occur, logic
Key Term
errors may be “hidden” and may not show any obvious sign that
something is amiss. In such cases, the only way to detect the
logic error is by running a test case and observing that the actual Logic error
output of the program is different from the expected output. Error that causes the
output of a program to
For example, in Program 6.10, we have introduced a logic error be different from what
on line 16 by using an incorrect formula that multiplies (instead is expected
of dividing) sum_values by the number of items in values to
calculate the average.
1 # Input
2 values = []
3 while True:
4 input_str = input("Enter integer, blank to end: ")
5 if input_str == "":
6 break
7 values += [int(input_str)]
8
9 # Process
10 if len(values) > 0:
11 sum_values = sum(values)
12 average = sum_values * len(values)
13 else:
14 average = None
15
16 # Output
17 print(average)
206
Module 4: Programming
While the program appears to finish running with no errors, the output value of 40 is different
from the expected output of 2.5 based on the input data of 2, 0, 1 and 7. Such unexpected or
incorrect output is the most obvious sign that a program contains “hidden” logic errors.
Sometimes, logic errors may be obvious when the program crashes with a run-time error. However,
the resulting run-time error may not occur immediately on the line of code containing the logic
error. Such cases are tricky because the programmer would usually need to “work backwards”
from the location of the crash to find out where the logic error is actually located. This is because
the crash would usually be caused by a variable with an invalid value assigned to it by a previous
line of code. This value could in turn be derived from another variable with an incorrect value
assigned by a previous line of code, and so on.
For instance, in the program below, a run-time error occurs on line 11 because the contents of
values are invalid for summing. However, this is actually caused by a logic error introduced on
line 7, where strs are being inserted into values instead of ints.
1 # Input
2 values = []
3 while True:
4 input_str = input("Enter integer, blank to end: ")
5 if input_str == "":
6 break Run-time error on line 11 is
actually caused by missing type
7 values += [input_str]
conversion on line 7.
8
9 # Process
10 if len(values) > 0:
11 sum_values = sum(values)
12 average = sum_values / len(values)
13 else:
14 average = None
15
16 # Output
17 print(average)
p Figure 6.23 Run-time error occurs on line 11 but is caused by logic error on line 7
207
How Do I Ensure That a Program Works as Intended?
p Figure 6.24 The program alerts the run-time error but not the logic error
To detect run-time errors and logic errors like the ones described in these examples, it is important
to run the programs that we write through multiple test cases. The test cases should cover three
types of conditions:
1. Normal conditions
2. Boundary conditions
3. Error conditions
The input and output requirements for this problem are specified in Table 6.9.
Table 6.9 Input and output requirements for the problem of converting a phrase into an acronym
Input Output
208
Module 4: Programming
However, we also want to be sure that the test cases for normal conditions accurately represent
the variety of inputs that we are expecting. To test this, we shall design an additional test case so
that words that start with both upper-case and lower-case letters are represented.
Test cases for normal conditions are the most straightforward and will usually be the first test
cases to be written and passed successfully.
209
How Do I Ensure That a Program Works as Intended?
For the acronym generation problem, there is no defined maximum length for the input phrase,
so it is not possible to design a test case that would be at the upper limit of what the program can
handle. However, we do want the program to work as intended even with the minimum length
of input possible, that is, an empty str. In this case, we can choose the correct output to also
be an empty str, as shown in Test Case 3.
In the acronym generation problem, we could also have chosen the expected output to be an error.
If you are unsure whether a set of inputs should generate an error or output something unusual, it
is likely that you have encountered a boundary condition where the inputs are at the limit of what
is considered normal. Usually, either option of generating an error or producing unusual output
(such as an empty string) would be acceptable. However, depending on the situation, one option
may be preferable to the other. For instance, if the output for the acronym generation problem
will in turn be used as a required input for another problem, it might be better to generate an error
instead so that a blank acronym is never produced.
Under normal conditions, we would expect the input phrase to be a sequence of words with
a single space in between. However, the program must also be able to run in the special case
where additional spaces are provided before and after words. Let us design an additional test
case to cover such situations.
For Test Case 4, the expected output for “ Computer Science ” is exactly the same as the
expected output for “Computer Science” (with no additional spaces).
Determining the boundary conditions for a problem and the expected output for each case can
be tedious as well as difficult. However, this process is necessary to ensure that the program
behaves as intended regardless of the input supplied.
210
Module 4: Programming
Test cases for error conditions are necessary because programs that fail to reject invalid input may
end up performing unintended or even harmful actions. For instance, a large number of security
flaws in programs today are caused by the improper handling of error conditions. A typical
example in some programming languages would be forgetting to check whether the input data
supplied by a user can fit into the memory space allocated to store the data. A program that tries
to store this invalid data anyway will end up overwriting subsequent areas of memory and allow
potential attackers to insert their own instructions into the program.
211
How Do I Ensure That a Program Works as Intended?
Figure 6.25 shows the analogy of a car to illustrate the three types of conditions that test cases
should cover.
Under normal conditions, the car has all four wheels on the ground. We expect the car to function
perfectly under these conditions.
Under boundary conditions, the car may be balancing on two wheels. The car may or may not
function correctly under these conditions, and depending on the problem context, the car may
or may not be required to function under such conditions.
Under error conditions, the car has all four wheels up in the air. A well-designed car would fail
to function in such a situation, and if possible, prevent any harmful actions from being taken by
the driver.
212
Module 4: Programming
Table 6.16 Input and output requirements for the problem of drawing a square
Input Output
The following are two test cases under normal conditions for this problem. (Note that
although the expected outputs may appear rectangular, they can be considered as
squares as they have the same number of characters on each side.)
213
How Do I Ensure That a Program Works as Intended?
a) Design another test case that covers normal conditions for this problem.
b) Design two test cases that cover the boundary conditions for this problem. For each
test case, explain why it covers a boundary condition.
c) Design two test cases that cover the error conditions for this problem. For each test
case, explain why it covers an error condition. (You may choose your own expected
error message.)
Questions 2 and 3 involve programs meant to solve the following diamond drawing
problem:
Table 6.19 Input and output requirements for the problem of drawing a diamond
Input Output
The following is a test case under normal conditions for this problem:
214
Module 4: Programming
2. The program below for the diamond drawing problem has three syntax errors.
a) Identify where the syntax errors are.
b) Rewrite the program to fix these syntax errors.
p Figure 6.26 Program for diamond drawing problem with syntax errors
215
How Do I Ensure That a Program Works as Intended?
3. The program below for the diamond drawing problem crashes with a run-time error on
line 25 when it runs on the given test case shown in Table 6.21.
216
Module 4: Programming
Enter n: 5 Failed
*
***
***
*
Fix this error by changing only one line and explain whether line 25 should be the line
of code to change.
217
How Do I Ensure That a Program Works as Intended?
Figure 6.28 shows a journal entry dated 9 September 1947 by users of an early electronic computer
called the “Mark II”. They had discovered a moth trapped in part of the computer, causing it to
malfunction. This dead moth is often considered the first actual computer “bug” to be found.
p Figure 6.28 A moth trapped in a computer was the first actual computer “bug”
218
Module 4: Programming
In this case, we use the condition that extracts only characters that follow a space.
1 # Input
2 input_text = input("Enter the input text: ")
3
4 # Process
5 acronym = ""
6 for index in range(len(input_text)):
7 if input_text[index - 1].isspace():
8 acronym += input_text[index]
9
10 # Output
11 print(acronym)
However, when we try to run the program on Test Case 1, we find that its output is different from
what is expected.
This is a sign that there is a bug. Let us look at some common debugging techniques to fix it.
219
How Do I Ensure That a Program Works as Intended?
One simple yet effective debugging technique is to keep track of how variables change as the
program runs using the print() function. By adding a print statement that outputs the value
of important variables inside a loop, we are able to get valuable information of how the variables
change over time, just like what we were able to do with trace tables for flowcharts. For instance,
the program below has been modified with the addition of line 9, which prints out the values of
index and acronym each time the loop repeats.
1 # Input
2 input_text = input("Enter the input text: ")
3
4 # Process
5 acronym = ""
6 for index in range(len(input_text)):
7 if input_text[index - 1].isspace():
8 acronym += input_text[index]
9 print("Debug: when index=" + str(index) + ", acronym=" + acronym)
10
11 # Output
12 print(acronym)
13
p Figure 6.30 Draft program with addition of print statement on line 10 (highlighted)
220
Module 4: Programming
Running this new program on Test Case 1 reveals how these two variables change over time as
characters are extracted from input_text to form the acronym.
Note that in this example, all the output starting with “Debug” appears together. In more complex
situations, however, different amounts of processing may occur between lines of debugging output,
which is why this technique is called using “intermittent” print statements.
From the debugging output, we can see that the first letter of “Science” is correctly extracted into
acronym when index reaches 9. However, the first letter of “Computer” is not.
221
How Do I Ensure That a Program Works as Intended?
Look again at line 7 in Program 6.15. When index is 0, the program is actually testing whether
input_text[-1](i.e., the last letter of input_text) is a space before deciding not to include
input_text[0] in the acronym. This is not the intended behaviour and is caused by the fact
that there is no “previous character” for the first character of a string.
Let us try to fix this bug by checking for this special case on line 7.
1 # Input
2 input_text = input("Enter the input text: ")
3
4 # Process
5 acronym = ""
6 for index in range(len(input_text)):
7 if input_text[index - 1].isspace() or index == 0:
8 acronym += input_text[index]
9 print("Debug: when index=" + str(index) + ", acronym=" + acronym)
10
11 # Output
12 print(acronym)
p Figure 6.31 Draft program with first character bug fixed by adding the highlighted code on line 7
222
Module 4: Programming
Running this amended program on Test Case 1 now produces the expected output (if we ignore
the lines of debugging output that start with “Debug”).
223
How Do I Ensure That a Program Works as Intended?
However, when we run the program on Test Case 2, once again we receive an unexpected output.
Fortunately, the cause of this bug is quite clear: we did not convert the output to upper case.
The program below fixes this by adding line 10 to convert the output to upper case when the
program has exited the loop.
1 # Input
2 input_text = input("Enter the input text: ")
3
4 # Process
5 acronym = ""
6 for index in range(len(input_text)):
7 if input_text[index - 1].isspace() or index == 0:
8 acronym += input_text[index]
9 print("Debug: when index=" + str(index) + ", acronym=" + acronym)
10 acronym = acronym.upper()
11
12 # Output
13 print(acronym)
p Figure 6.32 Draft program with letter case bug fixed by adding the highlighted code on line 10
224
Module 4: Programming
Running the amended program on Test Case 2 now generates the expected output (if we ignore
the lines of debugging output that start with “Debug”).
Let us continue running our program on the test cases we designed. The program behaves as
intended for Test Case 3 under the boundary conditions of having an empty str as input.
225
How Do I Ensure That a Program Works as Intended?
From the output, it looks like the acronym has some extra spaces that should not be there. To
investigate the cause of this bug, we shall decompose the program into smaller parts and test the
parts one at a time. This allows us to narrow down the possible locations of any bugs and reduce
the amount of code that we need to study at any one time.
226
Module 4: Programming
To test only one part of a program at a time, we can temporarily disable lines of code by inserting
a hash symbol (#) at the start of these lines. This process is called commenting out code since
it works by temporarily turning the code into comments. (Recall in section 4.3.2 on adding
comments to your program.)
Another method to break a program into smaller parts so that each part can be tested separately
is by putting each part in its own function. Since functions allow the same instructions to be
performed using different arguments, this makes it easy to test each part of the program repeatedly
using different inputs. For instance, Figure 6.33 shows how the tasks of getting input and testing if
a character should be included in the acronym can be broken out into two user-defined functions
named get_text() and should_include() respectively.
227
How Do I Ensure That a Program Works as Intended?
This modified program's overall behaviour is unchanged, so it still fails Test Case 4 by producing
output with some extra spaces that should not be there. We can proceed to narrow down where
the bug is located by decomposing the program line by line.
Since the letters “C” and “S” were extracted correctly, this implies that the following processes
are all working:
• Using get_text() to get input from the keyboard (lines 9–11)
• Iterating through the input characters (line 14)
• Adding characters to the acronym (line 16)
• Displaying the acronym on the screen (lines 19–20)
Hence, we can deduce that the error is located in should_include() as part of the code that
tests if the current character should be part of the acronym (line 15). We can also eliminate the
possibility that the error is located at the code converting the acronym to upper case (lines 17–18)
as it is not relevant in this case.
228
Module 4: Programming
To confirm the location of the bug, we can run mini test cases. All the lines except for the function
definitions are disabled by turning them into comments. At the end of the program, we then call
should_include() repeatedly with different combinations of input arguments to determine if
the function is working as expected.
p Figure 6.34 Testing a function with different input arguments to confirm location of bug
In this case, the expected outputs for the three calls to should_include() are False, False
and True. When text is " Computer Science " and i is either 0 or 1, we expect should_
include() to return False since both text[0] and text[1] are spaces and spaces should not
appear in acronyms. On the other hand, when i is 2 we expect should_include() to return
True since text[2] is precisely the first letter of the word "Computer".
229
How Do I Ensure That a Program Works as Intended?
However, when we run the program, we do not get the expected outputs.
True
True Failed
True
230
Module 4: Programming
This result tells us that the bug is definitely in should_include(). To fix this bug, we will add
an additional condition – the character being tested must not be a space.
p Figure 6.35 Draft program with unwanted spaces bug fixed by adding the highlighted code
CS
231
How Do I Ensure That a Program Works as Intended?
However, our program still does not pass Test Case 5 and Test Case 6 because it does not perform
any data validation. To fix this, a format check is added to the program to ensure that the input
phrase contains letters and spaces only, as shown in Figure 6.36.
232
Module 4: Programming
This final version of the program, Program 6.21, passes Test Case 5 and Test Case 6.
Since Program 6.21 passes all the test cases we have designed, we can confirm that it works as
intended in solving the acronym generation problem.
233
How Do I Ensure That a Program Works as Intended?
1. A new program has been written to solve the acronym generation problem. State
whether it passes or fails each of the six test cases we have designed for the acronym
generation problem.
1 # Input
2 input_text = input("Enter the input text: ")
3
4 # Process
5 acronym = ""
6 previous_char = ' '
7 for char in input_text:
8 if previous_char.isspace() and not char.isspace():
9 acronym += char.upper()
10 previous_char = char
11
12 # Output
13 print(acronym)
234
Module 4: Programming
Chapter Summary
Length Check
Using Intermittent
Range Check print Statements
Format Check
235
How Do I Ensure That a Program Works as Intended?
Review Questions
Table 6.34 Input and output requirements for the word reversal problem
Input Output
a) The main algorithm for the program consists of three parts. Read through the code and
state what each of the following parts of the program are intended to do:
i) Line 5
ii) Lines 7 to 11
iii) Lines 13 to 16
b) When this program is run on the following test case, the actual output does not match the
expected output:
Msgnitee on eetteuqit
i) State the kind of condition (normal, boundary or error) covered by this test case.
ii) State whether the program has a syntax error or a logic error and explain why.
iii) Find out where the error is located by disabling lines of code and testing the program
in chunks or parts. State the line on which the error is located and explain why the line
is incorrect.
iv) Rewrite the program to fix the error so that it passes the test case.
236
Module 4: Programming
2. The program in Figure 6.39 accepts a string of alphabetical letters only and outputs the string
with its upper-case letters converted to lower-case and vice versa. For example, if the input
were "comPUting", then the output would be "COMpuTING".
237
How Do I Ensure That a Program Works as Intended?
iii) Suppose an empty string should be treated as valid input for this program. Choose an
appropriate return value for is_letters_only('') and fill in the function body
for is_letters_only() accordingly. (To test your code, temporarily comment
out the main program and run test cases.)
c) Even with is_letters_only() correctly filled in, there is a bug in the program that
causes its output to be different from what is expected. For example, if the input were
"comPUting", the program will output "G" instead of the expected "COMpuTING".
i) Explain why a bug in convert_letter() can affect convert_string().
ii) Find the bug in convert_string() and suggest how it can be fixed.
238
7 Module 1: Data and Information
Alex learns about Siti’s situation and wants to help. He manages to find one of his old
book reports. Should he offer to let Siti submit his book report as her own?
Most programs are useful to us. However, not all programs perform tasks that are
desirable or beneficial to society. For instance, in this situation, Siti is the victim of a
program that used her email account without her permission and damaged her files.
When we write programs, we should anticipate and guard against the possibility that
these programs may be used in a harmful manner.
In this chapter, we will learn about data corruption and loss, unauthorised access to
data, threats to the privacy and security of data, as well as the concept of intellectual
property. We will also learn about the social and economic impact of computers in
various fields and explore ethical issues related to the use of computers.
239
How Can I Be a Safe and Responsible Computer User?
• Understand the effects of threats to privacy and security of data from spam, spyware,
cookies, phishing, pharming and unauthorised access, as well as defensive measures
employed such as the use of appropriate hardware and software.
• Describe ethical issues that relate to the use of computers, public and private networks,
freeware, shareware and open courseware; and the sharing of information.
• Compare the positive and negative social and economic impacts of technology in
education, communication (e.g., via mobile apps and social media), finance, medicine/
healthcare, transportation and entertainment; and explain plagiarism and software piracy.
If the corrupted data is not needed to read other data, then only the corrupted data itself is lost.
This situation is more likely if the amount of corrupted data is small. This is similar to having
smudged cells in a printed table of data – only the smudged data is lost.
240
Module 1: Data and Information
On the other hand, if the corrupted data is related to other data in the computer, then both the
corrupted data and its related data may be lost. This is because the corrupted data may contain
information that is required to read or interpret the related data. This situation is more likely if the
amount of corrupted data is large. This is similar to having smudged headers in a printed table of
data. While the cells of the table are still readable, it is not possible to interpret what the contents
mean, so the data stored in the table is meaningless and effectively lost.
p Figure 7.2 Data in the entire table is effectively lost due to the smudged header
In Siti’s case, she is probably unable to open her book report because one or more bytes in the
file that are required to read the rest of the file have been corrupted. Figure 7.3 shows a typical
error message that a user may get when trying to open a corrupted file in a word processor.
241
How Can I Be a Safe and Responsible Computer User?
Uninterruptible power
supply (UPS)
Device that provides enough
emergency power for a
computer to properly shut down
in case of a power failure
Table 7.1 Causes of and ways to prevent data corruption and loss
242
Module 1: Data and Information
Table 7.1 Causes of and ways to prevent data corruption and loss (continued)
243
How Can I Be a Safe and Responsible Computer User?
Besides losing her book report, Siti was also upset that
someone had used her computer and email account without Key Terms
her permission. Like Siti, most people want to have control
over how others access their data or property, such as online Authentication
accounts and personal information. For instance, most people Process of verifying the
would not want to share their passwords with others but would identity of a user
not mind sharing less private details such as their birthdates.
Authentication factor
Unauthorised access occurs whenever data owned by Category of evidence that
someone is used by someone else, such as an intruder or is used for authentication:
even a member of the public, without permission. Besides data something the user knows
loss, this can lead to many other undesirable consequences. or owns, or something
For instance, passwords or bank account information can that is measured from a
be used by an intruder to impersonate the owner’s identity, physical part of the user
steal his or her money, and commit fraud. An intruder can
also publicise private information that can lead to unwanted Unauthorised access
attention or bullying. Situation where data
owned by someone is
Unauthorised access can occur due to one or more of the used by someone else
following reasons: without permission
1. Poor authentication
2. Poor access control or authorisation
3. Poor understanding of privacy policies
7.2.1 Authentication
Authentication is the process of verifying the identity of a user. Authentication usually
requires the user to prove his or her identity by providing evidence from one or more of the
following categories:
1. Something the user knows, such as a password
2. Something the user owns, such as a mobile phone
3. Something unique that is measured from a physical part of the user, such as a thumbprint
Each category of evidence that is used for authentication is called an authentication factor.
244
Module 1: Data and Information
7.2.1.1 Passwords
The most common authentication method is to ask for a secret password or phrase that is known
only to the user. Most computer users are probably familiar with the process of entering a password
to use a computer or online account. Such passwords are usually entered together with a user
name that identifies who the user is claiming to be.
Unfortunately, passwords can be a poor form of authentication if they are chosen poorly or not
well-kept as a secret. Avoid using birthdates and surnames as passwords as they can be easily
guessed by an intruder. Instead, use hard-to-guess passwords that are a mixture of lower-case
letters, upper-case letters, numbers and symbols.
Avoid re-using passwords or leaving them unchanged for a long time as doing so makes it easier
for an intruder to guess the password. Instead, use unique passwords for each computer or online
account and update them at least once every 90 days.
More stringent authentication systems often require evidence from more than one authentication
factor. For instance, banks typically issue a device called a security token to users who wish to
access their bank accounts online.
245
How Can I Be a Safe and Responsible Computer User?
An OTP is usually a string of seemingly random numeric or alphanumeric characters. For security
reasons, an OTP is usually valid only for a short period of time, after which a new one has to be
requested. This reduces the possibility that the OTP is obtained by an intruder between the time
it is generated and when it is accepted by the bank.
OTPs may either be generated by the bank and sent securely to the user’s device, or generated
directly on the device using a secret algorithm that only the bank knows.
Two-factor authentication is stronger than using only a password as it is much more difficult for
an intruder to both guess a password and steal the user’s security token. This is why it is important
to keep the security token in a secure location at all times and to report a missing security token
as soon as possible.
However, if an OTP is sent wirelessly to the user’s mobile phone, it may be intercepted and used by
an intruder during the transmission process. Alternatively, if the secret algorithm used to generate
OTPs is poorly chosen or accidentally revealed, an intruder may find out how to generate OTPs
without needing the security token at all. Unfortunately, there is not much that a user can do to
prevent such intrusion attempts.
7.2.1.3 Biometrics
246
Module 1: Data and Information
Figure 7.5 shows some of the common human physical characteristics used in biometrics.
Voice Iris
DNA
Fingerprint
247
How Can I Be a Safe and Responsible Computer User?
Figure 7.6 shows an example of a file permissions dialogue box in the Windows and Mac
operating systems.
p Figure 7.6 Viewing file permissions in the Windows (left) and Mac operating systems (right)
Besides being able to override file permissions, an administrator is usually able to perform other
tasks related to authentication and authorisation, such as creating and removing user accounts
or resetting passwords.
248
Module 1: Data and Information
Unfortunately, managing permissions and administrative rights can be a complex task and it is
possible to unintentionally grant access to a file or administrative rights to an unauthorised user.
Such a user can then make use of such mistakes to gain unauthorised access to data and resources.
In addition, authentication for the administrator must be especially strong, as an intruder that
successfully claims to be the administrator can bypass file permissions entirely.
File permissions also do not prevent an intruder with physical access to a storage device from
accessing files or folders directly without going through the operating system. To prevent such
unauthorised access, it is necessary to use encryption (see section 7.2.2.3).
File permissions can be used as access control for both computers that are connected to a network
as well as computers that are not connected to a network but are shared by multiple users.
7.2.2.2 Firewalls
Firewall
Outside the network
Computer in a network
When properly configured, a firewall can protect the computers within a network from unauthorised
access. For instance, a firewall can be configured to block the transmission of data (known as
traffic) between any unauthorised senders and/or receivers, especially requests for data coming
from anonymous users on the Internet. This prevents intruders from gaining access to the computers
within a network.
249
How Can I Be a Safe and Responsible Computer User?
7.2.2.3 Encryption
Encryption is the process of encoding data so that a secret key is required to read the data. Like
passwords, the secret key is usually provided as a sequence of bytes. Before the encrypted data
is decoded using the secret key, it appears as random, meaningless data.
Encryption is often used to protect data from unauthorised access by allowing only authorised
users to have the secret key. It can also be used in combination with file permissions so that an
unauthorised user who is able to bypass file permissions would still be unable to use the accessed
data without knowing the secret key.
A website uses encryption if its address starts with “https://” and a padlock icon appears next to
its address on a web browser.
p Figure 7.8 A secure website is indicated by an address that starts with “https://” and a padlock icon
250
Module 1: Data and Information
Unfortunately, even as an increasing number of users share personal information such as photos
and location data using online services, many of them are still unfamiliar with the relevant
privacy policies or how such sharing habits may indirectly result in unauthorised access. A poor
understanding of the privacy policies of these services can often result in unauthorised access.
Social networking sites such as Facebook, Twitter, Instagram and TikTok are popular because
of their wide reach and convenience. They allow users to share photographs and information
quickly with their families and friends. They can also be used to help promote businesses or raise
awareness of particular campaigns or causes.
However, social networking sites can also pose many privacy concerns because most users do not
read or consider the repercussions of the privacy policies used by these sites regarding personal
information such as status updates, notes, photographs and location data. For instance, the privacy
policies for many social networking sites do not guarantee that the personal data they collect will
never be exposed to unauthorised users and may even require that some personal data be shared
with advertisers in order to use their sites. In this way, personal data can potentially be harvested
for spam and other threats to privacy (see section 7.3) that users did not authorise directly.
251
How Can I Be a Safe and Responsible Computer User?
Users of social networking sites should be aware that once any data is digitised and uploaded to
a public network such as the Internet, it can potentially remain there forever. This is because the
digital data is easily copied and may be republished in ways that are no longer under the control
of the original uploader.
Many users are also unaware that the privacy policies for some social networking sites do not
guarantee that their personal data will be deleted from the site completely or immediately even
after they close their account or remove the personal data from their account.
Personal data is sensitive and should not be shared publicly. Some companies may decide not to
hire candidates after reviewing the information and photographs posted on their social networking
accounts, even if this information was posted while they were still students in school.
Some good measures to prevent unauthorised access when using social networking sites:
• Read and fully understand the privacy policy of the social networking site.
• Set your sharing settings to “private” so that only people you know in real life can read
your posts.
• Think twice before posting any personal photographs or information that you may feel
uncomfortable sharing.
• Accept friend requests wisely. Make sure you know everyone in your friends list.
252
Module 1: Data and Information
253
How Can I Be a Safe and Responsible Computer User?
1. Search online to find out more about how biometric systems are used in schools to
track student attendance. State three advantages of using biometric systems over paper
attendance sheets.
2. Describe three measures that an e-commerce website can take to protect customer
data from unauthorised access.
254
Module 1: Data and Information
Term Description
• The stolen data can then be used for unauthorised access to even more
of the victim's data
• More difficult to detect than phishing as the fake website uses the same
address as the real website
Phishing • The use of emails and fake websites that appear to be from reputable
companies in order to steal personal information such as passwords and
credit card numbers from users
• The stolen data can then be used for unauthorised access to even more
of the victim's data
• The collected data can then be used for unauthorised access to even
more of the victim's data
255
How Can I Be a Safe and Responsible Computer User?
Term Description
• May arise as a result of other privacy and security threats that bypass
authentication and authorisation by exploiting software bugs or tricking
the user into performing harmful actions
• When the modified program is run by a user, the virus attaches copies of
itself to any other programs it can find, thus “infecting” them
256
Module 1: Data and Information
Operating system
While powerful, most anti-virus and anti-spyware programs
Software designed to
rely on a list of signatures, or unique evidence, for each
support a computer’s basic
known version of malware. This list needs to be updated
functions
regularly to ensure that the protection provided by these
programs continues to be effective against new malware.
Signature (malware)
For convenience, most anti-virus and anti-spyware programs
Unique evidence that is used
can update this list automatically through the Internet.
to detect a known version of
some malicious software
Some especially devious Trojan horses may appear to be
anti-virus and anti-spyware programs. To be safe, only anti-
virus and anti-spyware programs provided by reputable
companies or websites, or as part of the computer’s operating system should be trusted.
Viruses, spyware and Trojan horses typically require human interaction to start running, but
worms can run automatically. They typically do this by exploiting bugs or unintended behaviour
in otherwise legitimate programs that are already running on the computers. For instance, a
flawed web browser may have a bug that allows websites to run malicious programs without the
user’s knowledge.
To avoid such situations, it is important to update software regularly so that bugs that were
discovered since the last update can be fixed. This is especially important for software that is
used to interact with public networks such as the Internet, as data from public networks is more
likely to be malicious and designed to take advantage of known bugs.
257
How Can I Be a Safe and Responsible Computer User?
Phishing is a play on the word “fishing”. The idea is that bait is thrown out with the hope that
while most fish might ignore the bait, some will be tempted into biting. Similarly, phishing emails
are sent to many recipients in the hope that some recipients will eventually fall for the scam and
give away their personal data.
258
Module 1: Data and Information
Computer affected
by pharming Real bank website
For example, when victims of pharming enter the web address of their bank into a web browser,
they would be presented with a website that appears to be genuine but is actually provided by
the attacker’s web server. When they try to log in to the fake website, their user names, passwords
and account details would be recorded by the attacker, who can then use these details to access
the victims’ bank account on the bank’s actual website.
For pharming to be successful, the attacker must either have malware running on the victim’s
computer or have taken control of a network device such as a router or server. This can occur
as the software that runs on such devices is also susceptible to bugs.
Unlike phishing, it can be difficult to spot pharming attacks as everything seems to be normal
while the attack is taking place. Nevertheless, there are some measures that can be taken to
protect against pharming:
• Ensure that encryption (see section 7.2.2.3) is used when submitting credit card or other
sensitive information via the Internet. Clicking on the padlock icon that appears beside
the address bar usually displays more information to verify that the recipient is legitimate
and that encryption is working correctly.
• Regularly check bank, debit/credit card and other statements to ensure all transactions
are legitimate.
• Regularly update web browsers and the software running on network hardware so that all
known bugs are fixed.
• Make sure that two-factor authentication (see section 7.2.1.2) is enabled for all bank
transactions. This ensures that even if an attacker is able to access the bank account, no
unauthorised transactions can take place as the attacker would not be able to provide the
required OTP.
259
How Can I Be a Safe and Responsible Computer User?
In general, cookies are not malicious and are needed to keep track of authentication information
for identifying which user is currently logged in. However, they can also be used to keep track
of user movements and preferences within the website, such as which pages were most recently
visited by the user or how the user wants the site to be presented. Even worse, advertising
companies with advertisements on multiple websites can also use cookies to keep track of users
as they move from one website to another.
For users who want to keep their movement on the Internet private, the use of cookies in this
manner is undesirable. Thankfully, most web browsers have settings that allow users to manually
delete or prevent cookies from being created by untrusted websites. These settings can also be
configured to disable cookies or allow only selected websites to use cookies.
260
Module 1: Data and Information
To fully determine what information is being tracked by a website’s cookies and how that
information will be used, it is necessary to read and understand the website’s privacy policy (see
section 7.2.3).
1. You receive an unexpected email that advertises a free program. The advertisement
has links to download the program or to learn more about the product. The program
seems useful but you need more information. What should you do?
A Click on the link to download and run the program to try it out.
B Click on the link to learn more about the product.
C Close the email and search online for more information.
D Reply to the email and ask for more information.
2. Attackers may use spyware to monitor what you do on your computer. Which one of
the following is not likely to be a sign that your computer is affected by spyware?
A The light next to your computer’s camera cannot be turned off.
B Your computer’s network is disabled and there is no Internet access.
C Your computer seems to function more slowly than usual when it is online.
D Your web browser automatically visits an unfamiliar website at regular intervals.
3. State whether each of the following is a measure that can be taken to avoid spam:
a) Set up an email filter to block out unwanted emails.
b) Change web browsers regularly.
c) Use the same email address for schoolwork and online games.
d) Configure social networking accounts to keep email address information private.
e) Provide email addresses to a website only after checking its privacy policy to ensure
that personal information will not be shared.
f) Open email attachments as soon as possible.
4. A movie company uses the mobile phone numbers and email addresses of its
customers to send electronic tickets and to promote upcoming movies. As a result
of unauthorised access, an attacker manages to access the company’s collection of
mobile phone numbers and email addresses and releases this data to the public.
Suggest how such an incident may negatively affect the company and its customers.
5. Siti receives an email message from a stranger claiming that she has won a cash prize
from a lucky draw. The stranger wants to know her bank account details in order to
transfer the prize money to her. Suggest briefly what Siti should do.
261
How Can I Be a Safe and Responsible Computer User?
In the situation at the beginning of the chapter, Alex was considering whether he should let Siti
submit one of his old book reports as her own. Unfortunately, since the book report is meant to
represent each student’s individual work, it would be dishonest for Siti to claim Alex’s work as
her own.
Creative works such as Alex’s book report, songs or computer programs take effort to create and
are valuable as they benefit their owners. The owner of a song, for instance, can benefit from
feeling entertained by listening to the song. This is similar to how physical property such as a
chair or a house requires materials to produce and also benefit their owners. However, unlike
physical property, some works are creations of the mind and can exist purely as data with no
physical form. Such creative works are called intellectual property.
The owner of physical property such as a chair or a house can easily use locks or chains to prevent
the property from being stolen or used by others without permission. However, it is not as easy
to do the same for intellectual property such as music or
computer programs. For instance, while it is easy to copy
Key Terms
music files or programs from one computer to another, doing
so may be illegal if the owner of this intellectual property
Copyright
disallows it. The legal right of owners to control the use and
The legal right of owners
distribution of their intellectual property is called copyright.
to control the use and
In particular, copyright protects programmers against theft,
distribution of their
misuse and unauthorised copying of their software.
intellectual property
262
Module 1: Data and Information
Public domain software is not protected by copyright and Public domain software
anyone can legally copy, modify and distribute public Software where the legal
domain software. Public domain software also need not protections that are typically
always come with source code, although most do. granted to intellectual
property have either expired,
been surrendered or are
simply inapplicable
7.4.1.2 Free and Open-Source
Free and open-source software (FOSS) refers to software
where users are given freedom to change, copy, study and share the software. The “free” in
“free software” refers to the freedom to use and not the price. Like public domain software,
anyone can legally copy, modify and distribute FOSS. However, unlike public domain software,
the software is still protected under copyright and the copyright owners may use this protection
to require that the software must always be distributed with source code, that attribution to the
original authors must always be provided or that any modifications must use a similar licence if
they are also distributed.
There are many FOSS licences created by various institutions with varying levels of restrictiveness,
such as the Apache License, the Berkeley Software Distribution (BSD) License, the Massachusetts
Institute of Technology (MIT) License, the GNU Lesser General Public License (LGPL), and the
GNU General Public License (GPL). Knowledge of these licences and the differences between
them is not covered in this textbook.
263
How Can I Be a Safe and Responsible Computer User?
Besides software, other kinds of copyrighted works such as books, photographs and music can
be licensed in a similar manner using Creative Commons (CC) licences. Note, however, that
while CC licences are similar to FOSS licences in granting users the freedom to copy, modify
and distribute copyrighted works, CC licences are not designed for software and should not be
used for this purpose. On the other hand, CC licences are suitable for licensing content that is
delivered using software. For instance, higher-education course materials such as videos and notes
created by universities and distributed for free on the Internet, also known as open courseware,
often use CC licences.
The Linux operating system is an example of FOSS licensed under the GPL, in which the source
code is freely available for modification. The Python interpreter used in Chapter 4 of this textbook
is also distributed under an FOSS license.
Proprietary software
7.4.1.4 Freeware and Shareware Commercial software
for which most of the
Freeware is similar to proprietary software except that it is legal protections under
available for use at no cost. Some freeware are so-called copyright are retained
“lite” versions of proprietary software, which allow users
to try a limited version of the software while promoting the Shareware
full version. Similarly, shareware is demonstration software Demonstration software
that is distributed for free but for a specific evaluation period that is distributed for free
only. After the evaluation period, the program expires and but for a specific evaluation
the user can no longer access the program unless the user period only
pays a registration fee.
264
Module 1: Data and Information
Like proprietary software, the source code for freeware and shareware is usually kept secret and
modifying the software is usually illegal. However, unlike proprietary software, it may be legal to
copy and distribute freeware and shareware. Adobe Reader and Skype are examples of freeware,
while Camtasia Studio and WinRAR are examples of shareware.
If you suspect that you have unintentionally installed pirated software on your computer or device,
have a teacher or parent help you check whether the software you have installed is legal. In most
cases, pirated software can be replaced with an alternative open-source program that uses a less
restrictive licence and can be downloaded legally for free.
265
How Can I Be a Safe and Responsible Computer User?
In particular, users may even copy, modify or distribute copyrighted materials from the Internet
without realising that they have committed copyright infringement. Such infringement is illegal
even if the act of copying is unintentional. Students using materials from the Internet for schoolwork
should adhere to the following guidelines to avoid committing copyright infringement:
7.4.4 Plagiarism
Related to copyright infringement is plagiarism. Plagiarism is the
act of passing off someone else’s original work as your own. In Key Term
other words, it is the act of claiming to be the author of a piece of
work without providing proper credit or attribution to the actual Plagiarism
author. Unlike copyright infringement, plagiarism may not always Passing off someone
be illegal, but it is nevertheless an act of dishonesty and is usually else’s original work
a punishable offence when detected in schoolwork. as one’s own
On the other hand, some cases of copyright infringement are also cases of plagiarism. For example,
a student who copies more than 10% of copyrighted text and pictures from a website without the
owner’s permission and submits the copied materials in a school assignment without acknowledging
the actual author is committing both copyright infringement and plagiarism. Such acts are illegal
and dishonest.
266
Module 1: Data and Information
Published books and articles that use materials from other Citation
authors acknowledge and provide credit to these authors using Statement to
statements known as citations. Citations can also be used for acknowledge and
schoolwork to avoid plagiarism and give proper credit to the provide credit to
original authors of any reproduced work. There are multiple the author(s) of
standards for writing citations that differ by the relevant fields of reproduced materials
study and preferred conventions. Knowledge of these citation in published books
standards will not be covered in this textbook. and articles
1. For each of the following scenarios, state whether the person described is guilty of
copyright infringement, plagiarism, both or neither.
a) Alice takes her friend’s artwork and submits it for a contest as her own artwork
without her friend’s permission.
b) Bob installs a computer game on two different computers even though the licence
he purchased allows for one installation only.
c) Cindy purchases an educational licence for a spreadsheet program and installs the
program on her home laptop for school projects.
d) Dan downloads some public domain source code and submits it as his own for a
homework assignment.
So far in this chapter, we have learnt about the effects of data corruption and loss, the various
threats to privacy and security of data, as well as the legal and ethical issues surrounding intellectual
property. These topics have risen to prominence in recent years due to the rapid rate at which
technology has influenced our daily lives.
At this point, it is useful to look at the bigger picture and evaluate the overall impact of technology.
Since the World Wide Web’s invention in 1989 and the widespread availability of smart phones,
technology has completely changed the way we work, live and play.
Recently, with advancements in computing power and means of collecting data, computer scientists
have also developed algorithms that are successful in automating complex tasks such as classifying
images, making shopping recommendations and even generating works of art.
267
How Can I Be a Safe and Responsible Computer User?
Area Impact/Issues
• On the negative side, some people use the Internet to reinforce their
existing opinions or to spread rumours and misinformation. This is
worsened by the use of AI by some news and social networking sites
to promote content that the reader is likely to be interested in, since
most sites promote content based on how much they are able to elicit
responses and not by their level of accuracy.
Economic impact
• The rise of social media has led to the increased use of social media for
marketing purposes and has helped businesses to better understand
buying habits and consumer needs by analysing social media posts.
Ethical issues
• Is it right to collect and analyse social media posts in ways that the
original authors may not have intended?
268
Module 1: Data and Information
Area Impact/Issues
Economic impact
• The rise of open courseware has led to a large number of for-profit and
non-profit organisations that offer customised lessons on a variety of
topics for self-study online.
• Some traditional educators find that they need to retrain and pick up
new skills to better utilise these new technologies for education.
Ethical issues
269
How Can I Be a Safe and Responsible Computer User?
Area Impact/Issues
Economic impact
Ethical issues
• Should financial technology be limited to protect more
vulnerable users?
270
Module 1: Data and Information
Area Impact/Issues
• On the negative side, some patients find the use of robots and other
technology in healthcare impersonal and mistrust the ability of
machines to provide proper healthcare. Other patients may misuse
information from the Internet and make potentially dangerous
decisions based on incorrect diagnoses. Patients may also be
uncomfortable with the collection of medical data necessary to
improve the performance of healthcare-related AI.
Economic impact
Ethical issues
271
How Can I Be a Safe and Responsible Computer User?
Area Impact/Issues
• On the positive side, transport has become less stressful and more
predictable for travellers due to the availability of detailed maps as
well as real-time information on bus frequencies, traffic congestion
levels and street-level photographs of neighbourhoods around the
world. All this information is also available at low cost through popular
mobile devices. Location data from such mobile devices may also be
collected by AI to improve the accuracy and relevance of any map or
traffic data that is displayed.
Economic impact
• There are also multiple new companies that offer on-demand rides via
mobile applications. These developments have led to sweeping
changes in the taxi service industry as well as employment
opportunities for taxi drivers.
Ethical issues
272
Module 1: Data and Information
Area Impact/Issues
Economic impact
Ethical issues
273
How Can I Be a Safe and Responsible Computer User?
Chapter Summary
Effects
Ways Cookie Spyware
to Prevent
Causes and
Preventive Ways
Pharming Trojan Horse
DATA THREATS TO
CORRUPTION PRIVACY AND Phishing Virus
AND LOSS SECURITY
Spamming Worm
SAFE AND RESPONSIBLE
COMPUTER USE Types
Authentication
Understanding
Communication of Privacy Policies
INTELLECTUAL
Education PROPERTY Plagiarism
Finance
Transportation
274
Module 1: Data and Information
Review Questions
1. Recently, the customers of a bank received an email with “Transaction Advice” as the
subject. The email contained a hyperlink to a fake website which looked like the bank’s
real website. Some customers did not pay attention to the address of the hyperlink and
tried to log in as usual. As a result, their personal information was stolen.
a) Explain the difference between phishing and pharming.
b) State whether phishing or pharming occurred in this case.
c) Describe two ways how the bank customers could have avoided getting their personal
data stolen.
2. For each of the following threats, identify and explain whether unauthorised access is
likely to occur. For each threat where unauthorised access is likely to occur, suggest one
preventive measure that can be taken.
a) Cookies
b) Spam
c) Trojan horses
d) Viruses
3. Across the world, the music, television and film industries have been hit hard by acts
of piracy.
a) Describe two economic effects on the music and film industries if piracy is not curbed.
b) Siti enjoys watching a television series that is only available on an online video site.
After some research, she realises that she can download the television series onto her
computer hard drive so that she can watch the videos without going online. Explain
whether doing so is considered to be copyright infringement.
c) Alex finds a photograph of a wall mural on a website. He notices that the photograph
was labelled with the icon shown in Figure 7.12.
CC
p Figure 7.12 Icon used in a photograph’s label
275
How Can I Be a Safe and Responsible Computer User?
4. A company conducts a digital art competition where individual members of the public
are invited to upload their original artworks via a website. The uploaded artworks are
automatically stored on the website’s server.
a) Suggest two things that the company should do to prevent the uploaded artworks from
being lost due to sudden hardware or power failure.
b) After submissions are closed, the competition’s judges are surprised to discover that the
artworks submitted by two different members of the public are identical. Identify the
act of dishonesty that is likely to have taken place.
c) After choosing an artwork to win the competition, the judges are surprised again when
it is revealed that the chosen artwork was generated using a public domain artificial
intelligence (AI) program. Its creator reveals that the chosen artwork is actually the
automated result of combining content extracted from a public domain photograph
with textures extracted from a copyrighted painting. More precisely, the resulting
artwork depicts the same objects as in the photograph but is formed using colours and
textures copied from the painting. Figure 7.13 shows the chosen artwork and the two
source images that were used to generate it.
Public domain
photograph
Chosen
artwork
Copyrighted
painting
The artwork’s creator also submitted his artwork without asking permission from the
painting’s copyright owner. However, he claims that he did nothing wrong because his
artwork only uses a small portion of the original painting.
State whether you think the artwork’s creator should win the competition and explain
your reasoning.
276
8 Module 1: Data and Information
Siti notices that many colours are used in the website. She wonders how each colour
is represented by the computer and what may be some common ways of expressing
this information.
When we work with computers, we often need to express numbers in different ways.
This chapter will introduce the binary, denary and hexadecimal number systems. You
will learn about the purpose of each number system, the situations in which they are
used and how to convert numbers from one system to another.
277
How are Number Systems Applied in Real Life?
• Convert positive whole numbers from one number system to another – binary, denary
and hexadecimal; and describe the technique used.
• Describe situations in which the number systems are used such as ASCII codes, IP
(Internet Protocol) and Media Access Control (MAC) addresses, and RGB codes.
The number system that we are most familiar with and typically use is called the denary number
system. It is also known as the decimal number system and is made up of 10 distinct digits.
0 1 2 3 4 5 6 7 8 9
As the denary number system has 10 digits, it can also be called a base-10 system.
This is how we count using the denary number system:
0
1
2
278
Module 1: Data and Information
1289
hundred ten
thousands hundreds tens ones
Place thousands thousands
value
105 104 103 102 101 100
Denary
1 2 8 9
digit
Figure 8.3 shows how the value of 1289 can be calculated as the sum of each digit multiplied
by its place value:
Note that 1289 does not have any digits to the left of the
digit in the thousands place. The missing digits in the ten
Key Term
thousands place and higher are thus implied to be 0. These
implied digits are usually left out for denary numbers, but Leading zero
may sometimes be included, as leading zeros, when an Zero digit (0) that is to the left
exact number of digits is needed. For instance, 1289 may of the first non-zero digit in
be written as 001289 to use exactly six digits in the denary a number
number system. This may be useful to make numbers printed
in a column appear neater or easier to read.
279
How are Number Systems Applied in Real Life?
Figure 8.4 shows that 001289 still has a value of 1289 even with the leading zeros included:
p Figure 8.4 Calculating the value of a denary number with leading zeros
Denary numbers with many digits can also be difficult to read. To make interpreting such numbers
easier, denary numbers larger than 999 are often shown with a separator (either a space or
comma) after every group of three denary digits, starting from the right. For instance, the number
1234567890 can also be written as 1,234,567,890 or 1 234 567 890.
0 1
As the binary number system has two digits, it can also be called a base-2 system.
280
Module 1: Data and Information
The position of a digit in a binary number determines its place value, which is represented by
N
2 . The place value starts from 20 for the rightmost digit, with the power (N) increasing by 1 for
each subsequent digit on the left, as shown in Figure 8.6. This means that the place value of each
digit in the binary number system is two times the place value of the digit on its immediate right.
1101
To distinguish binary numbers from denary numbers, the binary numbers are appended the
number 2 as a subscript, like this: 11012. Alternatively, the binary number may be prefixed with
the characters “0b”, like this: 0b1101.
Just like in denary numbers, leading zeros do not affect the value of binary numbers but may still
be shown when an exact number of digits is needed. This is common when using binary numbers
in computer systems as each byte is defined to have exactly eight binary digits. Leading zeros are
often needed so that all eight binary digits of each byte are shown.
For readability, large binary numbers may also be shown with a separator (usually a space) between
groups of four digits, starting from the right. For instance, the binary number 100101102 can also
be written as 1001 01102.
Another acceptable way to write binary numbers is to enclose the binary digits in parentheses, like
this: (1101)2. This format is less ambiguous in situations where normal digits and subscript digits
may appear similar in handwriting.
256 128 64 32 16 8 4 2 1
Place
value
28 27 26 25 24 23 22 21 20
Binary
1 1 1 0 0 1
digit
281
How are Number Systems Applied in Real Life?
In this method, we divide the denary number repeatedly by 2. The remainders of each division
are then used to derive the binary number. The general algorithm is as follows:
Step 1: Draw a table with three columns – one column for denary numbers, one column for
the quotients and one column for the remainders.
Step 2: Fill in the denary number in the first row.
Step 3: Divide the denary number by 2 and fill in its quotient and remainder in the
same row.
Step 4: If the quotient is 0, proceed to Step 5. Otherwise, copy the quotient to the denary
number column of the next row and repeat Step 3.
Step 5: The equivalent binary number is the remainder column read from the bottom up.
135
2. Divide the denary number 135 by 2 and fill in its quotient and remainder in the same
row. In this case, 135 divided by 2 gives a quotient of 67 and a remainder of 1. As the
quotient 67 is non-zero, copy 67 to the denary number column of the next row and repeat
Step 3 of the algorithm.
282
Module 1: Data and Information
135 67 1
67
3. Divide the denary number 67 by 2 and fill in its quotient and remainder in the same row. In
this case, 67 divided by 2 gives a quotient of 33 and a remainder of 1. As the quotient
33 is non-zero, copy 33 to the denary number column of the next row and repeat Step 3
of the algorithm.
135 67 1
67 33 1
33
4. This sequence is repeated until the last iteration, when the denary number reaches 1. In this
case, 1 divided by 2 gives a quotient of 0 and a remainder of 1. As the quotient is 0, we
proceed to Step 5 of the algorithm.
135 67 1
67 33 1
33 16 1
Direction to
16 8 0
read the digits
8 4 0
4 2 0
2 1 0
1 0 1
283
How are Number Systems Applied in Real Life?
5. The converted binary number is the remainder column read from the bottom up. Hence,
the binary equivalent of 135 is 1000 01112.
In this method, we put 1s under the place values, starting from the left, so that the total adds up
to the value that we want to convert. The general algorithm is as follows:
Step 1: Write out the place value of each position in a binary number.
Step 2: Find the highest place value that is equal to or less than the denary number.
Step 3: Fill in the binary digit 1 below this place value.
Step 4: Subtract this place value from the denary number. If the result is 0, proceed to
Step 5. Otherwise, repeat Step 2.
Step 5: Fill in the binary digit 0 below any remaining place values.
Step 6: The equivalent binary number is formed by the row of binary digits below the
place values.
Binary
digit
2. Find the highest place value that is equal to or less than the denary number 135. In this
case, 27 (128) is the highest place value that is less than 135, so we place a 1 under 27, as
shown in Table 8.10.
3. Subtract the place value 128 from 135, which gives us 7, as shown in Table 8.10. As the
result is non-zero, we repeat Step 2 of the algorithm with the number 7.
Table 8.10 First iteration of Steps 2 to 4 in the sum of place values method
284
Module 1: Data and Information
4. Find the highest place value that is equal to or less than the denary number 7. In this case,
22 (4) is the highest place value that is less than 7, so we place a 1 under 22, as shown in
Table 8.11.
5. Subtract the place value 4 from 7, which gives us 3, as in Table 8.11. As the result is
non-zero, we repeat Step 2 of the algorithm with the number 3.
Table 8.11 Second iteration of Steps 2 to 4 in the sum of place values method
6. Following the same method, we place 1s under 21 and 20. As the result is now 0, we
continue with Step 5 of the algorithm.
Table 8.12 Last iteration of Steps 2 to 4 in the sum of place values method
256 128 64 32 16 8 4 2 1
Place
value
28 27 26 25 24 23 22 21 20
Binary
1 0 0 0 0 1 1 1
digit
285
How are Number Systems Applied in Real Life?
1. Convert the following denary numbers to binary using both the division by 2 and sum
of place values methods:
a) 54
b) 89
c) 100
0 1 2 3 4 5 6 7 8 9 A B C D E F
286
Module 1: Data and Information
As the hexadecimal number system has 16 digits, it can also be called a base-16 system.
0
1
2
Note that this method of counting also tells us that the digits A to F are equivalent to the denary
values of 10 to 15 respectively, as shown in Table 8.15.
Hexadecimal
digit 0 1 2 3 4 5 6 7
Denary
0 1 2 3 4 5 6 7
equivalent
Hexadecimal
digit 8 9 A B C D E F
Denary
8 9 10 11 12 13 14 15
equivalent
287
How are Number Systems Applied in Real Life?
The position of a digit in a hexadecimal number determines its place value, which is represented
by 16N. The place value starts from 160 for the rightmost digit, with the power (N) increasing by
1 for each subsequent digit on the left, as shown in Figure 8.9. This means that the place value
of each digit in the hexadecimal number system is 16 times the place value of the digit on its
immediate right.
1C6A
To distinguish hexadecimal numbers from binary and denary numbers, hexadecimal numbers are
appended the number 16 as a subscript, like this: 1C6A16. Alternatively, the hexadecimal number
may be prefixed with the characters “0x”, like this: 0x1C6A.
As with binary numbers, another acceptable way to write hexadecimal numbers is to enclose the
hexadecimal digits in parentheses, like this: (1C6A)16.
Just like denary and binary numbers, leading zeros do not affect the value of hexadecimal numbers
but may still be shown when an exact number of digits is needed. This is common when using
hexadecimal numbers in computer systems as each byte needs at most two hexadecimal digits
to be shown in full. Leading zeros are often needed so that each byte can be expressed using
exactly two hexadecimal digits.
For readability, hexadecimal numbers with more than three digits may also be shown with a
separator (usually a space) between groups of two or four digits, starting from the right. For
instance, the hexadecimal number 1C6A16 can also be written as 1C 6A16.
An easy way to convert values between different number systems is to use the Calculator program
in your computer. In Windows, you can find this program under the Accessories folder of the
Start Menu. In Mac, you can find this program in the Applications folder. For either version, set
the calculator to Programmer mode by clicking on the “View” menu and selecting “Programmer”
to perform conversions between different number systems.
288
Module 1: Data and Information
Hexadecimal
digit 0 1 2 3 4 5 6 7
Binary
0000 0001 0010 0011 0100 0101 0110 0111
equivalent
Hexadecimal
digit 8 9 A B C D E F
Binary
1000 1001 1010 1011 1100 1101 1110 1111
equivalent
Hexadecimal
digit 3 C
Binary
0011 1100
equivalent
3. Hence, as shown in Table 8.17, the binary equivalent of 3C16 is 0011 11002, which is the
same as 11 11002 if the leading zeros are omitted.
289
How are Number Systems Applied in Real Life?
Hexadecimal
digit C F 0 7
Binary
1100 1111 0000 0111
equivalent
As shown in Table 8.18, the binary equivalent of CF 0716 is 1100 1111 0000 01112.
Table 8.19 Split the binary number into groups of four digits, starting from the right
2. Since the leftmost group only contains two digits, we add two leading zeros to its left to
form a full set of four digits, as shown in Table 8.20.
Table 8.20 Add leading zeros to the first group from the left if it does not have four digits
290
Module 1: Data and Information
3. Using Table 8.16, we then convert each group of four binary digits into a hexadecimal
digit, as shown in Table 8.21.
Table 8.21 Converting each group of four binary digits into a hexadecimal digit
Hexadecimal
digit 2 8 6 B
Hexadecimal digit C 7 F
Denary equivalent 12 7 15
2. Multiply the denary equivalent of each digit with its place value as shown in Figure 8.10
and sum up the products.
291
How are Number Systems Applied in Real Life?
In this method, we divide the denary number repeatedly by 16. The remainders of each division
are then used to derive the hexadecimal number. The general algorithm is as follows:
Step 1: Draw a table with three columns – one column for denary numbers, one column for
quotients and one column for remainders.
Step 2: Fill in the denary number in the first row.
Step 3: Divide the denary number by 16 and fill in its quotient and remainder in the
same row.
Step 4: If the quotient is 0, proceed to Step 5. Otherwise, copy the quotient to the denary
number column of the next row and repeat Step 3.
Step 5: Convert the numbers in the remainder column to hexadecimal digits.
Step 6: The converted hexadecimal number is the remainder column read from the
bottom up.
1899
2. Divide the denary number 1899 by 16 and fill in its quotient and remainder in the same
row. In this case, 1899 divided by 16 gives a quotient of 118 and a remainder of 11. As
the quotient 118 is non-zero, copy 118 to the denary number column of the next row and
repeat Step 3 of the algorithm.
1899 118 11
118
292
Module 1: Data and Information
3. Divide the denary number 118 by 16 and fill in its quotient and remainder in the same
row. In this case, 118 divided by 16 gives a quotient of 7 and a remainder of 6. As the
quotient 7 is non-zero, copy 7 to the denary number column of the next row and repeat
Step 3 of the algorithm.
1899 118 11
118 7 6
4. Divide the denary number 7 by 16 and fill in the quotient and remainder in the same row.
In this case, 7 divided by 16 gives a quotient of 0 and a remainder of 7. As the quotient is
0, we will proceed to Step 5 of the algorithm.
1899 118 11
118 7 6
7 0 7
7 0 7 = 716
6. The converted hexadecimal number is the remainder column read from the bottom up.
Hence, the hexadecimal equivalent of 1899 is 76B16.
293
How are Number Systems Applied in Real Life?
In this method, we put hexadecimal digits under the place values, starting from the left, so that
the total adds up to the value that we want to convert. This method is similar to the method used
in section 8.2.2.2 to convert from denary to binary. However, it is significantly more complex as
there are more digits to work with in the hexadecimal number system compared to the binary
number system.
Hexadecimal
digit
2. Find the highest place value that is equal to or less than the denary number 1899. In this
case, 162 (256) is the highest place value that is less than 1899. Hence, under the 162
column, we fill in the largest hexadecimal digit such that its product with 162 is equal to
or less than 1899. Since 716 × 162 = 7 × 256 = 1792 (which is less than 1899) and
816 × 162 = 8 × 256 = 2048 (which exceeds 1899), the largest hexadecimal digit needed
here is 716, as shown in Table 8.29.
294
Module 1: Data and Information
3. Subtract the product 1792 from 1899, which gives us 107, as shown in Table 8.29. As the
result is non-zero, we repeat Step 2 of the algorithm with the number 107.
Table 8.29 First iteration of Steps 2 to 4 in the sum of place values method
4. Find the highest place value that is equal to or less than the denary number 107. In this
case, 161 (16) is the highest place value that is less than 107. Hence, under the 161 column, we
fill in the largest hexadecimal digit such that its product with 161 is equal to or smaller
than 107. Since 616 × 161 = 6 × 16 = 96 (which is less than 107) but 716 × 161 = 7 × 16 = 112
(which exceeds 107), the largest hexadecimal digit needed here is 616, as shown in
Table 8.30.
5. Subtract the product 96 from 107, which gives us 11, also shown in Table 8.30. As the
result is non-zero, we repeat Step 2 of the algorithm with the number 11.
Table 8.30 Second iteration of Steps 2 to 4 in the sum of place values method
6. Find the highest place value that is equal to or less than the denary number 11. In this case,
160 (or 1) is the highest place value that is less than 11. Hence, under the 160 column, we
fill in the largest hexadecimal digit such that its product with 160 is equal to or smaller
than 11. Since B16 × 160 = 11 × 1 = 11 (which is equal to 11) but C16 × 160 = 12 × 1 = 12
(which exceeds 11), the largest hexadecimal digit needed here is B16, as shown in
Table 8.31.
295
How are Number Systems Applied in Real Life?
7. Subtract the product 11 from 11, which gives us 0, also shown in Table 8.31. As the result
is 0, we proceed to Step 5 of the algorithm.
Table 8.31 Last iteration of Steps 2 to 4 in the sum of place values method
8. Fill in 0s under the remaining place values. In this case, all the place values after the first
non-zero hexadecimal digit are already filled.
1. Write down the hexadecimal and binary equivalents of the denary values from 32
to 47.
296
Module 1: Data and Information
This representation of colour is perfect for computers as we can use numbers to control the
intensity of each component. Each intensity value is stored in a single byte, which contains
eight bits. Hence, each value can only vary from 0000 00002 to 1111 11112. This corresponds to
0016 to FF16 and 0 to 255 in hexadecimal and denary respectively.
If all three colour intensities are 0, the resulting colour is black. If all three colour intensities are
at their maximum (FF16), the resulting colour is white. Table 8.32 shows some common colours
and the corresponding intensities of their red, green and blue components.
Table 8.32 Intensities of the red, green and blue components of some common colours
297
How are Number Systems Applied in Real Life?
An RGB colour code is displayed in the form of #RRGGBB, where RR, GG and BB are two-digit
hexadecimal numbers that represent the red (R), green (G) and blue (B) components of the colour.
Note that the six hexadecimal digits do not represent a single number but instead represent three
separate numbers with two hexadecimal digits each.
RGB colour codes are used to represent colours in websites, which are typically written using the
Hypertext Markup Language (HTML) and the Cascading Style Sheets (CSS) language.
While the Python language that you have learnt is specialised for describing instructions that a
computer performs, the HTML and CSS languages are specialised for describing documents that
are transmitted online. This textbook will not cover the HTML and CSS languages other than how
they use RGB colour codes to represent colours.
For instance, at the beginning of this chapter, Siti finds that the background of her school’s
website has an RGB colour code of #FF8000. This RGB colour code represents a colour with
a red component intensity of FF16 = 255, a green component intensity of 8016 = 128 and a blue
component intensity of 0016 = 0, which results in a shade of orange.
If Siti wishes to change this background colour to cyan, she first has to find out what the red, green
and blue component intensities of cyan are, and convert these values to two-digit hexadecimal
numbers. From Table 8.32, we see that cyan has a red component intensity of 0 = 0016, a green
component intensity of 255 = FF16 and a blue component intensity of 255 = FF16. Hence, the
RGB colour code for cyan is #00FFFF. Siti can use this colour code in the website’s HTML and
CSS codes to change the background colour to cyan. Table 8.33 shows some common colours
and their corresponding RGB colour codes.
Black #000000
Red #FF0000
Orange #FF8000
Yellow #FFFF00
Green #00FF00
Cyan #00FFFF
Blue #0000FF
Purple #800080
White #FFFFFF
298
Module 1: Data and Information
“IP” refers to a standard system of rules used by computers on the Internet to communicate
with one another. There are two versions of IP used today, namely IP version 4 (IPv4) and IP
version 6 (IPv6).
IPv4 address
(denary)
10.133.16.165
(0000 1010)2 (1000 0101)2 (0001 0000)2 (1010 0101)2
4 bytes = 32 bits
(binary)
An IPv4 address contains 32 bits, so the maximum number of possible IPv4 addresses is 232, or
approximately 4.3 billion. However, with the rapid growth of the Internet in the 1980s and 1990s,
it became obvious that this number of possible IPv4 addresses would not be enough.
299
How are Number Systems Applied in Real Life?
To provide a long-term solution to the shortage of IPv4 addresses, IP version 6 (IPv6) was introduced
in 1998, and expanded the size of IP addresses from 4 bytes (32 bits) to 16 bytes (128 bits). This
meant that the maximum number of IP addresses was increased to 2128 (approximately 3.4 × 1038),
which is large enough to overcome the problem of insufficient IP addresses for the foreseeable
future. Almost all computers connected to the Internet have an IPv4 address, and many use the
IPv6 address system as well.
Due to the increased length of IPv6 addresses, however, the old format of expressing IPv4
addresses was no longer suitable, as having 16 denary numbers separated by dots would take
up a maximum of 16 × 3 = 48 digits, which is too long.
As a result, a new format of showing IPv6 addresses using hexadecimal digits was developed.
In this new format, the 128 bits of an IPv6 address are divided into eight groups of 16 bits. Each
group of 16 bits can vary between 0000 0000 0000 00002 to 1111 1111 1111 11112, which is 000016
to FFFF16 in hexadecimal. As we can see, each group of 16 bits requires only four hexadecimal
digits. Therefore, an IPv6 address expressed in this format would only take up at most 8 × 4 =
32 digits (including leading zeros).
0010 0000 0000 0001 0000 1101 1011 1000 0000 0000 0000 0000
0000 0000 0000 0000 0000 0000 0010 0000 0000 0000 0001 0111
0000 1011 1010 1101 1100 0000 1101 1110
To make this format more compact, the leading zeros can be omitted. For instance, the IPv6
address in Figure 8.12 can also be written as 2001:db8:0:0:20:17:bad:c0de.
To be even more compact, a section of consecutive zeros can also be replaced with two colon
characters. For instance, the IPv6 address in Figure 8.12 can be written with the third and fourth
sections omitted as 2001:db8::20:17:bad:c0de.
300
Module 1: Data and Information
Consecutive zeros
IPv6 address
(consecutive
zeros replaced) 2001:db8::20:17:bad:c0de
p Figure 8.13 Shortening an IPv6 address
However, no more than one such section of consecutive zeros can be replaced with double
colons. For instance, 2001:db8:0:0:1:0:0:1 can be written as 2001:db8::1:0:0:1 or 2001:db8:0:0:1::1,
but not 2001:db8::1::1. Figure 8.14 illustrates this.
Invalid IPv6
address
2001:db8::1::1
p Figure 8.14 Only one section of consecutive zeros can be replaced in an IPv6 address
IP versions 0, 1, 2, 3 and 5 were experimental and not meant to be widely used. This is why only
IP versions 4 and 6 are in use today.
301
How are Number Systems Applied in Real Life?
Unlike IP addresses, which are used to direct transmitted data over the Internet, MAC addresses
are only used to direct data between devices in a local area network (LAN). MAC addresses are
not usually transmitted in full over the Internet. You will learn more about the difference between
the Internet and a LAN in Chapter 11.
MAC address
(hexadecimal) 20:17:0B:AD:C0:DE
(0010 0000)2 (0001 0111)2 (0000 1011)2 (1010 1101)2 (1100 0000)2 (1101 1110)2
0010 0000 0001 0111 0000 1011 1010 1101 1100 0000 1101 1110
6 bytes = 48 bits
(binary)
302
Module 1: Data and Information
To be precise, the MAC address only identifies the computer’s network interface controller (NIC)
and not the whole computer. Furthermore, while they are usually considered permanent, it is
actually possible to change or fake the MAC address of some NICs. You will learn more about
NICs in Chapter 11.
On Windows, you can view your computer’s IPv4 address, IPv6 address (if any) and MAC address
by opening Network and Internet Settings and clicking on the “View your network properties” link.
1 3
2
Example of
MAC address
Example of
IPv4 address
Example of
IPv6 address
p Figure 8.16 Viewing your computer’s IPv4, IPv6 and MAC addresses on Windows
On Mac, you can open the Network panel under System Preferences and click on the “Advanced…”
button. Your computer’s IPv4 address and IPv6 address (if any) can be found on the “TCP/IP” tab,
while your computer’s MAC address can found on the “Hardware” tab.
The first 32 ASCII codes (0 to 31) represent non-printing characters that are not written symbols
or the space character. These codes are instead used to control devices or to represent special
input such as typing the Backspace and Esc keys on the keyboard. The last ASCII code 127 is also
used to represent the special input of typing the Delete key on the keyboard.
Hexadecimal digits are often used to express ASCII codes as only two hexadecimal digits are
needed to express any ASCII code. This is more compact than using three denary digits or seven
binary digits.
303
How are Number Systems Applied in Real Life?
Table 8.34 shows how the ASCII codes from 32 to 127 may be expressed using the denary
(base-10), binary (base-2) and hexadecimal (base-16) number systems along with the corresponding
characters being represented.
304
Module 1: Data and Information
305
How are Number Systems Applied in Real Life?
Recall that, besides ASCII, an expanded standard called Unicode is used to represent characters in
languages other than English such as Tamil and Mandarin. Like ASCII, the Unicode standard uses
numbers to represent characters. However, while ASCII codes are defined to be exactly seven bits
long, Unicode is not as restrictive. In Unicode, the number of bits used to represent each character
can vary from 8 to 32 bits depending on the encoding scheme used. This flexibility means that
Unicode can be used to represent over a million unique characters from many different written
languages all over the world, which makes it a preferred choice for use over the ASCII standard.
The largest number that can be used to represent a character in Unicode is 1,114,111, which is
1 0000 1111 1111 1111 11112 and requires 21 binary digits. This means that if Unicode values are
stored directly as binary numbers, each character would take up 21 bits. Doing so would be wasteful,
however, as not all Unicode characters are used equally often. The designers of Unicode realised
that it would be more efficient to use fewer bits for frequently used characters at the expense of
using more bits for rarely used characters. This explains why the number of bits used to represent
a Unicode character can vary between 8 and 32.
1. Write down a possible RGB colour code for the colour magenta.
(Hint: Magenta can be formed by adding red light to blue light at maximum intensity.)
2. Write a program that accepts a string as input and outputs “Valid” if the input string is
a valid IPv4 address (i.e., four denary numbers between 0 and 255 separated by dots),
or “Invalid” if the input string is not a valid IPv4 address.
306
Module 1: Data and Information
Chapter Summary
APPLICATIONS OF
NUMBER SYSTEMS
Converting from
THE BINARY Hexadecimal to Binary
NUMBER SYSTEM
Converting from
Hexadecimal to Denary
Method 1: Method 2:
Division by 2 Sum of Place Values
Converting from
Denary to Hexadecimal
Method 1: Method 2:
Division by 16 Sum of Place Values
307
How are Number Systems Applied in Real Life?
Review Questions
1. Why is the hexadecimal number system preferred over the binary number system in
representing large numbers?
5. A lift system in a building uses an eight-bit register shown in Figure 8.17. The first four bits
indicate whether any of the four sensors has picked up a fault (denoted by the value 1) in
any of the subsystems. The last four bits indicate the lift number. There are 15 lifts in
the building.
8 4 2 1 Place values
Lift
number
Display Motor
sensor sensor
Lighting Ventilator
sensor sensor
308
Module 1: Data and Information
For example, the register shown in Figure 8.18 indicates that there are faults in the lighting
and motor subsystems for lift number 3.
0 1 1 0 0 0 1 1
p Figure 8.18 Register indicating that lift number 3 has faults in the lighting and motor subsystems
1 0 1 0 0 1 1 1
0 0 1 1 1 0 1 1
c) What would be the reading indicated by the register if there are faults in the display
and lighting subsystems for lift number 14? Give your answer as a binary number.
6. In a factory, there are 127 electrical points that provide power to various machinery. An
eight-bit register is used to represent the state of each electrical point. The first bit indicates
the operational state of the electrical point – a value of 1 means that it is operational and a
value of 0 means that it is faulty. The next seven bits indicate the electrical point number.
For example, the eight-bit register shown in Figure 8.21 indicates that electrical point
number 41 is operational.
64 32 16 8 4 2 1 Place values
1 0 1 0 1 0 0 1
309
How are Number Systems Applied in Real Life?
64 32 16 8 4 2 1 Place values
1 0 1 1 1 0 0 0
b) What would the register show if electrical point number 36 is faulty? Give your answer
as an eight-bit binary number.
c) Convert the number indicated by the register shown in Figure 8.23 to hexadecimal.
1 0 0 1 1 1 0 0
310
9 Module 2: Systems and Communications
We have already learnt how to write algorithms that output the result of decisions that
depend on one or more inputs. In this case, the output of whether Siti decides to take
the bus or to walk depends on two inputs: whether it is raining and whether she is late.
Like Siti, computers and other electrical systems use inputs to make decisions. However,
while Siti makes these decisions using her brain, computers and electrical systems make
these decisions using electrical circuits called logic gates. For instance, an automatic
lighting system may use logic gates to decide if the lights in a room should be switched
on based on whether the sun is shining and whether anyone is in the room. In computers,
the logic gates in their processors make the decisions based on the given inputs.
In this chapter, you will learn how logic gates make use of Boolean logic, the five different
types of logic gates and how logic circuits can be constructed from logic gates. You
will also learn about some applications of logic circuits.
311
How Do Logic Circuits Make Decisions?
• Construct the truth tables for given logic circuits (maximum three inputs).
• Design and construct simple logic circuits using AND, OR, NOT, NAND and
NOR gates.
In section 4.5.4, we learnt that Boolean values have only two possible values: True and False.
The study of Boolean values, called Boolean logic, was developed by the English mathematician
George Boole in 1847.
Boolean values are often used to determine the status of a system or a logical condition in an
algorithm. For example, the following algorithm produces an output of either “Pass” or “Fail”
based on whether the input mark is less than 50. The logical condition of whether “mark < 50”
in the decision symbol can be expressed using a Boolean value and must be either True or False
at any one time.
START
INPUT mark
STOP
312
Module 2: Systems and Communications
Besides True and False, Boolean values can be represented in other ways as shown in Table 9.1.
0 1
False True
Off On
Low High
No Yes
Boolean logic is very important as the data in a computer is stored as bits (binary digits) which
have a value of either 0 or 1. From Table 9.1, we see that 0 and 1 also correspond to the Boolean
values of False and True respectively. Hence, the data in a computer can be represented using
Boolean values, and common operations such as addition, subtraction, multiplication and division
are actually performed by the computer’s processor using Boolean logic.
The inputs and outputs of simple problems and decisions can each be represented using a single bit
or Boolean value as they are usually binary in nature. This means that there are only two possible
options for each input and output. For instance, in the example at the beginning of the chapter,
the input of whether it is raining can only be either True or False, with no other possible options.
Similarly, the output of Siti’s choice can only be either taking the bus or walking, with no other
possible options. For this chapter, we will consider all inputs and outputs to be binary in nature.
Table 9.2 Truth table for logic gate with one input
Input A Output
313
How Do Logic Circuits Make Decisions?
Table 9.3 Truth table for logic gate with two inputs
0 0
0 1
1 0
1 1
Table 9.4 Truth table for logic gate with three inputs
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
The output is determined by the type of logic gate, as described in the next section.
314
Module 2: Systems and Communications
Logical operation
An operation that acts on
9.3.1 OR Gate binary inputs to produce an
The OR gate has two inputs and one output. The logic output according to the laws
symbol and Boolean statement representing the OR gate of Boolean logic
are shown in Table 9.5. Boolean statements are also known
as logic statements.
A
OR Q Q = A OR B
B
The OR gate performs the OR operation, which gives an output of 1 when at least one of its
inputs is 1. The truth table in Table 9.6 illustrates this.
0 0 0
0 1 1
1 0 1
1 1 1
315
How Do Logic Circuits Make Decisions?
A
AND Q Q = A AND B
B
The AND gate performs the AND operation, which gives an output of 1 only when both inputs
are 1. The truth table in Table 9.8 illustrates this.
0 0 0
0 1 0
1 0 0
1 1 1
NOT A Q Q = NOT A
The small circle at the output of the NOT gate symbol represents an inversion.
The NOT gate performs the NOT operation, which inverts the input to its opposite state. Thus,
it is also known as the inverter gate.
316
Module 2: Systems and Communications
Input A Output Q
0 1
1 0
A
NOR Q Q = NOT (A OR B)
B
The NOR gate performs the NOR operation, which gives an output of 0 when at least one of its
inputs is 1. The truth table in Table 9.12 illustrates this.
0 0 1
0 1 0
1 0 0
1 1 0
A A
Q Q
B B
317
How Do Logic Circuits Make Decisions?
A
NAND Q Q = NOT (A AND B)
B
The NAND gate performs the NAND operation, which gives an output of 0 only when both
inputs are 1. The truth table in Table 9.14 illustrates this.
0 0 1
0 1 1
1 0 1
1 1 0
A A
Q Q
B B
p Figure 9.3 An AND gate followed by a NOT gate is equivalent to a NAND gate
318
Module 2: Systems and Communications
2. When using a NOR gate, which of the following conditions will return an output of 1?
A Both inputs are 0
B Both inputs are 1
C Either input is 1
D None of the above
3. When using a NAND gate, which of the following conditions will return an output
of 0?
A Both inputs are 0
B Both inputs are 1
C Either input is 0
D None of the above
319
How Do Logic Circuits Make Decisions?
The logic circuit in Figure 9.4 has three inputs (A, B and C), one intermediate input (D) and one
output (Q).
intermediate
input
inputs
A D
output
B Q
Consider the logic circuit in Figure 9.5. Let us use the steps above to generate its truth table.
A D
output
B Q
Step 1: Determine the total number of columns and rows in the truth table.
Since there are three inputs (A, B and C), one output (Q) and one intermediate
input (D):
Number of columns = total number of inputs, intermediate inputs and output = 5
3
Number of rows = 2 = 8
320
Module 2: Systems and Communications
Step 2: Draw the truth table and fill in all the input combinations.
Table 9.15 Filling in all the input combinations in the truth table
Intermediate
Input A Input B Input C Output Q
Input D
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Intermediate
Input A Input B Input C Output Q
Input D
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 0
1 0 0 0
1 0 1 0
1 1 0 1
1 1 1 1
321
How Do Logic Circuits Make Decisions?
Intermediate
Input A Input B Input C Output Q
Input D
0 0 0 0 0
0 0 1 0 1
0 1 0 0 0
0 1 1 0 1
1 0 0 0 0
1 0 1 0 1
1 1 0 1 1
1 1 1 1 1
A D
B Q
C E
In the previous section, you learnt that a logic gate can be represented by a Boolean statement. Like
a logic gate, a logic circuit can also be represented by a Boolean statement. Boolean statements
are useful in two ways:
1. They can be evaluated using truth tables to determine the output of a logic circuit.
2. They can determine the arrangement of the logic gates in a logic circuit.
322
Module 2: Systems and Communications
In mathematics, you would have learnt about order of operations in evaluating algebraic equations.
For instance, in 1 + 2 × 3, the multiplication operation is evaluated before the addition operation
to give the result of 7. A similar order applies when evaluating Boolean statements. The order of
operations for Boolean statements is summarised in Table 9.18.
1 Parentheses
2 NOT
3 AND
4 OR
We can evaluate it by expressing all combinations of its inputs and output using a truth table.
Step 1: Determine the total number of columns and rows in the truth table.
Since there are three inputs (A, B and C), one output (Q) and two intermediate inputs
((A AND B) and (A AND C)):
Number of columns = total number of inputs, intermediate inputs and output = 6
3
Number of rows = 2 = 8
Using the order of operations, the parts of the Boolean statement in parentheses are to
be evaluated first, followed by the part with the OR operation. This means that “A AND
B” as well as “A AND C” are evaluated first, from left to right, since they are contained
within parentheses. They form the two intermediate inputs, D and E.
Order 1 Order 2
Q = (A AND B) OR (A AND C)
Order 3
323
How Do Logic Circuits Make Decisions?
Step 2: Draw the truth table and fill in all the input combinations.
Intermediate Intermediate
Input Input Input Output
Input D Input E
A B C Q
(A AND B) (A AND C)
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
Intermediate Intermediate
Input Input Input Output
Input D Input E
A B C Q
(A AND B) (A AND C)
0 0 0 0 0
0 0 1 0 0
0 1 0 0 0
0 1 1 0 0
1 0 0 0 0
1 0 1 0 1
1 1 0 1 0
1 1 1 1 1
324
Module 2: Systems and Communications
Intermediate Intermediate
Input Input Input Output
Input D Input E
A B C Q
(A AND B) (A AND C)
0 0 0 0 0 0
0 0 1 0 0 0
0 1 0 0 0 0
0 1 1 0 0 0
1 0 0 0 0 0
1 0 1 0 1 1
1 1 0 1 0 1
1 1 1 1 1 1
The Boolean statement can be formed by creating an AND term for each input combination with
a final output of 1, and then ORing all the AND terms together.
325
How Do Logic Circuits Make Decisions?
0 0 0 0
0 0 1 0
0 1 0 0
0 1 1 1
1 0 0 0
1 0 1 0
1 1 0 1
1 1 1 0
Step 2: Create an AND term for each combination that results in an output of 1.
Since these conditions need to be all present for output
Q to be 1, we should AND them together. Note that
NOT A is used to represent A = 0. This is because when Key Term
A = 0, NOT A will give 1.
Sum-of-Product (SOP)
• ((NOT A) AND B) AND C
Boolean statement
• (A AND B) AND (NOT C) where the AND terms
are ORed together
Step 3: OR the AND terms together.
Since either of these two input combinations will result
in an output of 1, we should join the partial Boolean statements together using OR. Thus
we obtain the final Boolean statement below:
Q = (((NOT A) AND B) AND C) OR ((A AND B) AND (NOT C))
This form of representation where the AND terms are ORed together is known as
Sum-of-Product (SOP).
326
Module 2: Systems and Communications
Let us draw the logic circuit diagram represented by this Boolean statement:
Q = (A AND B) OR C
Recall that in Table 9.18, the AND operation is evaluated before the OR operation. This means
that the logic gate representing “A AND B” should be drawn first. The output of “A AND B” is
then connected as an input to an OR gate with input C.
A
B
A A AND B
B
p Figure 9.9 Connecting the output of the AND gate to the OR gate
A A AND B
B Q
C
327
How Do Logic Circuits Make Decisions?
Example 1:
Let us work out the Boolean statement of the logic circuit diagram shown in Figure 9.11.
A D
B Q
p Figure 9.11 Deriving the Boolean statement from a logic circuit diagram
To work out the Boolean statement of a logic circuit diagram, we can work backwards from its
final output.
Step 1: Work out the Boolean statement involving the final output.
An OR gate with inputs D and C is connected to the final output Q.
A D
B Q
Step 2: Work out the Boolean statement involving the intermediate input.
Notice that the intermediate input D is also the output D of an OR gate with inputs A
and B.
A D
B Q
328
Module 2: Systems and Communications
Example 2:
Let us now work out the Boolean statement of the logic circuit diagram in Figure 9.14.
A D
B
E
C
p Figure 9.14 Deriving the Boolean statement from a logic circuit diagram
Step 1: Work out the Boolean statement involving the final output.
An OR gate with inputs D and E is connected to the final output Z.
A D
B
E
C
Hence, we can work out the Boolean statement for this OR gate as:
Z = D OR E
Note that D and E are intermediate inputs.
329
How Do Logic Circuits Make Decisions?
Step 2: Work out the Boolean statements involving the intermediate inputs.
Notice that the intermediate input D is also the output D of a NOT gate with input A.
A D
B
E
C
A D
B
E
C
330
Module 2: Systems and Communications
Computing devices and electrical appliances perform logic decisions using logic circuits. This
section introduces three applications of logic circuits that are used to make logic decisions.
One example of a logic circuit used to operate motion-sensing lights is found in Figure 9.18.
Motion sensor
Motion = 1
No motion = 0 A
Light bulb
On = 1
Light sensor Q
Off = 0
Bright = 1 B C
Dark = 0
The corresponding Boolean statement for this logic circuit is Q = A AND (NOT B).
0 0 1 0
0 1 0 0
1 0 1 1
1 1 0 0
331
How Do Logic Circuits Make Decisions?
The motion-sensing light is switched on when both of its input conditions are fulfilled – its
surroundings are dark, and motion is detected within the toilet or office area. At all other times,
the light is switched off. This ensures that the light is not switched on when no one is around,
helping to reduce electricity consumption.
When the logic circuit is used to control a motion-sensing light, inputs A and B are provided by
a motion sensor and a light sensor respectively, and output Q is connected to a light bulb. Table
9.24 shows how the truth table should be interpreted for this circuit.
Table 9.24 Truth table for the logic circuit of a motion-sensing light
Intermediate
Input A Input B Output Q
Input C
We can then work out that there are two types of inputs (the key switch and remote control
2
sensor), resulting in 2 = 4 possible outputs for the lock.
Let us work out the truth table for the car door lock. If we connect inputs A and B to a key switch
and remote control sensor respectively, and the output Q to the lock, we can create the truth
table shown in Table 9.25.
332
Module 2: Systems and Communications
Hence, the logic circuit for this car door lock can be designed as follows:
Key switch
Key absent = 0 A
Key present = 1 Lock
Locked = 1
Remote control sensor Q Unlocked = 0
Signal absent = 0 B
Signal present = 1
In Figure 9.20, the light Q at the centre of a long corridor is controlled by switches A and B at
both ends of the corridor. Each switch has two states: 0 for “off” and 1 for “on”. Light Q also has
two states: 0 for “off” and 1 for “on”. Light Q will be on only when both switches A and B are
off or on concurrently. However, if only one switch is on and the other is off, light Q will be off.
Q
A B
p Figure 9.20 Light Q is controlled by switches A and B, which are placed at both ends of the corridor
333
How Do Logic Circuits Make Decisions?
If we connect the inputs of the logic circuit to switches A and B and the output to light Q, we
can create the truth table shown in Table 9.26.
0 0 1
0 1 0
1 0 0
1 1 1
To derive the Boolean statement from a truth table, recall the following two steps:
1. Create an AND term for each input combination that results in an output of 1.
2. OR the AND terms together.
Hence, the logic circuit for the two-way switches can be designed as follows:
A
B
334
Module 2: Systems and Communications
Chapter Summary
LOGIC CIRCUITS
AND DECISIONS
Order of Operations OR
Constructing
Truth Tables from
Boolean Statements
AND
Constructing
Boolean Statements
from Truth Tables
Constructing Logic NOT
Circuit Diagrams from
Boolean Statements
Constructing Boolean
Statements from Logic NOR
Circuit Diagrams
NAND
335
How Do Logic Circuits Make Decisions?
Review Questions
determine its:
a) Boolean statement; and
b) truth table.
3. Draw the truth table for the logic circuit shown below.
B
Q
336
10 Module 1: Data and Information
Alex, Bala, Siti and their classmates have to decide on the type of party to organise,
manage the costs of various party items and track the contributions made by attendees
to the party. These tasks may be difficult as there is a large amount of data that needs
to be collected, processed and analysed.
In the previous chapters, you learnt how computer programs can be written to help solve
such problems. A spreadsheet is a type of program that is useful for solving problems
that involve a large amount of data.
In this chapter, you will learn about the basics of using spreadsheets as well as how
they can be used to analyse data in various statistical contexts. You will also learn how
spreadsheets can be used to search for the best answer to meet a particular goal and
highlight details in a large set of data.
337
How are Spreadsheets Used to Process and Analyse Data?
• Use mathematical operators, functions and what-if data analysis (goal seeking) to prepare
spreadsheets and solve real-world problems such as:
– Find the total of a list of numbers, average of a list of numbers, minimum/maximum
of a list of numbers, square root of a number, simple interest and remainder after
division of numbers
– Round values
– Randomise values
– Convert data from one type to another (e.g., getting integer values from
decimal values)
– Count the number of data items
• Understand and use conditional statements (simple and nested): COUNTIF and IF with
relational and Boolean operators such as AND, NOT and OR.
• Use functions effectively to look up data in rows or columns (horizontal and vertical
table lookups) in a list or table for data processing.
Columns are labelled with letters and rows with numbers. Cell
The rectangular space located in a particular column and Space for data in a
row is called a cell. particular column and row
of a spreadsheet
Spreadsheet
Electronic worksheet used
to manage and manipulate
data arranged in columns
and rows
338
Module 1: Data and Information
Figure 10.1 shows a typical spreadsheet with a grid of cells that starts from column A and row 1
at the top-left corner and extends rightwards and downwards.
Column
Cell
Spreadsheet extends rightwards...
G H I J K ...
Row
...and downwards
13
14
15
16
17
18
..
339
How are Spreadsheets Used to Process and Analyse Data?
E2:G2
A1:C3
B5:B9
F8:F8
(or just F8)
340
Module 1: Data and Information
10:00:00 AM
341
How are Spreadsheets Used to Process and Analyse Data?
Most spreadsheet programs can automatically detect and change the data type of a cell based
on what is entered. For instance, entering “$2017” into a cell with a General data type will
automatically change it to use a Currency data type instead. However, the detected data type
may not always be what is intended by the user.
For instance, a user may want to enter “1-1” into a cell as Text, but if the cell initially has a
General data type, it will automatically be converted to use a Date data type instead. This can
be avoided by setting the cell to use a Text data type beforehand, or by entering an apostrophe
in front of the data like this: '1-1. Thus, it is a good practice to always set the data type of cells
to the most appropriate option before entering any values. You will learn how to do this as part
of data collection in section 10.2.1.
Some spreadsheet applications such as Microsoft Excel do not consider Currency and Date as
data types. Instead, they are treated as number formats that determine how a cell is displayed, but
not how it is actually stored. The difference between data types and number formats in Microsoft
Excel is not covered in this textbook.
342
Module 1: Data and Information
For example, in Figure 10.5, cell A1 has a constant with a value of 2017 while cell A2 has a
formula that calculates the sum of 2000 and 17. Cell A3, on the other hand, has a constant with a
text value of “2000+17” and does not have a formula. This is because the expression “2000+17”
was entered in cell A3 without an equals sign in front.
Constant (number)
Formula
Ctrl ` Constant (text)
To make a relative cell reference, use a cell address or range address directly in a formula. For
instance, in Figure 10.6, cell A5 uses relative cell references to calculate the product of the values
in cells A1, A2 and A3.
Ctrl `
343
How are Spreadsheets Used to Process and Analyse Data?
An important feature of relative cell references is that when a formula from one cell is copied
to another cell, all the relative cell references in the copied formula are changed so that they
maintain the same relative positions to the new cell.
In Figure 10.6, when cell A5 is copied to cell B5, the resulting formula in cell B5 automatically
has its relative cell references change from A1, A2 and A3 to B1, B2 and B3 respectively. This
allows repetitive calculations or relationships between cell values to be set up quickly by copying
the formula from one cell to multiple cells.
Suppose Bala is helping to manage the costs for the party described at the beginning of this chapter
using the spreadsheet shown in Figure 10.7. In row 2, the unit cost of balloons is $0.80 (cell D2),
so the total cost for balloons is $0.80 (cell D2) × 3 (cell B2) = $2.40.
Bala notices that the formula to calculate the total cost for subsequent rows follows the same
pattern of multiplying the quantity in column B by the unit cost in column D. Hence, instead of
entering the formula for rows 3 to 10 manually, he enters the formula “=B2*D2” into cell E2 and
copies it into cells E3 to E9.
Ctrl `
p Figure 10.7 Using relative cell references to calculate the total cost of party items
In Figure 10.7, the two relative cell references of B2 and D2 are automatically changed to B3 and
D3 for row 3, B4 and D4 for row 4, B5 and D5 for row 5, and so on for the subsequent rows.
Note that this occurs automatically to all relative cell references in the copied formula.
344
Module 1: Data and Information
An alternative to copying and pasting formulas manually is to use the “fill” feature of a spreadsheet.
Select the cell you wish to copy from. Notice the square on the bottom-right corner of the cell.
This is called the fill handle. Drag this square to cover the adjacent cells that you wish to paste
the formula into.
Fill handle
+
Ctrl `
p Figure 10.8 Dragging the fill handle (indicated by the small square) to copy C2 into C3:C9
Absolute cell references are used when we do not want the cell references in formulas to change
when copied to other cells. To make an absolute cell reference, type a dollar sign ($) before the
column letter or row number that should not change. This indicates that the particular column
letter or row number will not be automatically changed when the formula is copied into other
cells. For instance, in Figure 10.9, “$A$1” is an absolute cell reference that means the cell reference
to cell A1 does not change even when the formula is copied to another cell.
Ctrl `
345
How are Spreadsheets Used to Process and Analyse Data?
Suppose Alex is helping to track the contributions made by the attendees for the class party
using the spreadsheet shown in Figure 10.10. The amount each attendee needs to contribute is
$12.00 (cell C1).
The number of additional family members attending for Aisha is 1 (cell B4), so the total contribution
by Aisha and her family is 2 (cell B4 + 1) × $12.00 (cell C1) = $24.00.
346
Module 1: Data and Information
Without absolute cell references, Alex may enter the formula “=(B4+1)*C1” into cell C4. However,
Figure 10.11 shows that copying this formula into cells C5 to C17 will result in “=(B5+1)*C2”
for row 5, “=(B6+1)*C3” for row 6, “=(B7+1)*C4” for row 7, and so on. This is incorrect as the
formulas in these cells should only refer to the cell C1.
Cell references to
C1 are incorrectly
changed after pasting
p Figure 10.11 The cell reference to C1 should not change when copied to C5:C17
To prevent the row number 1 from changing in the cell reference “C1”, Alex should use the
absolute cell reference “C$1” instead. The dollar sign in front of “1” indicates that it will not be
automatically changed when the formula is copied into other cells. Hence, Alex can enter the
formula “=(B4+1)*C$1” into cell C4 before copying the formula into cells C5 to C17, as shown
in Figure 10.12. This results in the correct formulas “=(B5+1)*C$1” for row 5, “=(B6+1)*C$1”
for row 6, “=(B7+1)*C$1” for row 7, and so on. Note that since the column letter C should not
change as well, he can also use “$C$1”.
347
How are Spreadsheets Used to Process and Analyse Data?
Cell references to
C$1 correctly remain
as C$1 after pasting
p Figure 10.12 Using C$1 prevents the cell reference’s row number
from being changed when copied to other cells
You can think of the dollar sign as a pin or lock that prevents the column letter or row number
in the cell reference from being changed when copied to other cells.
C 6 $C$6
S 20 S$20
E 17 $E17
A 5 A5
p Figure 10.13 The dollar sign acts like a pin to prevent the column
letter or row number from being changed
348
Module 1: Data and Information
While editing a cell reference, you can press F4 to cycle through all the possible relative and
absolute versions of the cell reference. For instance, pressing F4 while editing the formula “=C2”
will cycle through “=$C$2”, “=C$2”, “=$C2” and back to “=C2” again.
The advantage of using cell and range references is that the spreadsheet can automatically
recalculate the formulas in all affected cells if any referenced cells are updated.
For instance, in Bala’s situation in Figure 10.14, updating the unit cost for balloons (cell D2) from
$0.80 to $0.50 will automatically recalculate the total cost for balloons (cell E2), which would
change from $2.40 to $1.50. This occurs because the spreadsheet already understands the
relationship between cells D2 and E2.
349
How are Spreadsheets Used to Process and Analyse Data?
Similarly, in Alex’s situation in Figure 10.15, updating the amount each attendee needs to contribute
(cell C1) from $12.00 to $10.00 will automatically recalculate all the cells from C4 to C17 using
the new amount. Once again, this is because the spreadsheet already understands how cell C1
is related to the calculations in range C4:C17.
…the amounts in
C4:C17 are automatically
recalculated
350
Module 1: Data and Information
351
How are Spreadsheets Used to Process and Analyse Data?
Suggest a formula for cell C2 that can be copied to range C3:C6 in order to complete
the spreadsheet correctly.
While the class party described at the beginning of this chapter involves only a small amount
of data, more complex tasks such as managing the accounts of a company require the handling
of vast amounts of data. Hence, it is important to manage this data effectively to make good
decisions. To do this, we need clear and efficient processes to collect, analyse and store data for
repeated use. This series of processes is called the data life cycle.
352
Module 1: Data and Information
Column headings
p Figure 10.19 Empty spreadsheet with proper column headings and data type settings
Since each column or field should only store one type of data, it is also a good practice to set the
data type of each column to the most appropriate option before any data is entered. For instance,
in Figure 10.19, the data type for the “Name” column should be set to “Text” while the data type
for the “Additional family members” column should be set to “Number” with zero decimal places.
353
How are Spreadsheets Used to Process and Analyse Data?
Example of a record
p Figure 10.20 A row of related data from multiple fields is called a record
Raw data may be imperfect due to input errors, missing data and/or contradictory data. For
instance, suppose the data for the class party is collected using paper forms before being entered
into a spreadsheet. Input errors may occur if a student’s name is misspelled, missing data may
occur if a required question is left blank and contradictory data may occur if two mutually
exclusive responses are both indicated (e.g., a student cannot claim to be both a vegetarian and a
non-vegetarian at the same time).
The data processing stage is similar to the process of input validation in programming and a
spreadsheet application typically has built-in functions that can help in data processing tasks.
You will learn about these functions in section 10.3.
354
Module 1: Data and Information
For example, the price of party items may be summed up to determine if there are enough
contributions to cover the total cost, or the mean score for a test may be calculated to decide
whether class performance has improved. Spreadsheets typically have built-in functions to
perform such data analysis. You will learn about these functions in section 10.3.
1. The spreadsheet template in Figure 10.21 is prepared for the data collection of purchases
made for the class party.
p Figure 10.21 Spreadsheet template for data collection of purchases for the class party
355
How are Spreadsheets Used to Process and Analyse Data?
10.3 Functions
Just like functions in Python, spreadsheet functions are predefined formulas used to perform
particular operations. Each function takes in zero or more inputs (arguments) and returns one
output (return value). Most spreadsheet applications have various categories of built-in functions.
In Microsoft Excel, these categories are found under the Formulas tab, as shown in Figure 10.22.
Functions can only be used in formulas. When using a function, type its name, followed by zero
or more comma-separated arguments enclosed within parentheses.
For more details on each function, open its function arguments window by selecting the appropriate
category under the Formulas tab and clicking on the function’s name as shown in Figures 10.23
and 10.24. You will learn about some of the most common functions in the following sections.
p Figure 10.23 Selecting the SUM() function from the “Math & Trig” category
356
Module 1: Data and Information
Operator Meaning
= Equal to (equivalence)
357
How are Spreadsheets Used to Process and Analyse Data?
In Python, we typically use the conjunction (and), disjunction (or) and negation (not) operators
to combine Boolean values and make decisions using if-else statements. In spreadsheets,
however, we use logical functions instead, as described in Table 10.3.
358
Module 1: Data and Information
Figure 10.25 shows a spreadsheet that tracks the attendees for the class party. Suppose Bala
would like to fill column C with information that indicates whether each classmate is bringing
more than three additional family members. To do this, he could enter “=B2>3” into C2 and copy
this formula into C3:C15.
In this case, using the greater than (>) relational operator produces a logical value that is either
TRUE or FALSE. Now, suppose Bala wishes for the column to use “Yes” and “No” instead of
“TRUE” and “FALSE”. He could use the IF() function to accomplish this, as shown in Figure 10.26.
Ctrl `
359
How are Spreadsheets Used to Process and Analyse Data?
As explained in Table 10.3, the IF() function returns the second argument if the first argument
is TRUE. Otherwise, it returns the third argument instead.
Table 10.4 shows some examples of how IF(), AND(), OR() and NOT() may be used to determine
and display different information about each classmate. In each of these examples, note that the
formula quoted is for the student in row 2 only (i.e., Aisha) and should be copied into rows 3 to
15 to show the same information for all classmates.
Information
Example Formula for row 2
to display
2 Whether Option A
each =IF(OR(B2<2, B2>4), "Yes", "No")
classmate
This formula returns “Yes” when the number of additional
has less
family members attending is either less than 2 or greater
than 2 or
than 4.
more than
Otherwise, it returns “No”.
4 additional
family
Option B
members
=IF(AND(B2>=2, B2<=4), "No", "Yes")
attending
Alternatively, since the information to display is exactly the
Result must
opposite of Example 1, we could also swap the responses
be either
of “Yes” and “No” from the formula given in Example 1.
“Yes” or
“No” only
Option C
=IF(NOT(AND(B2>=2, B2<=4)), "Yes", "No")
360
Module 1: Data and Information
Table 10.4 Examples of using IF(), AND(), OR() and NOT() (continued)
Information
Example Formula for row 2
to display
361
How are Spreadsheets Used to Process and Analyse Data?
362
Module 1: Data and Information
p Figure 10.27 The lookup value of 10 and column number of 3 are used to find the value “Mei Ling”
363
How are Spreadsheets Used to Process and Analyse Data?
Spreadsheets typically provide a VLOOKUP() function to perform these steps if you provide a
lookup value, a lookup table and a column number. For spreadsheets that use rows for fields
and columns for records, the HLOOKUP() function can be used instead. Note that VLOOKUP()
performs a vertical search while HLOOKUP() performs a horizontal search. These functions are
described in Table 10.7.
Note that the last argument to both HLOOKUP() and VLOOKUP() is a logical value named range_
lookup that determines whether an exact match or approximate match is used.
An exact match looks for the exact lookup value and returns the error value #N/A if no match is
found. An approximate match, on the other hand, looks for the largest value that is less than or
equal to the lookup value. It only returns an error value if all the values in the first column or row
of the lookup table are larger than the lookup value.
364
Module 1: Data and Information
When using an exact match, the only way to make sure that all lookups are successful is to include
all possible lookup values in the lookup table. This can make the lookup table very long. Using
an approximate match lets us shorten the lookup table as the table does not need to contain
the exact lookup values. This is useful if a single row or column of the lookup table is used for
multiple lookup values, as explained in the following examples.
Figure 10.28 shows how VLOOKUP() is used to look up the names of three students based on
their registration numbers (5, 6 and 9).
Lookup
table
These grades are obtained from Lookup Column
A2:C15 using VLOOKUP() value number
Ctrl `
In this case, the first argument for each use of VLOOKUP() is a registration number in column E
(that is, E2, E3 or E4). This is the lookup value that we are searching for.
The second argument for all three uses of VLOOKUP() refers to the class list in A1:C15. This is
the lookup table that we wish to search through and retrieve a value from. (An absolute range
reference is used here to make copying the formula to new rows easier.)
Finally, the third argument of 3 tells VLOOKUP() to return the value in the third column of the
lookup table if it finds a match. This is because the names we wish to retrieve are in the third
column of the lookup table.
365
How are Spreadsheets Used to Process and Analyse Data?
Figure 10.29 shows how VLOOKUP() can also be used to convert rounded percentage scores into
grades (such as A1, A2 and B3). However, the lookup table in this case is very long as it includes
every possible rounded percentage score from 0 to 100.
Lookup
table
Lookup Column These grades are obtained from
value number D2:E102 using VLOOKUP()
Ctrl `
Lookup
table is 102
rows long!
p Figure 10.29 Using VLOOKUP() to convert rounded percentage scores into grades
However, notice that the entries in the second column of the lookup table are the same for every
value in the first column from 0 up to but not including 40, from 40 up to but not including 45,
from 45 up to but not including 50, and so on. This indicates that we can achieve the same result
using approximate matching and a shorter lookup table that only includes the boundaries for
the different grades in the first column. This works because approximate matching looks for the
largest value that is less than or equal to the lookup value.
Approximate matching is used by default when the fourth argument is left out. Nonetheless, to
make it clear that we wish to use approximate matching, we shall include the fourth argument
and set it to TRUE, as shown in Figure 10.30.
366
Module 1: Data and Information
Lookup
table range_lookup These grades are obtained from
Lookup Column D2:E10 using VLOOKUP()
value number with approximate matching
p Figure 10.30 Using approximate matching to convert rounded percentage scores into grades
Figure 10.30 shows how VLOOKUP() with approximate matching can be used to convert rounded
percentage scores into grades (such as A1, A2 and B3). For example, the formula in cell B2 looks
for the largest value in column D (the first column) that is less than or equal to the lookup value of
98, i.e., the value of 75, which is found in row 10. It then returns the value found in column E (the
second column) of the same row, i.e., “A1”. Hence, the formulas in column B are able to return
valid results even though most of the values in column A do not have exact matches in column D.
On the other hand, if the fourth argument is set to FALSE, VLOOKUP() will perform an exact
match instead, as shown in Figure 10.31. In this case, only the scores of 65 and 40, which exactly
match rows 8 and 3 of range D2:E10 respectively, will have a valid result. The other cells will
return the error value #N/A.
Lookup
table range_lookup
Only the percentages 65 and
Lookup Column 40 have valid results using
value number VLOOKUP() with exact matching
Ctrl `
p Figure 10.31 Using exact matching returns an error value if an exact match cannot be found
367
How are Spreadsheets Used to Process and Analyse Data?
Figure 10.32 shows how VLOOKUP() with approximate matching can be used to group BMI values
into the Underweight, Acceptable and Overweight categories.
Lookup
table range_lookup
These results are obtained from
Lookup Column D2:E4 using VLOOKUP()
value number with approximate matching
Ctrl `
p Figure 10.32 Using VLOOKUP() with approximate matching to categorise BMI values
Suppose that the lookup table D2:E4 is now “flipped” such that rows are used for fields and
columns are used for records instead. This means that the BMI values of 0, 16 and 27 are now
on row 1 and the categories “Underweight”, “Acceptable” and “Overweight” are now on row
2. In this case, we will have to use HLOOKUP() instead of VLOOKUP() to perform the same task.
Lookup
table range_lookup These results are obtained from
Lookup Row E1:G2 using HLOOKUP()
value number with approximate matching
Ctrl `
Lookup table E1:G2 has rows
and columns swapped
p Figure 10.33 Using HLOOKUP() with approximate matching to categorise BMI values
When using HLOOKUP(), the first two arguments still refer to the lookup value and lookup table
range respectively. However, the third argument is now a row number instead of a column
number. In this case, the row number is still 2 as the groupings we want to retrieve are on the
second row of the lookup table.
368
Module 1: Data and Information
Note that for approximate matching to work, the first column (for VLOOKUP()) or row (for
HLOOKUP()) of the lookup table must have its values arranged in increasing order. For instance,
the values in the ranges D2:D10 in Figure 10.30 and D2:D4 in Figure 10.32 are arranged in
increasing order from top to bottom.
Similarly, the values in range E1:G1 in Figure 10.33 are arranged in increasing order from left to
right. Exact matching, on the other hand, does not require the values in the first column or row
of the lookup table to be arranged in increasing order.
369
How are Spreadsheets Used to Process and Analyse Data?
370
Module 1: Data and Information
371
How are Spreadsheets Used to Process and Analyse Data?
372
Module 1: Data and Information
373
How are Spreadsheets Used to Process and Analyse Data?
374
Module 1: Data and Information
1. The spreadsheet in Figure 10.34 is used to tabulate costs for a class party.
a) Suggest a formula for cell E2 that can be copied into range E3:E9 in order to complete
the spreadsheet.
375
How are Spreadsheets Used to Process and Analyse Data?
b) Suggest a formula for cell E10 to calculate the total cost from the subtotals.
c) Predict the value that will be returned for the following formulas:
i) =COUNTIF(B2:B9,">3")
=IF(A6="Drinks", IF(B6<10, "A", "B"), "C")
ii)
2. The formulas below are two ways of comparing the value in cell A1 with the number
3. They both return a text value of “Less than 3”, “Equal to 3” or “Greater than 3”.
• =IF(A1<3, "Less than 3", IF(A1=3, "Equal to 3", "Greater than 3"))
• =IF(A1<3, "Less than 3", IF(A1>3, "Greater than 3", "Equal to 3"))
Suggest two other formulas that can achieve the same result by rearranging the IF()
functions, arguments and/or changing the conditions.
Timings of one minute or less get a score of 5. Timings between one minute and three
minutes get a score of 2. Timings exceeding three minutes get a score of 1.
a) Suggest a formula for cell C2 that can be copied to range C3:C6 in order to look up
the correct score from E2:F4. The formula must not require any further changes
after it is copied into C3:C6.
b) Suggest a formula for cell C8 to calculate the average of the five scores.
c) Suggest a formula for cell C9 to calculate the median of the five scores.
376
Module 1: Data and Information
4. The spreadsheet in Figure 10.36 is used to track the weight readings of students in
a class.
a) Suggest a formula for cell C2 that can be copied to range C3:C15 in order to round
the weight readings to one decimal place.
b) Suggest a formula for cell E17 to count the number of students with a rounded
weight reading that is less than 60 kg.
c) Suggest a formula for cell E18 to calculate the range of rounded weight readings.
377
How are Spreadsheets Used to Process and Analyse Data?
As you have learnt, a spreadsheet is able to perform automatic recalculation of all affected cells if
any referenced cells are changed. We can use this ability to answer “what if” questions and predict
the outcomes that may occur when particular values are changed. In particular, a spreadsheet lets
us search for the best answer to meet a particular goal (using the Goal Seek feature).
For instance, suppose we have a spreadsheet that calculates the BMI (cell C2) of a student, given
his height (cell A2) and weight (cell B2) using the formula:
BMI = (Weight in kg) / (Height in m)2
As shown in Figure 10.37, the student is currently overweight as his BMI is above 27 (based
on the BMI guideline for 15- to 16-year-olds). To find out what his weight must be to reach an
acceptable BMI of 27, we can use the Goal Seek feature that can be found under the “What-If
Analysis” menu of the “Data” tab.
Figure 10.38 shows what the Goal Seek window looks like when it is first opened.
378
Module 1: Data and Information
In this case, we want to set the BMI (cell C2) to 27 by changing the weight (cell B2). Figure 10.39
shows how these requirements can be entered into the Goal Seek window.
After clicking on “OK”, the spreadsheet automatically calculates the value of 56.77 for the student’s
weight in order to achieve a BMI of exactly 27, as shown in Figure 10.40. This means that the
student with a height of 1.45 m can only reach a BMI of 27 by reducing his weight to 56.77 kg.
379
How are Spreadsheets Used to Process and Analyse Data?
The spreadsheet in Figure 10.41 collates the expected attendance, funding and costs for the class
party described at the beginning of this chapter.
Formulas are used to calculate the total funding (cell C18) based on the amount each attendee
is expected to contribute (cell C1).
Formulas are also used to calculate the total cost (cell I12) based on the quantities of items needed
to organise a potluck. In particular, the formula in cell I1 shows whether the class party makes a
net profit or loss by calculating the difference between total funding and total cost.
Suppose the organisers of the class party wish to change the contribution per attendee so that the
class party breaks even exactly and neither makes nor loses money. This means that we should
set the difference between total funding and total cost (cell I1) to 0 by changing the contribution
per attendee (cell C1), as shown in Figure 10.42.
380
Module 1: Data and Information
p Figure 10.42 Using Goal Seek by setting the value for cell I1 to 0
After clicking on “OK”, the spreadsheet automatically calculates that each attendee should
contribute $3.00 in order to break even, as shown in Figure 10.43.
p Figure 10.43 The solution (contribution per attendee) found by Goal Seek
381
How are Spreadsheets Used to Process and Analyse Data?
To create a rule, select the range of cells that the rule should be
applied to, then choose “Conditional Formatting” under the “Home” tab, as shown in Figure 10.44.
The most common cases and options for using conditional formatting are listed under this menu.
More complex rules can be created using the “New Rule…” option while existing rules can be
edited or removed using the “Manage Rules…” and “Clear Rules” options respectively.
382
Module 1: Data and Information
For instance, suppose we wish to highlight the cells in the second column of the spreadsheet in
Figure 10.45 that contain a number that is less than 3.
p Figure 10.45 Spreadsheet for tracking class party contributions by each classmate’s family
383
How are Spreadsheets Used to Process and Analyse Data?
Instructions
Step 1:
Select the range of cells that the new rule should be applied to.
In this case, we select the cells containing numbers under the second column (range
B4:B17).
Step 2:
Select “Conditional Formatting” under the “Home” tab.
Since we wish to highlight the cells that contain a number that is less than 3, we can
select “Highlight Cells Rules” and choose the “Less Than…” option.
384
Module 1: Data and Information
Instructions
Besides varying the font, fill and border of cells, we can also use the “Data Bars”, “Color
Scales” and “Icon Sets” (outlined in green) options to display tiny bar graphs called data
bars, or icons that update automatically based on each cell’s contents.
Step 3:
Complete the “LESS THAN” condition by entering a value of 3.
(Note that cell references can also be used here instead of constants. Relative cell
references will be changed relative to the top-leftmost cell of the range when applied to
other cells in the range.)
The box outlined in green describes how cells that match the condition will be formatted.
Besides the default options, we can also choose the “Custom Format…” option to
customise the formatting.
385
How are Spreadsheets Used to Process and Analyse Data?
Instructions
In this case, we will use the default option of “Light Red Fill with Dark Red Text” and
click “OK”.
Result:
The cells under the second column which contain a number less than 3 are now
highlighted using a light red fill and dark red text, as expected.
Note that changing the contents of highlighted cell to a number that is 3 or more will
automatically remove its highlighting. Similarly, changing the contents of a
non-highlighted cell in B4:B17 to any number that is less than 3 will automatically
highlight it.
386
Module 1: Data and Information
If we select the “Manage Rules…” option under the conditional formatting menu while the
highlighted cells are selected, we see that there is now a rule applied to range $B$4:$B$17 that
formats any cells in this range containing a value less than 3 with a light red fill and dark red text,
as shown in Figure 10.46.
To remove a conditional formatting rule, select it in the Conditional Formatting Rules Manager
window and click “Delete Rule”. To change the rule’s range, condition and/or highlighting style,
select it and click “Edit Rule…”.
Sometimes, none of the common cases and options under the “Conditional Formatting” menu
are suitable for what is required. In that case, we will need to create a custom rule by selecting
“New Rule…” under the conditional formatting menu.
Suppose we wish to highlight the cells in the first column of the spreadsheet in Figure 10.47 that
contain a name starting with the letter M.
387
How are Spreadsheets Used to Process and Analyse Data?
Instructions
Step 1:
Select the range of cells that the new rule should be applied to.
In this case, we select the cells containing the names under the first column (range
A4:A17).
388
Module 1: Data and Information
Instructions
Step 2:
Select “Conditional Formatting” under the “Home” tab.
We wish to highlight the cells that contain names starting with the letter M. Unfortunately,
none of the common options under the “Conditional Formatting” menu appear suitable
for this requirement.
This means that we will need to create a custom rule by selecting “New Rule…”.
389
How are Spreadsheets Used to Process and Analyse Data?
Instructions
Step 3:
The New Formatting Rule window will appear.
None of the first five options appear suitable for checking whether any cells start with
the letter M, so we will select the last option: “Use a formula to determine which cells
to format”
Step 4:
Now we need a formula that will produce a logical value based on whether the first letter
of the name in each cell is the letter M.
We always write custom formulas for conditional formatting rules relative to the top-left
cell of the range. In this case, it is cell A4.
To check whether the first character of A4 is the letter M, we use the formula
“=LEFT(A4, 1)="M"”.
We then customise the formatting to have a yellow fill and click “OK”.
390
Module 1: Data and Information
Instructions
Result:
Cells under the first column containing a name which starts with the letter M are now
highlighted using a yellow fill.
391
How are Spreadsheets Used to Process and Analyse Data?
Chapter Summary
Data Data
Processing Distribution
SPREADSHEETS
UNDERSTANDING
FUNCTIONS
SPREADSHEETS
Logical Functions
Cell and Range
Text Functions Addresses
Date Function
Math Functions
392
Module 1: Data and Information
Review Questions
1. The spreadsheet in Figure 10.48 is used to tabulate the monthly interest earned and money
withdrawn from a bank account.
p Figure 10.48 Interest earned and money withdrawn from a bank account
393
How are Spreadsheets Used to Process and Analyse Data?
Create this spreadsheet with the formulas shown in Figure 10.58 using a spreadsheet
program, then answer the following questions:
a) Suggest a formula for cell C2 that can be copied into range C3:C13 in order to
calculate the interest earned for each month before money is withdrawn, using the
interest rate that is specified in cell G1.
b) In the program, use Goal Seek to determine what the monthly interest rate must be to
have $4,200 left at the end of December. Give your answer to two decimal places.
c) In the program, use conditional highlighting to automatically colour the text of cells in
column D red if they show a withdrawal amount greater than $100.
394
11 Module 2: Systems and Communications
How Do I Create a
Simple Network?
Siti’s family has several devices, such as laptops, mobile phones, tablets and a wireless
printer. She wants to create a simple network to connect all these devices together in
her new home so that data can be transferred between them. Previously, she could only
print her assignments from her brother’s desktop computer but not from her laptop.
She would like to print from both devices.
In previous chapters, we learnt about ethical issues that relate to the use of computers
in public and private networks. The world is becoming increasingly connected with the
growing number of network hardware being linked to the Internet. Computer networking
has enabled the fast and effective transfer of data, which is essential for the daily
operations of businesses and our way of life.
In this chapter, we will learn about what makes up a network and the different types
of networks. We will also learn about the practical concerns of implementing a wired
and a wireless network, the differences between client-server and peer-to-peer network
architectures, and the components of a simple home network.
395
How Do I Create a Simple Network?
• Describe the difference between wired and wireless networks and explain the factors
that will determine the use of each type of network.
• Describe the components for a simple home network and design a simple
home network.
396
Module 2: Systems and Communications
Advantages Disadvantages
• Shared storage: Data files can be • Risk of data loss: Data may become
stored on a central server for ease of lost due to hardware failures or errors.
access and backup purposes. Using a network means regular data
backups are needed.
• Communication: Computers in the
same network are often able to share • Server outage: If the server fails, the
instant messages and emails for network will not be able to function,
communication. thus affecting work processes.
Computer networks can be classified according to their geographical size, transmission medium
and network organisation type.
The number of users that it serves will also vary according to its geographical size.
397
How Do I Create a Simple Network?
Metropolitan area
11.2.1.2 Metropolitan Area Network
network (MAN)
A metropolitan area network (MAN) is a network of Network of computing
computing devices covering a larger geographical area – devices typically spanning
two or more buildings within the same town or city – than across two or more
a LAN. A MAN is typically owned and operated by a large buildings within the same
organisation such as a business or government body. town or city
A WAN differs from a LAN in several ways. Unlike a LAN, a WAN is not limited to a single
geographical location and can span long distances via long-range transmission media such as
telephone lines, fibre optic cables or satellite links.
WANs can be either private or public. Private WANs are built and maintained by large
multinational companies and government organisations. The Internet is an example of a public
WAN. A WAN also uses more expensive and high-speed technology than a LAN.
398
Module 2: Systems and Communications
Figure 11.1 compares the geographical distribution of these three types of network.
WAN
Wide Area Network
MAN
Metropolitan
Area Network
LAN
Local Area Network
399
How Do I Create a Simple Network?
Although any computer can function as a server, computers that are designed specifically for use
as servers are built to be reliable and less prone to failure. This means that they may be much
more expensive than normal computers.
400
Module 2: Systems and Communications
In a client-server network, one or more higher-capacity computers will act as servers while the
remaining computers are clients. Each server contains data and other resources to be shared with
clients. The server also fulfils requests from clients.
Advantages Disadvantages
• Centralised control of data • Higher initial cost due to the need for
and resources a server
• Easy to schedule backups of all shared • Administrative costs needed for the
files at regular intervals maintenance of server and clients
• Security may be enhanced with the
use of specialised software or
operating system features that are
designed for servers
Peer-to-peer (P2P) is a type of network where all computers are considered as equals and the
load is distributed among all computers. Each computer in the network is able to act as both a
client and a server, communicating directly with other computers. Users are able to share files
and resources located on their computers as well as access shared resources found on other
computers in the network. These networks are low in cost.
Computer Computer
Computer Computer
Computer Computer
401
How Do I Create a Simple Network?
Advantages Disadvantages
Bandwidth
The average number of bits of data
that can be transmitted from a source
to a destination over the network
in one second; usually measured
in megabits per second (Mbps) or
gigabits per second (Gbps)
Function Data and resources are shared Data and resources are shared
using one or more dedicated directly between computers;
servers; each computer has a each computer acts as both a
distinct role – client or server client and server
402
Module 2: Systems and Communications
Setup cost High as the use of specialised Low as basic computers can act
high-performance servers would as servers to share resources
be needed
Storage Centralised and carried out only Decentralised and can be carried
at the server; usually managed by out by individual users at each
a network administrator computer
1. Company X has a remote office in another country that needs to access information
from its main headquarters. What is the type of network used in this case?
A LAN
B MAN
C WAN
D None of the above
2. The main office of Y Corporation occupies five floors in their building. What is the type
of network used in this case?
A LAN
B MAN
C WAN
D None of the above
403
How Do I Create a Simple Network?
Both wired and wireless networks have their advantages and disadvantages.
Table 11.5 shows the factors that should be considered when deciding between a wired or
wireless network.
Cost Initially cheaper but becomes Initially expensive due to the cost
more expensive as network of wireless networking equipment
grows in size due to the cost of but becomes more cost-effective
cables as network grows in size
404
Module 2: Systems and Communications
1. Siti wants to send print jobs from both her laptop as well as tablet whenever she is at
home. Describe two advantages of using a wireless network to enable printing from
multiple devices in her home.
2. Give two reasons why wireless networks are preferred over wired networks at cafes
and canteens.
11.4 Identifiers
Networks typically have many computers and programs using them at any one time. In order
to identify the different networks, computers and programs, we need to give them names and
describe their respective functions.
In Chapter 8, you learnt how IP addresses (sections 8.4.2.1 and 8.4.2.2) and MAC addresses
(section 8.4.2.3) are used to identify individual computers so that data transmitted over a network
can be directed to the correct destination. The following sections will discuss other ways that
networks, computers and programs can be identified – using port numbers and SSIDs.
You can list all the port numbers that are in use on your computer by entering “netstat -na”
at the command prompt.
405
How Do I Create a Simple Network?
1. On a computer or laptop connected to the network, use the command line interface
or network tools available to find its MAC and IPv4 addresses.
2. Choose the correct words in the statements below from the given choices:
a) A program running on a network can be uniquely identified by its (MAC address /
port number / SSID) and IP address.
b) All (wired/wireless) devices connected to the same WAP must use the same SSID.
406
Module 2: Systems and Communications
Data is seldom sent in a single stream over networks. Instead, it is broken up into smaller packets
before being sent independently over the network through devices such as hubs, switches and
routers. Besides containing a piece of the original data, each packet also contains a header with
information about the source and destination addresses that are needed for transmission. These
data packets are then reassembled at the destination device.
As discussed in section 8.4.2.3, each NIC also has a unique 48-bit MAC address that can be used
to identify the particular computer or device with that NIC.
407
How Do I Create a Simple Network?
Computer A
Computer B
Data for
Data for Computer A
Hub
Computer A
Data for
Computer A Computer C
A hub is the cheapest option among all the network connection devices as it does not store any
information about the devices that are connected to it. However, because the hub transmits data
to all connected devices regardless of the intended recipient, it can cause bottlenecks that reduce
the overall efficiency of the network.
408
Module 2: Systems and Communications
In Figure 11.7, suppose the switch receives a packet from computer A. The switch first examines
the destination MAC address stored in the packet’s header and decides whether to forward or
drop the packet.
Network 1
Network 2
Hub
Computer A
Computer B
Switch Computer C
Computer D
Hub
If the destination MAC address is that of computer B, the switch will drop the packet as computers
A and B are already on the same network.
On the other hand, if the destination MAC address is that of computer C or D, the switch will
forward the packet to the other network and on towards its intended destination.
A network switch that connects exactly two networks together is also called a network bridge. Most
large networks use switches and bridges instead of hubs as these devices are more “intelligent” and
will send packets through a connection only if the switch or bridge determines that the intended
recipient is on the other end, hence avoiding unnecessary bottlenecks. This makes using switches
and bridges more efficient than using hubs.
409
How Do I Create a Simple Network?
11.5.4 Router
A router forwards packets between separate networks.
While a switch combines multiple similar networks that Key Term
use the same protocol into a single network, a router keeps
the connected networks (which may use fundamentally Router
different protocols) separate and forwards packets between Device that forwards packets
them using Internet protocols. between separate networks
In order for a router to forward packets between different networks using Internet protocols,
both the device sending the packet and the device receiving the packet must be identified using
IP addresses.
Note that switches (described in section 11.5.3) forward packets based on permanent MAC
addresses, while routers forward packets based on IP addresses that may change dynamically.
The name “Internet” actually comes from the term “internetworking”, which refers to the practice
of forwarding packets between different and separate networks. The Internet Protocol achieves
this by serving as an additional “overlay” protocol on top of the different Ethernet, Wi-Fi and other
protocols used by the separate networks.
11.5.5 Modem
While Ethernet cables are useful for LANs and some MANs, they are not suitable for connecting
devices over a long distance (such as from Siti’s ISP to her home) as the number of transmission
errors increases with cable length. Instead, ISPs typically use special protocols and long-range
transmission media such as telephone lines or fibre optic cables.
Long-range transmission media, however, are typically not designed for transferring digital data
(i.e., 0 and 1 bits) that are used by computers. For instance, telephone lines are designed for
transmitting analogue sounds and not digital data. Hence, it becomes necessary to convert digital
data into a form suitable for the transmission medium before it can be transmitted.
410
Module 2: Systems and Communications
Modulation
Conversion of digital
data into a form suitable
for transmission
WAN LAN
Internet
Telephone
lines
Modem 192.168.1.9
86.102.26.202
Router
p Figure 11.8 How a LAN is connected to the public network using a modem
411
How Do I Create a Simple Network?
1. Which of the following network hardware allows separate networks that use different
network protocols to be connected together?
A Firewall
B Hub
C Switch
D Router
2. Give two examples of network hardware used in your home and school.
A topology describes the physical layout of a network. Understanding the topology is essential
to designing a network.
During communication, a sender will transmit data along the bus and all the devices connected
to the bus can detect that data is being transmitted. However, only the intended recipient will
accept and process the data.
Figure 11.9 shows how the devices are connected in a bus topology.
Data bus
412
Module 2: Systems and Communications
Advantages Disadvantages
• Easy and cheap to install as it uses less • A break anywhere along the bus may
cabling than other network designs disable the entire network
• Scalable as new computers can be • The size of the network is limited by
easily added the capacity and length of the bus
• Can continue to operate even when • A single bus is unsuitable for networks
one of the computers breaks down with many computers; performance
slows down as the number of
• Works well for small networks
computers increases
Figure 11.10 shows how the devices are connected in a ring network.
Computer Computer
Computer Server
Computer Printer
413
How Do I Create a Simple Network?
Advantages Disadvantages
• Can operate over larger distances and • If a computer or cable in the network
handle more data than a bus topology fails, the entire network may fail as the
data cannot be passed on
• Data packets that are sent between
two computers will pass through • Adding a new computer to the
intermediate computers, hence a ring network would mean that the
central server is not required to whole communication ring needs to
manage the network be temporarily interrupted
Figure 11.11 shows how the devices are connected in a star topology.
Computer
Computer Computer
Network
hardware in
the centre
such as
a hub
Computer Printer
414
Module 2: Systems and Communications
Advantages Disadvantages
• The load on each section of cabling is • Uses more cabling than other
reduced as each computer uses a topologies and hence costs more
separate cable from the rest
• If the central network hardware fails,
• If a fault occurs at a computer or the entire network fails
cable, it is easy to isolate the fault and
do a replacement without affecting the
rest of the network
2. Which of the following network topologies is the least reliable in the event of a
breakdown of a computer in the network?
A Bus
B Client-server
C Ring
D Star
415
How Do I Create a Simple Network?
Data can become lost or corrupted during transmission. As mentioned in section 11.5, data is
seldom sent in a single stream, but is instead broken up and sent in smaller packets. This allows
us to focus on detecting transmission errors one packet at a time.
There are two basic error-checking methods to ensure that the data received at the destination
is the same as that at the source:
1. Parity check
2. Checksum
Each packet illustrated in the following examples is eight bits long. Note that, in reality, the packets
are usually much longer.
1 1 0 1 0 0 0 1
p Figure 11.12 Each packet in an even parity system should have an even number of 1 bits
416
Module 2: Systems and Communications
Table 11.9 describes the two possible scenarios that may occur when the packet arrives at the
destination. If the data transmission is successful, the total number of 1 bits detected would be
even, which is the same as that at the source. This means that the data is error-free. However, if
there is a simple error in the data transmission, the total number of 1 bits detected would be odd,
which differs from that at the source. This means that the data is corrupted.
Table 11.9 Successful and failed data transmission in an even parity system
Data transmission
Original Parity system
data bits – even
Successful Unsuccessful
If the same packet is used in an odd parity system instead, then the parity bit of 0 is added, as
there is already an odd number of 1 bits in the original data string.
0 1 0 1 0 0 0 1
p Figure 11.13 Each packet in an odd parity system should have an odd number of 1 bits
Table 11.10 describes the two possible scenarios that may occur when the packet arrives at the
destination. If the data transmission is successful, the total number of 1 bits detected would be
odd, which is the same as that at the source. This means that the data is error-free. However, if
there is a simple error in the data transmission, the total number of 1 bits detected would be even,
which differs from that at the source. This means that the data is corrupted.
Table 11.10 Successful and failed data transmission in an odd parity system
Data transmission
Original Parity system
data bits – odd
Successful Unsuccessful
417
How Do I Create a Simple Network?
Parity checking has certain limitations. For instance, it is able to detect that an error has occurred
but cannot determine where the error occurred.
It is also unable to detect all the possible types of errors. It can detect simple errors where only a
single bit is changed, but not serious errors where multiple bits are changed. In particular, if two
different errors occur in a single packet, they can effectively cancel each other out and result in
the correct parity even though the data is actually corrupted.
In general, parity checking can only detect errors when an odd number of bits have been corrupted
but not when an even number of bits have been corrupted.
11.7.2 Checksum
A checksum is a calculated value that is used to determine the integrity of transmitted data.
A checksum serves as a unique identifier for the data and is sent together with the data.
Before transmission, the checksum for the data is first calculated. Assuming that the checksum is
one byte in length, it can be calculated in two ways.
If the sum of all the bytes in the data is less than or equal to 255, the checksum is this value.
If the sum is greater than 255, the checksum is the modulus of 256.
The data and the checksum are then sent together. At the Checksum
destination, the checksum is recalculated and compared Calculated value that is
to the sent checksum value. If the checksum value of the used to determine the
received data matches the sent checksum value, the data integrity of transmitted data
was transmitted correctly. If they differ, an error has occurred.
418
Module 2: Systems and Communications
1. Determine the parity bit for the data string in Figure 11.14 if an odd parity system
is used.
1 1 0 0 0 0 1
2. Determine the parity bit of the following data strings if an even parity system is used:
a) 1001000
b) 1100110
c) 1001111
3. Determine whether each of the following data packets has been corrupted. Use the
parity check method, given that the last bit is the parity bit and an odd parity system
is used.
a) 11100111
b) 11011010
419
How Do I Create a Simple Network?
Chapter Summary
GEOGRAPHICAL TRANSMISSION
ORGANISATION SIZE MEDIUM
Service Set
Checksum
Identifiers
COMPUTER
ERROR-CHECKING
NETWORKS IDENTIFIERS
METHODS
HARDWARE NETWORK
TOPOLOGY
Network
Interface Bus
Controller
Network Hub Ring
Network Star
Switch
Router
Modem
420
Module 2: Systems and Communications
Review Questions
2. Match statements A–L below to the correct network architecture in Table 11.11.
A. Access to shared resources on the network such as printers, data files and web
access is managed by servers
B. Resources and files located on one computer are accessible by all other computers
within the network
C. All computers are independent of each other and are of equal status when
communicating with each other
D. The network consists of computers operated by users and centralised computers
which control the resources of all computers
E. Regular backups can be easily implemented with the use of a centralised
backup storage
F. Backups are difficult to organise since there is no centralised backup storage
G. Data is stored independently in each computer
H. Data is stored in a centralised storage device monitored by the server
I. More commonly found in businesses and organisations
J. More commonly found in homes
K. There is often no mechanism to manage access to the network, hence it is
less secure
L. Access to the network is managed using a database of usernames, passwords and
user profile and permission settings, hence the network tends to be more secure
a) Organisation of components
b) Resource management
c) Storage
d) Mode of backup
e) Security
f) Type of environment
421
How Do I Create a Simple Network?
3. A doctor wishes to set up a LAN in his new clinic. He has three staff located at different
counters within the clinic. During operating hours, he and his staff need to retrieve records
from a patient history database using their individual desktop computers. They will also
need to access a shared network printer to print receipts and medical certificates.
For security reasons, the patient history database and printer must be isolated from the
Internet and connected using a wired network. However, he would still like to provide his
patients with free wireless Internet access while they wait for their consultation.
a) The doctor purchases a network switch to help set up the LAN. Name the network
topology that is formed when multiple devices are connected to the network switch.
b) The patient history database needs to be accessed and modified by the doctor and his
staff multiple times throughout the day. Recommend, with explanation, whether the
patient history database should be stored on a single server or distributed among the
doctors and his staff using a peer-to-peer network.
c) Draw a labelled diagram of the clinic’s network(s). The diagram should include at least
four laptops, one printer, one fibre optic modem (for Internet access), one network
switch and one combined wireless router/access point. You may include additional
devices as needed.
422
Answers
ANSWERS
Chapter 1 How Do Computers Work?
1. True. However, “kilobyte” is still often confused with “kibibyte”, which is 1,024 bytes.
1. The main differences between RAM and ROM are summarised below:
RAM ROM
Read and write: stored data can be Read only: stored data cannot be easily
easily changed changed
Volatile: loses data once power supply Non-volatile: retains data regardless of
to the computer is interrupted whether the power supply is switched
on or off
Purpose: to store data and instructions Purpose: to store data and instructions
temporarily so that they can be quickly that would be needed for a computer to
accessed by the processor when needed start up or before data can be loaded into
RAM
1. True. Memory addresses are just numbers so they can also be treated as data.
1. a) A hard disk. It is the most common device with storage capacity available in terabytes.
b) A memory card. It is the only device small and flat enough to fit in a wallet. A memory
card would also be less vulnerable to mechanical shocks that could occur in a wallet.
A–1
Answers
Review Questions
1. a) Control unit: follows instructions and decides when data should be stored, received or
transmitted by different parts of the computer
b) Arithmetic logic unit (ALU): processes data by performing basic mathematical and
logical operations
c) Main memory: stores data and instructions temporarily so they can be quickly accessed
by the processor when needed
d) Secondary storage: way of storing large amounts of data that will not be lost when
power supply is interrupted
2. a) Volatile
b) Volatile
c) Non-volatile
d) Non-volatile
A–2
Answers
9. a) False. The data bus connects the processor to memory only and not to secondary
storage such as hard disks.
b) Two improvements (accept any possible answer):
1. Faster reading and writing of files
2. Less noise when laptop is on
3. Lighter laptop
4. Longer battery life due to reduced power use
A–3
Answers
1. A. By itself, the map does not have any step-by-step instructions and therefore is not
an algorithm.
2. A modified addition algorithm that works for three positive whole numbers is provided
below with the required changes indicated in italics:
Step 1: Write the first number, then write the second number below it and the third
number below the second number such that the digits in the ones place are
aligned vertically. Draw a horizontal line below the third number.
Step 2: Let the current column be the right-most column of digits in the ones place.
Step 3: Add all the digits (including any “carry-over” digits) in the current column.
If a digit is missing, treat it as 0. There is no “carry-over” digit for the
right-most column.
Step 4: If the result is greater than 9, write the first digit of the result at the top of the
column immediately to the left of the current column as a “carry-over” digit.
Step 5: Write the last digit of the result at the bottom of the current column, below
the line.
Step 6: If there are still digits (including “carry-over” digits) to the left of the current
column, redefine that column on the left as the new current column and go
back to Step 3. Otherwise, the final answer is formed by the row of digits
below the line.
The only changes needed are in Step 1 and Step 4. The most significant difference is that
when adding three numbers, “carry-over” digits can be either 1 or 2.
3. A possible algorithm to find the top score of a class test is provided below:
Step 1: Let the current score be the first score on the list of scores. Write down the
current score.
Step 2: If the current score is the last score in the list, proceed to Step 5, otherwise
proceed to Step 3.
Step 3: Redefine the current score to be next score in the list.
Step 4: If the current score is greater than the score that was previously written down,
erase the previous score and write down the current score. Proceed to Step 2.
Step 5: The final answer is the written score.
A–4
Answers
1. The modified input and output specifications for addition problem are as follows:
Input Output
• y: a whole number
The addition algorithm provided previously is not a solution to this modified problem.
See the counter-example below of using the algorithm to add –2017 to 1965:
The counter-example shows that the addition algorithm produces an incorrect answer of
3982 while the correct answer should be –52.
2. The input and output requirements for the problem are as follows:
Input Output
Input Output
A–5
Answers
3. The title of the class test should be excluded as it is irrelevant and does not affect what
we require for the output.
Input Output
2. A.
Input Output
A–6
Answers
d) Let the first number be x and the second number be y. A possible general solution is
as follows:
Step 1: Compare x and y.
Step 2: If x is bigger, subtract y from x, otherwise subtract x from y. The final answer
is the absolute difference.
Review Questions
A–7
Answers
Input Output
Leave home
Check time
Yes No
Reach school
A–8
Answers
result x OUTPUT
1
3
3
2
6
1
6
0
6
result x OUTPUT
1
5
5
4
20
3
60
2
120
1
120
0
120
A–9
Answers
c) From the pattern of output in a) and b), we can deduce that the algorithm uses the test
data to calculate the following:
Result = n × (n–1) × (n–2) … 2 × 1
where n is the input.
This is the formula for the factorial operation you may have learnt in mathematics to
count the number of ways that n distinct objects can be arranged. Thus, the purpose of
this algorithm is to calculate the factorial of the input, or n! for short.
INPUT number
IF number == 1 THEN
OUTPUT "one"
ELSEIF number == 2 THEN
OUTPUT "two"
ELSEIF number == 3 THEN
OUTPUT "three"
ELSEIF number == 4 THEN
OUTPUT "four"
ELSEIF number == 5 THEN
OUTPUT "five"
ELSEIF number == 6 THEN
OUTPUT "six"
ELSEIF number == 7 THEN
OUTPUT "seven"
ELSE
OUTPUT "Invalid input"
ENDIF
Input Output
A–10
Answers
2. Using the FOR...NEXT iteration construct, the required algorithm can be represented in
pseudo-code as follows:
sum = 0
FOR i = 1 to 10
INPUT number
sum = sum + number
NEXT
average = sum / 10
OUTPUT average
Input Output
A–11
Answers
20
20
17
37
19
56
65
121
20
141
18
159
19
178
66
244
20
264
19
283
28.3
28.3
A–12
Answers
Review Questions
START
INPUT x
counter = 0
Is x == 1?
No Yes
Yes No
x=x/2 x = 3x + 1 STOP
counter = counter + 1
A–13
Answers
x counter OUTPUT
16
A–14
Answers
x counter OUTPUT
6
0
3
1
10
2
5
3
16
4
8
5
4
6
2
7
1
8
8
INPUT x
counter = 0
WHILE x != 1
IF x is even THEN
x = x / 2
ELSE
x = 3x + 1
ENDIF
counter = counter + 1
ENDWHILE
OUTPUT counter
A–15
Answers
START
INPUT temperature
Yes
Is temperature > 20 °C? Turn air conditioner ON
No
Yes
Is temperature < 18 °C? Turn air conditioner OFF
No
INPUT Choice
INPUT Amount
A–16
Answers
1 Count = 0
2 ClubSociety_Count = 0
3 Sport_Count = 0
4 UniformedGroup_Count = 0
5
6 WHILE Count < 5
7 INPUT Type
8 IF Type == "Club/Society" THEN
9 ClubSociety_Count = ClubSociety_Count + 1
10 Count = Count + 1
11 ELSEIF Type == "Sport" THEN
12 Sport_Count = Sport_Count + 1
13 Count = Count + 1
14 ELSEIF Type == "Uniformed Group" THEN
15 UniformedGroup_Count = UniformedGroup_Count + 1
16 Count = Count + 1
17 ELSE
18 OUTPUT "Invalid Option"
19 ENDIF
20 ENDWHILE
21
22 OUTPUT "Clubs and Societies: " + ClubSociety_Count
23 OUTPUT "Sports: " + Sport_Count
24 OUTPUT "Uniformed Groups: " + UniformedGroup_Count
A–17
Answers
15
22
11
33
18
51
56
17
73
74
10.57
The sum is 74
A–18
Answers
i = 0
INPUT numbers[i]
WHILE numbers[i] != 0
i = i + 1
INPUT numbers[i]
ENDWHILE
WHILE i >= 0
OUTPUT numbers[i]
i = i - 1
ENDWHILE
i numbers OUTPUT
0
20
1
20, 17
2
20, 17, -19
3
20, 17, -19, 65
4
20, 17, -19, 65, 0
0
3
65
2
-19
1
17
0
20
-1
c) The flowchart's purpose is to output the given sequence of integers (including the last
integer 0) in reverse order.
A–19
Answers
1.
Name of variable that Final value
# Statement
is assigned a value being assigned
3 to_hit = 17 + 3 to_hit 20
2.
# Source code Result Explanation
A–20
Answers
3. original_value = 99
copy_1 = original_value
copy_2 = original_value
A–21
Answers
4. The fixed values of "Hello, ", "Ms. " and "Mr. " are repeated multiple times in the source
code. To make the source code more readable and easier to maintain, we should use
constants with descriptive names, that is, GREETING, FEMALE_TITLE and
MALE_TITLE respectively instead.
# Program greetings_with_constants.py
1 # Constants
2 GREETING = "Hello, "
3 FEMALE_TITLE = "Ms. "
4 MALE_TITLE = "Mr. "
5
6 # Prints out a series of greetings, with title based on gender.
7 print(GREETING + FEMALE_TITLE + "Aishah")
8 print(GREETING + MALE_TITLE + "Paul")
9 print(GREETING + MALE_TITLE + "Xiaoming")
10 print(GREETING + FEMALE_TITLE + "Jessica")
1.
Literal Data type
a) -2017 int
b) '"' str
c) 'Computing' str
d) 3.14159 float
e) "\"" str
f) "\"[]\"" str
g) True bool
h) 6.02e23 float
i) '\'' str
j) "2017" str
k) "'" str
l) [""] list
m) 'False' str
A–22
Answers
2.
What will be
# Source code Explanation
displayed
1 print(1e100) 1e+100
2 print(103E1) 1030.0
4 print(3.141) 3.141
9 print('Hello,\n\tWorld') Hello,
World
10 print('Hello,\t\nWorld') Hello,
World
11 print("\\\"") \" Python reads the
str literal as two
escape codes: \\
(a backslash) and
\" (a double
quote).
13 print(True) True
A–23
Answers
What will be
# Source code Explanation
displayed
15 print(False) False
17 print(None) None
19 print([]) []
21 print([[]]) [[]]
A–24
Answers
3.
What will be
# Source code Explanation
displayed
1 print(int("-3")) -3
2 print(float('-3')) -3.0
4 print(str(-3)) -3
5 print(str(-3.0)) -3.0
6 print(str(-3e3)) -3000.0
A–25
Answers
1.
What will be
# Source code Explanation
displayed
1 print(len(range(3))) 3
5 print(len([phrase])) 1 [phrase] is
just a list made
up of 1 item (i.e.,
phrase).
6 print(list(range(7))) [0, 1, 2,
3, 4, 5,
6]
A–26
Answers
What will
# Source code Explanation
be displayed
9 print(x, x) 5 5
# Program count_phrase.py
1 # Input
2 phrase = input("Enter phrase: ")
3
4 # Process
5 count = len(phrase)
6
7 # Output
8 print(count)
A–27
Answers
3.
What will be
# Source code Explanation
displayed
1 x = 2 (nothing
displayed)
2 x **= 3 (nothing
displayed)
7 quotient = 55 // 7 (nothing
displayed)
8 remainder = 55 % 7 (nothing
displayed)
9 print(quotient * 7 + True
remainder == 55)
11 print((remainder + 91 As (remainder
quotient) * 7) + quotient) is
enclosed in brackets,
the addition is
performed first before
the multiplication.
A–28
Answers
What will be
# Source code Explanation
displayed
12 55 = x Error:
SyntaxError
A–29
Answers
7.
# Source code What will be displayed Explanation
A–30
Answers
What will be
# Source code Explanation
displayed
6 print(data[-3:-2]) [9] The slice operator
is used with a start
index of 1 (3rd last
item) and a stop
index of 2 (2nd last
item). This results in
the list [9], which
is a list with just the
item in index 1.
A–31
Answers
8.
What will be
# Source code Explanation
displayed
1 x = 5 (nothing
displayed)
9.
Variable/Constant Local or Global
A–32
Answers
# Program list_input.py
Using the modular approach, we can break down the task into two parts. The first part
repeatedly asks the user for an item and adds it to our output list. We can think of this
as the “default behaviour” of the program. The second part detects when a blank item
is received and ends the program. We can think of this as an “exceptional case” to the
default behaviour of the program.
The first part of the task can be accomplished using a loop that keeps repeating without
end. We do this using a while condition that is always True on line 3. As the loop keeps
repeating, we keep getting the next item from the user (line 4) and adding it to our output
list, which we call result (line 7). Of course, before we start the loop we must initialise
result to an empty list first (line 2).
The second part of the task requires us to detect an exception to the default case. We do
this using an if statement immediately after getting the user’s input (line 5). If we detect
that the input is blank, we break out of the loop (line 6). The use of break helps to explain
why our program does not run forever as long as a blank input is eventually supplied by
the user.
A–33
Answers
# Program password_check.py
Using generalisation, we see that, like the previous task, there is again a default behaviour
that is required (i.e., repeatedly asking for two passwords and printing “Invalid”) as well as
an exceptional case (i.e., stopping when the two passwords match and printing “Valid”).
Thus we can adapt our program from the solution to the previous task. Instead of
prompting for an item, we now prompt for two passwords (lines 3 and 4). Instead of adding
an item to a list, we print “Invalid” (line 7). Finally, instead of detecting a blank input, we
detect when the two passwords match (line 5) before breaking out of the loop (line 6) and
printing “Valid” as the final output (line 8).
A–34
Answers
Review Questions
1. a) Alex’s method will simplify his program as the int values increase in the same order as
the corresponding dates. He can then easily compare whether one date is earlier or
later than another date by using the less than (<) and greater than (>) operators.
# Program overdue.py
A–35
Answers
2. The diagram below shows the different ways we can extract the hours, minutes and
seconds components from the time string using the slice operator:
time[−2:]
time[:2] time[6:]
time[0:2] time[3:5] time[6:8]
−8 −7 −6 −5 −4 −3 −2 −1
0 1 2 3 4 5 6 7 8
time H H : M M : S S
# Program time_string.py
1 # This program extracts the hour, minute and second values (as ints)
2 # from a time string that is provided in the format "HH:MM:SS".
3 # It is assumed that input data will always be valid.
4
5 # Input
6 time = input("Enter time string: ")
7
8 # Process
9 hours = int(time[:2])
10 minutes = int(time[3:5])
11 seconds = int(time[-2:])
12
13 # Output
14 print(hours)
15 print(minutes)
16 print(seconds)
A–36
Answers
# Program time_interval.py
A–37
Answers
1.
x −20.17 −19.65 −0.1 0.0 0.1 19.65 20.17
import random
We want to use counts[2], counts[3]
counts = [0] * 13 and so on up to counts[12] to track the
for i in range(1000): results. However, list indices always start
from 0, so counts[0] and counts[1]
die1 = random.randint(1, 6)
must also exist such that counts has 13
die2 = random.randint(1, 6)
entries in total from index 0 to index 12.
counts[die1 + die2] += 1
for i in range(2, 13):
print('{}: {}'.format(i, counts[i]))
valid = True
for char in title:
if not (char.isupper() or char.isspace()):
valid = False
break
print(valid)
A–38
Answers
Review Questions
1. a) Siti’s new solution stops searching through the list once the availability of the book is
determined, but the solution in availability_refined.py will always search
through the entire list, which makes it slower.
# Program availability_revision.py
1 # Input
2 titles = [
3 # The following data is copied from the given text file.
4 "How to Solve a Mystery", "Vacant Memories",
5 "The Cybersnake Chronicles", "Music History",
6 "Like Tears in Rain", "Out of the Abyss"
7 ]
8 availability = [
9 # The following data is copied from the given text file.
10 "Not Available", "Available", "Available", "Not Available",
11 "Not Available", "Available"
12 ]
13 search = input("Enter title of book to check: ")
14
15 # Process
16 result = "Book Not Found"
17 index = 0
18 while index < len(titles):
19 if titles[index] == search:
20 result = availability[index]
21 break
22 index = index + 1
23
24 # Output
25 print(result)
A–39
Answers
# Program Q2.py
1 # Input
2 values = []
3 while True:
4 item = input("Enter item, blank to end: ")
5 if item == "":
6 break
7 values += [int(item)]
8
9 # Process
10 if len(values) >= 2:
11 # For the basic sub-problem, only consider the first
12 # two items in values.
13 if values[0] < values[1]:
14 minimum_value = values[0]
15 second_minimum_value = values[1]
16 else:
17 minimum_value = values[1]
18 second_minimum_value = values[0]
19 # Start considering beyond the first two items.
20 for value in values[2:]:
21 if value < minimum_value:
22 # If there will be a new minimum value, the second
23 # minimum value will be the old minimum value.
24 second_minimum_value = minimum_value
25 minimum_value = value
26 else:
27 minimum_value = None
28 second_minimum_value = None
29
30 # Output
31 if minimum_value == None:
32 print("None")
33 else:
34 print(minimum_value, second_minimum_value)
A–40
Answers
# Program Q3.py
1 # Input
2 length = int(input("Enter length: "))
3 values = []
4 for i in range(length):
5 value = float(input("Enter item: "))
6 values += [value]
7
8 # Process
9 average = None
10 sum_values = 0
11 num_positive = 0
12 for value in values:
13 if value > 0:
14 sum_values += value
15 num_positive += 1
16 if num_positive > 0:
17 average = round(sum_values / num_positive, 2)
18
19 # Output
20 print(average)
A–41
Answers
1. a) Format check.
A combination of the len() function, the in operator and the str.isdigit()
method is used to check that the input str matches the pattern of an O-Level grade
(i.e., a capital letter from A to F, followed by a digit).
b) Presence check.
The first and last strs are required inputs and must be provided (i.e., they must
not be equal to empty strs).
c) Range check.
The < and > operators are used to ensure that the input float p is strictly between
0.0 and 1.0.
d) Length check.
The len() function is used to check that the length of input list integers is equal
to the input int n.
# Program postal_code_validation.py
Line 3, which asks the user to enter a postal code, is included inside an infinite while
loop which will repeat until the condition in line 4 is True, i.e., the user enters a str that
is six characters long and consists only of numbers. When that happens, the loop will be
exited and we will continue with the rest of the program.
If the str entered by the user does not fulfil the conditions in line 4, the program will go
on to print the error messages in lines 6 and 7. The loop then restarts and asks the user
once again to enter a postal code.
A–42
Answers
1. a) Test case that covers normal conditions (accept any possible answer):
b) Test case for the boundary condition when n is at its lower limit:
This is a boundary condition as the input of 1 is the smallest possible value for n that is
valid based on the problem definition.
A–43
Answers
Test case for the boundary condition when n is at its upper limit:
This is a boundary condition as the input of 20 is the largest possible value for n that is
valid based on the problem definition.
n should be an integer
between 1 and 20 inclusive
n should be an integer
between 1 and 20 inclusive
A–44
Answers
# Program diamond_with_syntax_errors_fixed.py
A–45
Answers
3. Although the run-time error occurs on line 25, it is actually caused by a logic error on line
18. This program uses stars to store each line of the diamond in a list but leaves out
the widest line of the diamond due to a logic error. The most straightforward way to fix this
error is to change the condition on line 18 from “index < n” to “index <= n”.
# Program diamond_crash_fixed.py
A–46
Answers
1. The results from running acronym_alternative.py on all six test cases are as follows:
A–47
Answers
This shows that acronym_alternative.py passes all test cases except Test Cases 5
and 6 as the program does not perform any input validation.
Review Questions
b) i) Normal condition
ii) A logic error has occurred. While the program has not crashed, its actual output is
different from the expected output.
iii) The error is located on line 10 and is caused by using the index operator to count
from the end of a string incorrectly. (The last character of a string is at index −1, not
index −0.)
A–48
Answers
# Program word_reversal.py
1 # Input
2 input_text = input("Enter input text: ")
3
4 # Process
5 word_list = input_text.split()
6
7 for i in range(len(word_list)):
8 current_word = ""
9 for letter_index in range(len(word_list[i])):
10 current_word += word_list[index][-letter_index - 1]
11 word_list[i] = current_word
12
13 result = ""
14 for word in word_list:
15 result += word + " "
16 result = result[:-1]
17
18 # Output
19 print(result)
2. a) i) True
ii)
False
iii)
True
iv)
False
b) i) The return value should be True since an empty string, having no characters,
trivially satisfies the requirement of not containing any non-letter characters.
ii) The return value should be False since an empty string, having no characters, is
not a sensible input for the task of checking whether every character is a letter.
iii) Since an empty string is considered valid input, we choose to have
is_letters_only('') return True so an empty string will pass data
validation. A possible answer is as follows:
A–49
Answers
# Program opposite_case_finished.py
A–50
Answers
3. CAPTCHA stands for Completely Automated Public Turing Test to Tell Computers and
Humans Apart. It tries to verify that an actual human and not a machine is using the
website by requiring the user to type out the letters and/or numbers in a distorted image.
CAPTCHAs are usually used when access control is needed to deny computer programs
and other automated systems access to a website or its services.
A–51
Answers
1. C
2. B
3. a) Yes
b) No
c) No
d) Yes
e) Yes
f) No
5. Siti should verify the authenticity of the source by checking that the sender is legitimate,
and if necessary, calling the organiser of the lucky draw and asking for proof of the lucky
draw results. Even if the result is legitimate, she should request to receive the cash prize in
a manner that will not reveal her personal details to a stranger. If there is any doubt, she
should decline to reveal any personal details and ignore the message.
A–52
Answers
Review Questions
1. a) Phishing uses email hyperlinks that lead to a fake website with a different address from
the real website, while pharming uses website redirection to show a fake website that
uses the same address as the real website.
b) Phishing
2.
Unauthorised access likely
Threat Preventive measure
to occur? Why?
a) Cookies No. –
Cookies are files that store user
information each time a user
visits a website. They are not
malicious in nature and cannot
store information that the user did
not already provide to the website
through a web browser.
b) Spam No. –
Spam is the sending of mass
emails for advertising purposes.
Unless any hyperlinks from the
spam are clicked, no unauthorised
data is likely to be sent back to the
spammer.
A–53
Answers
b) Siti’s actions are considered to be copyright infringement as she did not have explicit
permission from the copyright owner to copy the television series onto her hard drive.
c) The icon represents that the photograph is under a Creative Commons licence. It means
that users are granted permission to copy, modify and distribute the photograph under
certain conditions.
b) Plagiarism.
A–54
Answers
1. a) 11 01102
54 27 0
27 13 1
13 6 1
Direction to
read the digits
6 3 0
3 1 1
1 0 1
A–55
Answers
b) 101 10012
89 44 1
44 22 0
22 11 0
11 5 1 Direction to
read the digits
5 2 1
2 1 0
1 0 1
A–56
Answers
c) 110 01002
100 50 0
50 25 0
25 12 1
12 6 0 Direction to
read the digits
6 3 0
3 1 1
1 0 1
2. 7 = 1112
8 = 10002
9 = 10012
10 = 10102
3. a) 19
b) 96
c) 134
A–57
Answers
1.
Denary Binary Hexadecimal
32 10 00002 2016
33 10 00012 2116
34 10 00102 2216
35 10 00112 2316
36 10 01002 2416
37 10 01012 2516
38 10 01102 2616
39 10 01112 2716
40 10 10002 2816
41 10 10012 2916
42 10 10102 2A16
43 10 10112 2B16
44 10 11002 2C16
45 10 11012 2D16
46 10 11102 2E16
47 10 11112 2F16
2. a) 100 10012
b) 1010 1011 01102
c) 1101 1010 0100 01112
3. a) 157
b) 2998
c) 44432
4. a) 3E816
b) ECD16
c) 10 7C16
A–58
Answers
# Program: valid_ipv4.py
1 # Input
2 input_str = input("Enter string: ")
3
4 # Process
5 is_valid = True # Assume input is valid until proven wrong
6 processed = 0
7 digits = ""
8 index = 0
9
10 # To avoid repeated code, <= instead of < is used so collected
11 # digits are checked one final time at end of input string
12 while index <= len(input_str):
13 # Get current character only if not at end of input string
14 # Otherwise, current character is None
15 current = None
16 if index < len(input_str):
17 current = input_str[index]
18
19 if current == None or current == '.':
20 # If at end of input string or current character is a dot,
21 # check that digits is a valid int between 0 and 255
22 if not digits.isdigit():
23 is_valid = False
24 break
25 digits_int = int(digits)
26 if not (digits_int >= 0 and digits_int <= 255):
27 is_valid = False
28 break
29 # digits was valid, so increase processed and reset digits
30 processed += 1
31 digits = ""
32 elif current.isdigit():
33 # If current character is a digit, add it to digits
34 digits += input_str[index]
35 else:
36 # If current character is not a digit or a dot, input is invalid
37 is_valid = False
38 break
39
40 # Update index to process next character
41 index += 1
42
A–59
Answers
Review Questions
1. When representing large numbers, the hexadecimal form is much shorter than the binary
form, making it more compact and easier to key in.
2. a) 10102
b) 1111 11112
c) 1 0000 10112
3. a) D16
b) 916
c) 516
d) ED16
e) 5516
f) 16 CD16
4. a) 0110 11002
b) 6C16
c) Common uses of the hexadecimal number system (accept any two valid answers):
• To represent red, green and blue intensities in an RGB colour code
• To present IPv6 addresses in a compact format
• To present MAC addresses in a compact format
• To present ASCII codes in a compact format
A–60
Answers
5. a) There are faults in the display and motor subsystems for lift number 7.
b) There are faults in the motor and ventilator subsystems for lift number 11.
c) 11001110
A–61
Answers
1. a) True
b) True
c) False
2. A
3. B
b) 1
1. a) Truth table:
Intermediate Intermediate
Input Input Input Output
Input Input
A B C Q
(NOT A) (NOT A OR B)
0 0 0 1 1 0
0 0 1 1 1 1
0 1 0 1 1 0
0 1 1 1 1 1
1 0 0 0 0 0
1 0 1 0 0 0
1 1 0 0 1 0
1 1 1 0 1 1
A–62
Answers
b) Logic circuit:
B Q
Review Questions
1. a) Q = A OR (B AND C)
Intermediate
Input A Input B Input C Output Q
Input (B AND C)
0 0 0 0 0
0 0 1 0 0
0 1 0 0 0
0 1 1 1 1
1 0 0 0 1
1 0 1 0 1
1 1 0 0 1
1 1 1 1 1
Intermediate Intermediate
Input A Input B Output X
Input (NOT A) Input (NOT B)
0 0 1 1 1
0 1 1 0 0
1 0 0 1 0
1 1 0 0 0
A–63
Answers
c) Y = A AND (NOT B)
0 0 1 0
0 1 0 0
1 0 1 1
1 1 0 0
d) Z = (A OR B) AND (C OR D)
Intermediate Intermediate
Input Input Input Input Output
Input Input
A B C D Z
(A OR B) (C OR D)
0 0 0 0 0 0 0
0 0 0 1 0 1 0
0 0 1 0 0 1 0
0 0 1 1 0 1 0
0 1 0 0 1 0 0
0 1 0 1 1 1 1
0 1 1 0 1 1 1
0 1 1 1 1 1 1
1 0 0 0 1 0 0
1 0 0 1 1 1 1
1 0 1 0 1 1 1
1 0 1 1 1 1 1
1 1 0 0 1 0 0
1 1 0 1 1 1 1
1 1 1 0 1 1 1
1 1 1 1 1 1 1
A–64
Answers
2. a) Boolean statement: Q = A OR B
b) Truth table:
0 0 0
0 1 1
1 0 1
1 1 1
3
3. Step 1: Total number of rows in truth table = 2 = 8 as there are three inputs.
Step 2: Draw the truth table and fill in all the input combinations.
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
A–65
Answers
A D
B
Q
E
G
F
C
D = A AND B
E = B OR C
F = NOT C
G = E AND F
Intermediate
Input Input Input Output
A B C Q
Input D Input E Input F Input G
0 0 0 0 0 1 0
0 0 1 0 1 0 0
0 1 0 0 1 1 1
0 1 1 0 1 0 0
1 0 0 0 0 1 0
1 0 1 0 1 0 0
1 1 0 1 1 1 1
1 1 1 1 1 0 0
A–66
Answers
Intermediate
Input Input Input Output
A B C Q
Input D Input E Input F Input G
0 0 0 0 0 1 0 0
0 0 1 0 1 0 0 0
0 1 0 0 1 1 1 1
0 1 1 0 1 0 0 0
1 0 0 0 0 1 0 0
1 0 1 0 1 0 0 0
1 1 0 1 1 1 1 1
1 1 1 1 1 0 0 1
A–67
Answers
1. a) Number
b) Text
c) By entering an apostrophe in front of the equals sign, i.e., '=
d) C4:C15
e) E4 only
f) C4:C15 and E4:E15
2. =A2/B2
1. a) 3
b) Text
c) Currency
1. a) =B2*D2
=SUM(E2:E9)
b)
c) i) 6
B
ii)
3. a) =VLOOKUP(B2, $E$2:$F$4, 2)
b) =AVERAGE(C2:C6)
c) =MEDIAN(C2:C6)
A–68
Answers
4. a) =ROUND(B2, 1)
b) =COUNTIF(C2:C15, "<60")
c) =MAX(C2:C15)-MIN(C2:C15)
Review Questions
1. a) =$G$1*B2
b) 0.67%
c) The required conditional formatting settings for D2:D13 are as follows:
A–69
Answers
1. C
2. A
4. a) Peer-to-peer
b) Client-server
c) Client-server
1. She can install a wireless printer to connect to both her laptop and tablet. Possible
advantages (accept any possible answer):
• Siti can move her printer to anywhere within the range of the wireless coverage at
home without being restricted to a fixed location.
• It is easy to connect more mobile devices to the wireless printer.
• The physical organisation is neater without the use of cables running across rooms.
2. Why wireless networks are preferred over wired networks at cafes and canteens (accept
any possible answer):
• A wireless network allows for user mobility within the range of the wireless coverage
without being bound to a fixed position unlike in wired networks.
• The physical organisation is neater without the use of cables.
2. a) A program running on a network can be uniquely identified by its port number and
IP address.
b) All wireless devices connected to the same WAP must use the same SSID.
A–70
Answers
1. D
1. B
2. C
3. a) Star topology. If a fault occurs at a computer or cable, it is easy to isolate the fault and
do a replacement without affecting the rest of the network. This makes the network
more fault-tolerant and resilient as compared with the bus and ring topologies.
A–71
Answers
1. In an odd parity system, the total number of 1 bits is odd, hence the parity bit has to be 0.
0 1 1 0 0 0 0 1
2. a) Parity bit: 0
The total number of 1 bits is 2, which is even.
b) Parity bit: 0
The total number of 1 bits is 4, which is even.
c) Parity bit: 1
The total number of 1 bits should be 6, which will give an even parity system.
3. a) The data packet is corrupted. The total number of 1 bits in the first seven bits is 5,
which is an odd number. The parity bit should be 0 and not 1 in an odd parity system.
b) The data packet is not corrupted. The total number of 1 bits in the first seven bits is 5,
which is an odd number. The parity bit is 0, which tallies with the odd parity system.
Review Questions
1. Three advantages of setting up a home or office network (accept any possible answer):
• Allows users to share data and files
• Allows users to share network resources such as printers or storage
• Allows fast data transmission and communication between users within the network
• Allows data to be backed up and accessed centrally
• Can be configured for added security
A–72
Answers
2.
Design factors Peer-to-peer Client-server
A–73
Answers
3. a) Star topology.
b) The patient history database should be stored on a single server using a client-server
network. As the database contains sensitive data, access to its contents needs to
be controlled and monitored. This is easier to achieve on a client-server network
compared to a peer-to-peer network. Centralising storage on a single server also
ensures that there is only one official copy of the database and that it is always the
latest version. On the other hand, distributing the database using a peer-to-peer
network may result in multiple conflicting copies as different users make changes.
c) For the wired network, the following devices should be connected to the network
switch: one laptop for the doctor, three laptops for staff, one printer and one
dedicated server (optional). For the wireless network, the fibre optic modem should
be connected to the wireless router/access point to provide Internet access. Both
networks should be isolated from each other. (Note: Detailed drawings of laptops,
printers or other devices are not required. Simple shapes such as boxes or ovals can
be used to represent any device as long as they are clearly labelled.)
Internet
Staff's laptop
Doctor's laptop
Staff's laptop
Staff's laptop
A–74