0% found this document useful (0 votes)
25 views138 pages

Pseudocode Fundamentals and Exercises

The document is a textbook on pseudocoding, covering various programming concepts such as variables, boolean expressions, conditional statements, loops, functions, and lists. It includes detailed explanations, examples, exercises, and trace tables to help students understand how to write and analyze pseudocode effectively. The second edition adds new chapters on functions and lists, providing a comprehensive resource for learning algorithm design at the senior secondary level.

Uploaded by

daaim.shah.roots
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views138 pages

Pseudocode Fundamentals and Exercises

The document is a textbook on pseudocoding, covering various programming concepts such as variables, boolean expressions, conditional statements, loops, functions, and lists. It includes detailed explanations, examples, exercises, and trace tables to help students understand how to write and analyze pseudocode effectively. The second edition adds new chapters on functions and lists, providing a comprehensive resource for learning algorithm design at the senior secondary level.

Uploaded by

daaim.shah.roots
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Successful Pseudocoding

© Code for Schools, [Link]

Successful Pseudocoding
Edition 2.01 alpha

[Link]

1
Successful Pseudocoding
© Code for Schools, [Link]

Table of Contents
Notes to Edition 2.0 alpha​ 3
Chapter 1 - Introduction to Pseudocode​ 3
Chapter 2 - Variables​ 4
2A - Variables​ 4
2B - Variables on the Right Hand side of ←​ 7
2C - Trace Tables​ 10
2D - Self-assignment​ 15
2E - Writing Pseudocode​ 17
Chapter 3 - Boolean Expressions​ 19
3A - Boolean Expressions​ 19
3B - Compound Boolean Expressions​ 21
3C - Boolean Expressions involving Variables​ 23
3D - Writing Pseudocode​ 26
Chapter 4 - Conditional Statements​ 28
4A - IF Statements Analysis​ 28
4B - Writing Pseudocode​ 34
4C - IF / ELSE IF / ELSE Statements Analysis​ 36
4D - Writing Pseudocode​ 42
Chapter 5 - While Loops​ 45
5A - While Loops Analysis​ 45
5B - Writing Pseudocode​ 52
5C - Applications​ 55
Chapter 6 - Functions​ 60
6A - Functions Analysis​ 60
6B - Writing Functions​ 66
6C - Multiple Functions Analysis​ 70
Chapter 7 - Introduction to Lists​ 73
7A - Initialising Lists​ 73
7B - List Elements​ 76
7C - Appending to Lists​ 78
7D - List Indices​ 81
7E - Indexing Lists​ 82
7F - Indexing Lists with Expressions​ 84
7G - Looping Through Lists (Analysis)​ 86
7H - Looping Through lists (Writing Code)​ 91
7I - Updating lists (Analysis)​ 92
7J - Updating lists (Writing Code)​ 96
Appendix A - Pseudocode to Python​ 97
Solutions to Exercises​ 98

2
Successful Pseudocoding
© Code for Schools, [Link]

Notes to Edition 2.1 alpha


Chapters 6 and 7 have been added which covers functions and lists.

Added a URL and QR code to the front page.

As a result, this resource now provides a complete course of tools and techniques for students
to analyse and design mathematical algorithms required at senior secondary level.

Still to do:
-​ Deeper and clearer elaborations of concepts
-​ Links to animated / video walkthroughs of trace tables
-​ Fix errors in questions and solutions (of which I’m sure there are many!)

Any questions, comments, feedback, or errors please let the author know via
toan@[Link]

3
Successful Pseudocoding
© Code for Schools, [Link]

Chapter 1 - Introduction to Pseudocode


An algorithm is the set of steps to solve a logical or mathematical problem.

Pseudocode is a structured way of representing instructions for humans to follow the algorithm.

Algorithms are generally written in pseudocode.

Like coding languages which you may have come across previously (Scratch, Python, Java,
C++ etc.) pseudocode does have a set of rules to follow with regard to how to use its symbols
(operators) and how to represent numbers etc. However, unlike these coding languages, the
rules are more relaxed and pseudocode is designed for humans to read and understand.

Pseudocode also provides a common format for us humans to share algorithms between
ourselves. For instance, when one person only knows how to program in Python and another
person only knows how to program in Javascript, pseudocode provides a relaxed “common
ground” for us both.

If we would like to have a computer run our algorithm, we will need to translate pseudocode into
actual program code by using a particular programming language. Appendix A covers briefly
how to convert the algorithms in this book into the Python programming language - so you can
see these algorithms “come to life” on the computer.

4
Successful Pseudocoding
© Code for Schools, [Link]

Chapter 2 - Variables

2A - Variables

A variable is named location in the computer’s memory which we use to store data values.
We use variables to represent unknown quantities, represent quantities which change
(vary-able) and to generally keep track of values.

To store a value inside a variable we use the assignment operator, ←.


← value
variable_name

Whenever we see an ← sign, a variable must be on the left hand side (because we’re storing a
value into it).

We may also see variables on the right hand side too (see later section).

We read this as “value is assigned to variable_name”.


For example, for the instruction:

x ← 10
We read this as “10 is assigned to x” or “x is assigned the value of 10”

Any valid mathematical expression can be found on the right hand side of the assignment

operator ←. Any arithmetic expressions are evaluated using standard B(O/I)DMAS rules.

5
Successful Pseudocoding
© Code for Schools, [Link]

Examples:
Which of these are valid assignment statements?

Assignment Statement Valid?

a.
c ← 42 True

b.
10 ←c False
(the left hand side must be a variable)

c. 14 → x False
(the assignment operator is in the
wrong direction)

d.
y ← (10 + 2) ✕ 3
True

e. (20 - 8) / 3 → d False
(the assignment operator is in the
wrong direction)

g. x + 3 False
(there is no assignment operator - the
value in the x variable does not change)

h.
area ← 42 True

6
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:
Which of these are valid assignment statements?

Assignment Statement Valid?

1.
d ←0
2.
x ← 30.0 - 2
3. 0→x

4. (20 - 8) / 3 → y

5.
x + 2 ←4
6.
z + x ← (10 + 2) + 3

7.
g ← ((10 / 2) ✕ (3 + 5)) - 3.3

8. a / 3

9. x + z + 3

10.
perimeter ← 2 * 10 + 2 * 5

7
Successful Pseudocoding
© Code for Schools, [Link]

2B - Variables on the Right Hand side of ←


When a variable is found on the right hand side of the ← , this means that the value currently
assigned to the variable is substituted for that variable in the expression.

Variable on the right hand side: value is read from the variable.
Variable on the left hand side: value is written to the variable.

Any variable found on the right hand side does not change its value.
If we wish to update the value in a variable we must put it on the left hand side.

When there are two or more pseudocode instructions, they are executed in order, one line at a
time, from top to bottom.

Any variable found on the right hand side of the ← , must already have a value assigned to it
otherwise the pseudocode is invalid.
E.g.

x ←y+3
The above pseudocode is invalid there has been no value assigned to the y variable.

y ←3
x ← y + 3
The above pseudocode is now valid (read from top to bottom).

y ←x-3
x ← y + 3
The above pseudocode is invalid because in the very first line, x does not have a value
assigned to it (read from top to bottom).

x ←0
y ← x - 3

x ← y + 3
The above pseudocode is now valid (read from top to bottom).

8
Successful Pseudocoding
© Code for Schools, [Link]

Examples:
What is the final value of x and y in each of the following pseudocode programs:

Assignment Statement Final values of x and y

a.
y ← 42 x: 21
y: 42

x ← 21

b.
y ← 42 x: 44
y: 42

x ← y + 2

c.
y ← 10 + 2 x: 6
y: 12

x ← y / 2

9
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:
What is the final value of x and y in each of the following pseudocode programs:

Assignment Statement Final values of x and y

1.
y ← -3
x ← -6

2.
y ← 0.4
x ← y / 2

3.
y ← -3 + 2
x ← y + 2

10
Successful Pseudocoding
© Code for Schools, [Link]

2C - Trace Tables
Trace tables are an extremely useful tool for keeping track of variable values throughout the
execution of pseudocode. Once you learn how to use trace tables effectively, you will be able to
deconstruct algorithms and see how they work.

The trace table below consists of 2 columns, one listing the variable names, and the other
containing the values found within the corresponding variable.

Variable Value

To use a trace table with pseudocode, we step through the pseudocode, one line at a time, and
record any changes to variables and their values in the tracetable. It is important to keep the
trace table updated with every line of pseudocode!

11
Successful Pseudocoding
© Code for Schools, [Link]

Here’s a step-by-step example:

y ←0
y ← 3

x ← y - 2

y ← x

x ← y + 5

y ←0
Variable Value
y ← 3
y 0
x ← y - 2

y ← x

x ← y + 5

y ←0
Variable Value
y ← 3
y 0, 3
x ← y - 2

y ← x

x ← y + 5

y ←0
Variable Value
y ← 3
y 0, 3
x ← y - 2
x 1
y ← x

x ← y + 5

12
Successful Pseudocoding
© Code for Schools, [Link]

y ←0
Variable Value
y ← 3
y 0, 3, 1
x ← y - 2
x 1
y ← x

x ← y + 5

y ←0
Variable Value
y ← 3
y 0, 3, 1
x ← y - 2
x 1, 6
y ← x

x ← y + 5

13
Successful Pseudocoding
© Code for Schools, [Link]

Examples:
Using trace-tables, execute the following pseudocode and determine the final values of all the
variables:

Pseudocode Trace-table

a.
a ←1
Variable Value
b ← a - 3
a 1, -1
a ← b + 1
b -2

b.
x ←3-2
Variable Value
y ← x / 2
x 1, 3
z ← x + y
y 0.5
x ← z / y z 1.5

c.
x ← -3
Variable Value
y ← x + x
x -3, 2
x ← y / x
y -6, 4
y ← x * x

14
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:
Using trace-tables, execute the following pseudocode and determine the final values of all the
variables:

Pseudocode Trace-table

1.
a ← 10
b ← 20

a ← b

b ← a

2.
x ← 10
y ← 20

z ← x

x ← y

y ← z

3.
x ←8
y ← x - x / 2

x ← y + x

y ← x * x

4.
x ←1
y ← 1

z ← x + y

x ← y + z

y ← x + z

z ← x + y

15
Successful Pseudocoding
© Code for Schools, [Link]

2D - Self-assignment
The same variable can be found on both sides of the assignment operator.

E.g.

x ←2
x ← x + 1

When this occurs, as usual, we evaluate the right hand side of the assignment operator first and
read from the variable, operate on it, and then assign it back to the variable.

Remember that any operations on the right hand side of the assignment statement does not
change the value of any variables. The values are only read from the variables.

Examples:
Using trace-tables, execute the following pseudocode and determine the final values of all the
variables:

Pseudocode Trace-table

a.
x ←0
Variable Value
x ← x + 1
x 0, 1

b.
x ←2*3
Variable Value
x ← x * 2
x 6, 12, 144
x ← x * x

c.
a ← -3
Variable Value
b ← a + a
a -3, 0.5
a ← a / b
b -6

16
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:
Using trace-tables, execute the following pseudocode and determine the final values of all the
variables:

Pseudocode Trace-table

1.
a ← 10
a ← a / 2

2.
x ←8
y ← (x + 4) / 3

y ← y * 3

y ← y - 4

3.
x ←4
y ← 8

y ← x + y

x ← y - x

y ← y - x

17
Successful Pseudocoding
© Code for Schools, [Link]

2E - Writing Pseudocode

Examples:
Write the pseudocode to achieve the goal provided.

Goal Code

a. ●​ Assign the value 10 to the variable, a


●​ Square the value of a and assign it to the a← 10
variable b
b ← a * a

b. ●​ Assign the value 3 to the variable, r


●​ Calculate the area of a circle with the r←3
radius given by the value in the variable r
●​ Assign the result to the variable, a a ← 3.14159 * r * r

c. ●​ Assign the value 2 to the variable l


●​ Assign the value 3 to the variable w l ←2
●​ Calculate the area of a rectangle with a
length given by l, and a width given by w w ← 3
●​ Assign the result to the variable a
a ← l * w

d. ●​ Assign the value -2 to the variable, x


●​ Double the value of x and assign it back x ← -2
to x
x ← x * 2

18
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:
Write the pseudocode to achieve the goal provided.

Goal Code

1. ●​ Assign the value -20 to the variable, a


●​ Halve the value of a and assign it to the
variable b

2. ●​ Assign the value 0.6 to the variable, d


●​ Calculate the area of a circle with the
diameter given by the value in the
variable d
●​ Assign the result to the variable, a

3. ●​ Assign the value 2 to the variable l


●​ Assign the value 3 to the variable w
●​ Assign the value 4 to the variable d
●​ Calculate the volume of a cuboid with a
length given by l, a width given by w, and
a depth given by d
●​ Assign the result to the variable v

4. ●​ Assign the value 10 to the variable, x


●​ Double the value of x and assign it back
to x
●​ Triple this value and assign it back to x

5. ●​ Assign the value 180 to the variable, h


●​ Assign the value 80 to the variable, w
●​ Calculate the BMI given with a height (in
cms) given by h, and a weight (in kgs)
given by w
○​ BMI = kgs / m2
●​ Assign the answer to the variable, bmi

19
Successful Pseudocoding
© Code for Schools, [Link]

Chapter 3 - Boolean Expressions

3A - Boolean Expressions
Boolean expressions are expressions which evaluate to either True or False.

The following operators are used to compare the left and right hand operands.
They will always evaluate to True or False.

●​ > is greater than


●​ < is less than
●​ >= is greater than or equal to
●​ <= is less than or equal to
●​ == is equal to
●​ != is not equal to

The not operator will negate (flip between True and False) whatever boolean expression follows
it.

Examples:

Expression Evaluation

a. 1<3 True

b. 3 == 1 + 2 True

c. True True

d. 3 != 1 True

e. not False True

f. not (2 == 2) False

20
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:
Expression Evaluation

1. 5 == 2

2. 10 < 10

3. 2 != 2

4. 32 >= 32

5. (5 + 3) < 8

6. 7 + 2 * 2 == (11 - 0)

7. True == False

8. 4.1 >= 4.01

9. False

10. not True

11. not (1 <= 0)

21
Successful Pseudocoding
© Code for Schools, [Link]

3B - Compound Boolean Expressions

We can combine two or more boolean expressions together using the and and or operators.

The and operator will evaluate to True only when both the LHS and RHS are True. Any other
combination will evaluate to False.

The or operator will evaluate to True when either the LHS or RHS are True. If both LHS and
RHS are False, it will evaluate to False.

Examples:
Evaluate the following combined boolean expressions.

Expression Evaluation

a. 1 == 1 and 2 == 2 True

b. 4 == -4 and 10 == 10 False

c. 10 < 8 and 10 > 1 False

d. 4 >= 4 and 3 > 2 True

e. 6 == 5 or 7 < 8 True

f. 10 > 3 or 4 == 3 True

g. 8 < 2 or 4 >1 True

h. (9 > 1 and 7 < 10) or (40 > 100) True

i. True and False False

22
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:
Evaluate the following combined boolean expressions.

Expression Evaluation

1. 3 <=3 and 4 <= 4

2. 8 > 0 or 1 < 0

3. 10 == (9 + 1) and 3 < 5

4. 1 > 0 and 6 > 2

5. 50 < 34 or 34 > 43

6 10 == 10 and 10 < 10

7. True and False

8. False or True

9. (10 > 5 and 4 > 1) or (50 >= 50)

10. True or False

11. True and True

12. False or False

23
Successful Pseudocoding
© Code for Schools, [Link]

3C - Boolean Expressions involving Variables


We can read values from variables within boolean expressions and also assign boolean
expressions to variables.

The same rules for assignment statements apply.

Examples:
Using trace-tables, execute the following pseudocode and determine the final values of all the
variables:

Pseudocode Trace-table

a.
age ← 10
Variable Value
result ← age < 18
age 10

result True

b.
height ← 1.83
Variable Value
six_feet ← height >= 1.83
height 1.83

six_feet True

c.
f ← 32.0
Variable Value
c ← (f - 32.0) * 5/9
f 32.0
freezing ← c <= 0
c 0

freezing True

24
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:
Using trace-tables, execute the following pseudocode and determine the final values of all the
variables:

Pseudocode Trace-table

1.
age ← 18
result ← age < 18

2.
date ← 31/01/2001
return_by ← 28/02/2001

late ← date > return_by

3.
height ← 180
height ← height / 100

six_feet ← height >= 1.83

4.
t ← 200.0
t ← (t - 32.0) * 5/9

boiling ← t >= 100

5.
hours ← 150
age ← 19

test_passed ← True

licensed ← hours > 120 and age > 18 and

Test_passed

25
Successful Pseudocoding
© Code for Schools, [Link]

6.
distance ← 1500
distance ← distance / 1000

fast_food ← True

rating ← 4

order ← distance < 2.0 and rating >= 4


and not fast_food

26
Successful Pseudocoding
© Code for Schools, [Link]

3D - Writing Pseudocode

Complete the following programming challenges.

Examples:

Requirements Pseudocode

a. ●​ A student obtained a score of 17 out of


20 score ← 17 / 20
●​ Write pseudocode to:
○​ Convert this score to a score ← score * 100
percentage
○​ Assign a boolean value to the A ← score >= 80
variable, A, indicating whether
or not the percentage was
greater than or equal to 80

b. ●​ A rectangle has a length of 7, and a


width of 8 length ←7
●​ Write pseudocode to:
○​ Determine whether the width ← 8
rectangle is a square
○​ Assign a boolean value to the is_square ← length == width
variable, is_square, indicating
whether or not the rectangle
has sides of equal length

27
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:

Requirements Pseudocode

1. ●​ A student obtained a score of 16 out of


30
●​ Write pseudocode to:
○​ Convert this score to a
percentage
○​ Assign a boolean value to the
variable, pass, indicating
whether or not the percentage
was greater than or equal to 50

2. ●​ A rectangle has a length of 3, and a


width of 5
●​ Write pseudocode to:
○​ Determine the area
○​ Assign a boolean value to the
variable, is_large, indicating
whether or not the rectangle
has an area greater than 15

28
Successful Pseudocoding
© Code for Schools, [Link]

Chapter 4 - Conditional Statements

4A - IF Statements Analysis

An IF statement is used to control which sequence of instructions is executed depending on the


result of a boolean expression.

The IF statement must always be followed by a boolean expression (this is called the
condition).

If the boolean expression (condition) evaluates to True, any sequence of instructions which are
indented underneath the IF statement is executed.

If the condition evaluates to False, the sequence of instructions indented underneath the IF
statement is skipped and the next instruction underneath the IF statement at the same
indentation level is executed.

Examples:

Code Trace Table

a.
x ← 0
if x <= 0 Variable Value

x ←1 x 0, 1

b.
x ← 10
if x >= 5 Variable Value

x ←x/2 x 10, 5, 15

x ← x + 10

29
Successful Pseudocoding
© Code for Schools, [Link]

c.
x ←2
Variable Value
y ← 1
x 2
min ← x
y 1
if y < x

min ←y min 2, 1

* How to determine the smaller


of 2 numbers.

* Notice how the minimum is set


up “by default” to be x. It is only
changed to y if the value in y is
less than x.

d.
x ←2
Variable Value
y ← 1
x 2
max ← x
y 1
if y > x

max ←y max 2

* How to determine the larger of


2 numbers.

e.
x ←6
if x > 2 and x < 10 Variable Value

←x+2
x x 6, 8, 4

x ← x / 2

f.
x ←12
if x > 0 and x < 5 Variable Value

←x-2
x x 12, 6

x ← x / 2

30
Successful Pseudocoding
© Code for Schools, [Link]

g.
t ← 21
Variable Value
fan_on ← False
t 21
if t >= 20 and fan_on == False

fan_on ← True fan_on False, True

h.
x ←3
if x >= 0 Variable Value

x ←
-x x 3, -3, 0
if x <= 0

x ←x+3
i.
x ←-3
if x >= 0 Variable Value

x ←
-x x -3, 0
if x <= 0

x ←x+3

31
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:

Code Trace Table

1.
y ←-3
if y <= 0

y ←y*2
2.
x ←1
if x >= 1

x ←x+2
x ← x - 10

3.
x ←4
if x > 4 and x < 6

←x-1
x

x ← x / 2

4.
z ←10
if z > 0 and z < 50

←x/2
x

x ← x - 2

5.
t ← 21
fan_on ← True
if t >= 20 and fan_on == False

fan_on ← True

32
Successful Pseudocoding
© Code for Schools, [Link]

6.
t ← 24
fan_on ← False
if t >= 20 and fan_on == False

fan_on ←
True
if t <= 16 and fan_on == True

fan_on ← False
7.
y ←1
if y >= 0

y ←
-y
if y <= 0

y ← y * -10
8.
x ←10
if x > 5 and x < 15

x ←
-x
if x <= 0

← x + 10
x

x ← x + 3

9.
day_hours ← 10
night_hours ← 2

is_night ← False
if is_night and night_hours < 120

night_hours ←
night_hours + 1
if not is_night and day_hours <
120

day_hours ← day_hours + 1

33
Successful Pseudocoding
© Code for Schools, [Link]

10.
x ←8
y ← 10

z ← 2

min ← x
if y < x and y < z

min y←
if z < x and z < y

min ←z
11.
x ←8
y ← 10

z ← 2

max ← x
if y > x and y > z

max y←
if z > x and z > y

max ←z

34
Successful Pseudocoding
© Code for Schools, [Link]

4B - Writing Pseudocode

For the exercises below, write the pseudocode to fulfill the requirements

Examples:

Requirements Pseudocode

a. ●​ Assign 81 to the variable, score


●​ Assign False to the variable, score ← 81
distinction
●​ If score is greater than or equal to 75, distinction ←
False
then assign the value True to the if score >= 75
variable distinction
distinction ← True
b. ●​ Assign True to the variable, in_victoria
●​ Assign 10 to the variable, time in_victoria ← True
●​ If in_victoria then add 1 to the variable
time time ← 10
if in_victoria

time ← time + 1
c. ●​
●​
Assign 18 to john_age
Assign 20 to jane_age john_age ← 18
●​ Assign 22 to jack_age
●​ Calculate the largest age and assign it jane_age ← 20
to the variable largest_age
jack_age ← 22

largest_age ← john_age
if jane_age > jack_age and
jane_age > john_age

largest_age ←
jane_age
if jack_age > jane_age
and jack_age > john_age

largest_age ← jack_age

35
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:

Requirements Pseudocode

1. ●​ Assign 58 to the variable, score


●​ Assign False to the variable, credit
●​ If score is greater than or equal to 60,
then assign the value True to the
variable credit

2. ●​ Assign False to the variable, in_WA


●​ Assign 12 to the variable, time
●​ If in_WA then subtract 1 from the
variable time

3. ●​ Assign 18 to john_age
●​ Assign 20 to jane_age
●​ Assign 22 to jack_age
●​ Calculate the middle age and assign it
to the variable mid_age

36
Successful Pseudocoding
© Code for Schools, [Link]

4C - IF / ELSE IF / ELSE Statements Analysis

When we would like the program to execute a sequence of instructions when the condition to an
IF statement is False, we use the else statement.

All the instructions indented beneath the else statement will be executed when the IF condition
is False.

Examples:

Code Trace Table

a.
x ← 0
if x > 0 Variable Value

x ←1 x 0, 2
else

x ←2
b.
x ← 10
if x > 20 Variable Value

x ←x+2 x 10, 8, 4
else

←x-2
x * Unindented code after IF
statements are always executed
x ← x / 2 sequentially afterwards

c.
x ← 16
if x > 20 Variable Value

x ←
x + 2 x 16, 14, 7
else if x > 10

x ←x-2
else

←2
x

x ← x / 2

37
Successful Pseudocoding
© Code for Schools, [Link]

d.
x ←8
if x > 20 Variable Value

x ←x+2 x 8, 12, 14,


7
x ← x + 4
else if x > 10

x ←x-2 * All indented pseudocode under


an IF statement are executed in
x ← x + 4
sequence.

else

←x+4
x

x ← x + 2

x ← x / 2

e.
a ← 32
Variable Value
b ← 42
a 32, 40
if a > 20 and b < 40

a ←b+2 b 42, 36

b ← a + 4
else if a > 20 and b < 50

a ←b-2
b ← a - 4
else if x > 30 and b < 50

a ←b+4
b ← a + 2
else

a ←b-4
b ← a - 2

38
Successful Pseudocoding
© Code for Schools, [Link]

f.
x ← 1
if x >= 1 Variable Value

x ←
x + 1 x 1, 2
else if x >= 2

x ←
x - 1
else if x >= 3

x ←x-2
else

x ←0
g.
x ← 1
if x >= 1 Variable Value

x ←
x + 1 x 1, 2, 1, 0
if x >= 2

x ←
x - 1 * Notice how the sequential IF
if x >= 3 statements are always checked
x ←x-2 (contrast with the example
above where the ELSE IFs are
else
only checked when the IF
x ←0 statement before it is false)

39
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:

Code Trace Table

1.
y ← -2
if y > 0

y ←y+1
else

y ←y-2
2.
z ← 20
if z >= 30

z ← z - 20
else

← z + 20
z

z ← z / 4

3.
x ← 2
if x >= 2

x ←
x + 2
else if x < 0

x ←x-2
else

←4
x

x ← x * 2

40
Successful Pseudocoding
© Code for Schools, [Link]

4.
y ←0
if y > 10

y ←y+2
y ← y + 4
else if y > 5

y ←y-2
y ← y + 4
else

←y+4
y

y ← y + 2

y ← y * 0.5

5.
a ← 16
b ← 20
if a > 20 and b < 30

a ←b+2
b ← a + 4
else if a > 10 and b < 30

a ←b-3
b ← a - 2
else if a > 0 and b < 50

a ←b+4
b ← a + 2
else

a ←b-4
b ← a - 2

41
Successful Pseudocoding
© Code for Schools, [Link]

6.
x ← 2
if x >= 1

x ←
x - 1
else if x >= 2

x ←
x + 1
else if x >= 3

x ←x-2
else

x ←0
7.
z ← 2
if z >= 2

z ←
z + 1
if z >= 3

z ←
z - 1
if z >= 4

z ←z-2
else

z ← z * 10

42
Successful Pseudocoding
© Code for Schools, [Link]

4D - Writing Pseudocode

For the exercises below, write the pseudocode to fulfill the requirements

Examples:

Requirements Pseudocode

a. ●​ Assign 61 to the variable, score


●​ If score is greater than or equal to 80, score ← 61
then assign the value 1 to the variable
grade if score >= 80
●​ If score is between 70 inclusive and 80
exclusive, then assign the value 2 to grade ←1
the variable grade else if score >= 70 and
●​ If score is between 60 inclusive and 70 score < 80
exclusive, then assign the value 3 to
the variable grade grade ←2
●​ If score is between 50 inclusive and 60 else if score >= 60 and
exclusive, then assign the value 4 to score < 70
the variable grade
●​ If the score is below 50, assign the grade ←3
value 5 to the variable grade else if score >= 50 and
score < 60

grade ←4
else

grade ←5
OR

score ← 61
if score >= 80

grade ←1
else if score >= 70

grade ←2
else if score >= 60

grade ←3
else if score >= 50

grade ←4
else

grade ←5

43
Successful Pseudocoding
© Code for Schools, [Link]

b. ●​ Assign 2 to the variable, x


●​ Is x is greater than 1, assign x to the x ←2
variable y
●​ If x is less than -1, assign -x to the if x > 1
variable y
●​ If x is between -1 inclusive and 1 y x←
inclusive, assign 0 to the variable y else if x < -1

y ← -x
else

y ←0

44
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:

Requirements Pseudocode

1. ●​ Assign 59 to the variable, score


●​ If score is greater than or equal
to 85, then assign the value 4 to
the variable GPA
●​ If score is between 70 inclusive
and 85 exclusive, then assign
the value 3 to the variable GPA
●​ If score is between 60 inclusive
and 70 exclusive, then assign
the value 2 to the variable GPA
●​ If score is between 50 inclusive
and 60 exclusive, then assign
the value 1 to the variable GPA
●​ If the score is below 50, assign
the value 0 to the variable GPA

2. ●​ Assign -3 to the variable, x


●​ Is x is greater than 2, assign x2
to the variable y
●​ If x is less than -2, assign -x2 to
the variable y
●​ If x is between -2 inclusive and 2
inclusive, assign 0 to the
variable y

3. ●​ Assign 2 to the variable, x


●​ Assign 6 to the variable, y
●​ Assign 3 to the variable, z
●​ Determine the which value is the
product of the other 2 values
and assign that value into the
variable, multiple
●​ If no values are a multiple of the
other 2, then assign -1 to the
variable, multiple

4. ●​ Assign 4 to the variable, w


●​ Assign 2 to the variable, x
●​ Assign 6 to the variable, y
●​ Assign 3 to the variable, z
●​ Determine the which is the
largest value and assign that
value into the variable, max

45
Successful Pseudocoding
© Code for Schools, [Link]

Chapter 5 - While Loops

5A - While Loops Analysis

When we need to repeat a sequence of instructions, we apply a concept called a loop.

There are a few types of loops in Python, in this section we will discuss the while loop.

A while loop operates similarly to an IF statement. The while keyword is followed by a boolean
expression (called the condition), like the IF statement. If the boolean expression (condition)
evaluates to True, the code indented underneath the while statement is executed (just like in the
IF statement). We call this "entering the loop".

However, unlike the IF statement, when the indented sequence of instructions are completed,
the while condition is re-evaluated. If the condition is True, then the indented code is executed
again. After each execution of the sequence of indented code, the condition is always
re-evaluated, and the indented block of code is always repeated while the condition is True. To
avoid the loop going on forever (an infinite loop) we need to ensure that some part of the code
within the indented sequence of instructions will eventually cause the while condition to become
False.

When the condition is False, the next unidented instruction underneath the while statement is
executed. We call this "exiting the loop".

Examples:

Code Trace Table

a.
x ← 0
while x < 3 Variable Value

x ←x+1 x 0, 1, 2, 3

b.
x ← 0
while x <= 3 Variable Value

x ←x+1 x 0, 1, 2, 3,
4

46
Successful Pseudocoding
© Code for Schools, [Link]

c.
i ←1
Variable Value
t ← 0
i 1, 2, 3, 4,
while i <= 4
5
t ←t+i t 1, 3, 6, 10
i ← i + 1

* This is how we can sum all


integers from 1 to 4

d.
a ←0
Variable Value
b ← 1
a 0, 1, 3, 8, 21
while a <= 8

a ←a+b b 1, 2, 5, 13, 34

b ← a + b
* produces the fibonacci
sequence

e.
x ← -2
Variable Value
sum ← 0
x -2, -1, 0, 1, 2, 3
while x <= 2

y ←x*x y 4, 1, 0, 1, 4

sum ← sum + y
sum 0, 4, 5, 5, 6, 10

x ← x + 1
* sums all the values of x2 for x
𝞊 Z and -2 <= x <= 2

47
Successful Pseudocoding
© Code for Schools, [Link]

f.
x ← -1
Variable Value
min ← ∞
x -1, 0, 1, 2, 3
while x < 3

y ←(x - 1)2 + 3 y 7, 4, 3, 4
if y < min min ∞, 7, 4, 3
min ←y
x ←x+1 * finds the minimum y value for
-1 <= x <= 2

g.
a ←0
Variable Value
b ← 1
a 0, 1, 2
f ← 0
b 1, 2, 3
n ← 1 f 0, 1, 1, 2, 3
while n < 6
if n == 1 n 1, 2, 3, 4, 5, 6
f ←a
else if n == 2
* f will be the nth fibonacci
f ←b number
else

f ←a+b
a ← b

b ← f

n ← n + 1

48
Successful Pseudocoding
© Code for Schools, [Link]

h.
x1 ←0
Variable Value
max_grad_x← x1 x1
max_grad ← -∞
max_grad_x

while x1 <= 1 max_grad

y1 ← -x1 - 3x1 + 6x1 - x1 - 2


4 3 2 y1

x2 ← x1 + 0.5 x2

y2 ← -x2 - 3x2 + 6x2 - x2 - 2


4 3 2 y2

grad ← (y2 - y1) / (x2 - x1)


grad

if grad > max_grad


* finds the maximum gradient
max_grad ← grad for 0 <= x <= 1 in increments of
0.5
max_grad_x ← (x1 + x2) / 2

x1 ← x2

49
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:

Code Trace Table

1.
x ←3
while x > 0

x ←x-1
2.
x ←0
while x <= 6

x ←x+2
3.
i ← 10
t ← 0
while i <= 15

t ←t+i
i ← i + 1

4.
a ←1
b ← 2
while a <= 21

a ←a+b
b ← a + b

5.
x ←0
sum ← 0
while x <= 2

y ← -x + 2x2

sum ← sum + y

x ← x + 0.5

50
Successful Pseudocoding
© Code for Schools, [Link]

6.
x ←0
min ← ∞
while x <= 2

y ←x2 - 2x
if y < min

min ←y
x ← x + 0.5
7.
x ←0
max ← -∞
while x <= 2

y ←-x3 + 3x
if y > max

max ←y
x ← x + 0.5

51
Successful Pseudocoding
© Code for Schools, [Link]

8.
a ←2
b ← 1

L ← 0

n ← 1
while n < 6
if n == 1

L ←a
else if n == 2

L ←b
else

L ←a+b
a ← b

b ← L

n ← n + 1

(What is the name of this


sequence?)

52
Successful Pseudocoding
© Code for Schools, [Link]

5B - Writing Pseudocode
For the exercises below, write the pseudocode to fulfill the requirements

Examples:

Requirements Pseudocode

a. ●​ Find the sum of all the integers


between 10 and 90 i ← 10
sum ← 0
while i <= 90

sum← sum + i
i ← i + 1

b. ●​ Find the sum of the first 10 square


numbers (ie. 1 + 4 + 25 + 36 … + 100) i ←1
sum ← 0
while i <= 10

square ←i*i
sum← sum + square
i ← i + 1

c ●​ Find the minimum value of y = 0.5x4 -


x3 -2x2 for x ∈ [-1, 3], x ∈ Z x ← -1
min ← ∞
while x <= 3

y ←0.5x4 - x3 -2x2
if y < min

min ←y
x ←x+1

53
Successful Pseudocoding
© Code for Schools, [Link]

d. ●​ Find the x intercept for


y = 0.5x3 - x2 - 0.18x + 0.36 x ←1
where x ∈ [1, 1.5. 2, 2.5]
x_intercept x ←
while x <= 2.5

y ←0.5x3-x2–0.18x+0.36
if y == 0

x_intercept ←x
x ← x + 0.5
e. ●​ Find the sum of y values for
y = 0.5x4 - x3 -2x2 for x ∈ [-1, 3], x ∈ Z x← -1
sum ← 0
while x <= 3

y ← 0.5x - x
4 3
-2x2

sum ← sum + y

x ← x + 1

54
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:

Requirements Pseudocode

1. ●​ Find the sum of all the even numbers


between 20 and 90

2. ●​ Find the sum of the first 10 cube


numbers (ie. 1 + 8 + 27 + … + 1000)

3. ●​ Calculate 20! (20 factorial)

4. ●​ Find the minimum value of


y = sin x + cos x for x ∈ [0, 4], x ∈ Z

5. ●​ Find the sum of y values for


y = sin x + cos x for x ∈ [0, 4], x ∈ Z

6 ●​ The tribonacci numbers are like the


Fibonacci numbers, but instead of
starting with two predetermined terms,
the sequence starts with three
predetermined terms and each term
afterwards is the sum of the preceding
three terms.
●​ The first few tribonacci numbers are:
0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81
●​ Find the 20th tribonacci number

55
Successful Pseudocoding
© Code for Schools, [Link]

5C - Applications
For the exercises below, write the pseudocode to fulfill the requirements

Examples:

Requirements Pseudocode

a. ●​ Calculate 20! (factorial)


n ← 20
factorial ←1
while n >= 1

factorial ← factorial * n
n ←n-1
OR

n ←1
factorial ←1
while n <= 20

factorial ← factorial * n
n ←n+1

56
Successful Pseudocoding
© Code for Schools, [Link]

b The Leibniz formula for π,


named after Gottfried Leibniz, n ←1
states that
sum ← 0

sign ← 1
●​ Calculate the value for PI after
100 iterations denom ← 1
while n <= 100

← sign * 1 / denom
term

sum ← sum + term

sign ← -sign

denom ← denom + 2

n ← n + 1

PI ← sum * 4

57
Successful Pseudocoding
© Code for Schools, [Link]

c.
●​ Calculate 3
using bisection a ← -2
b ← -1
method, where a = 2 and b = -1

c ← (a + b) / 2

f_c ← c - 32

accuracy ← |f_c|

answer ← a
while accuracy > 0.01

f_a ←a 2
- 3

f_b ← b 2
- 3
if f_a * f_c < 0

b ←c
else

a←c
c ← (a + b) / 2

f_c ← c - 3 2

accuracy ← |f_c|

answer ← c

58
Successful Pseudocoding
© Code for Schools, [Link]

d. ●​ Using the left-endpoint estimate


rectangle rule for integration,​ a ←1

calculate the area under the b ← 6
curve ​
y = 9 - 0.1x2 ​ n ← 10

between x = 1 and x = 6 using 10 step ← (b - a) / n


rectangles
sum ← 0

x ← a
while x < b

f_x ← 9 - 0.1x2

sum ← sum + f_x * step

x ← x + step
—------------ OR —---------
a ←1
b ← 6
n ← 10
step ← (b - a) / n
sum ← 0

x ← a
while x < b

← 9 - 0.1x
f_x 2

sum ← sum + f_x

x ← x + step

sum ← sum * step

59
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:

Requirements Pseudocode

1. loge2 =

●​ Calculate ln 2 using the above


series up to 200 terms

2. 3
●​ Calculate 4
using the
bisection method

3. ●​ Using the Trapezoidal rule for


integration,​

calculate the area under the
curve ​
y = 9 - 0.1x2 ​

between x = 2 and x = 5 using 6


trapeziums

60
Successful Pseudocoding
© Code for Schools, [Link]

Chapter 6 - Functions

6A - Functions Analysis
Functions are chunks of pseudocode which have been grouped together under a name and
perform a specific task or calculation, given some information.

For example, let’s imagine we would like to add 1 to the value in a variable. We might write a
function like the one below:

def addOne(x)

result ←x + 1
return result

This is the name of the function.


This is the body of the function.
This is the parameter to the function.

When we write a function like the one above, the pseudocode does not run… yet. In order to
use the function, we must call it.

For example:

z ← addOne(10)
This is the name of the function we are calling.
This is the argument given to the function.

Notice a few points:


●​ To call the function, we must use the same name we defined the function as
●​ The number of arguments must match the number of parameters

61
Successful Pseudocoding
© Code for Schools, [Link]

Examples:
Complete a trace table for each of the pseudocode functions below.

Code Trace Table

a. def add(x, y)

total ←x + y Variable Value


return total
x 10

z ← add(10, 20) y 20

total 30

z 30

b. def add(x, y)
return x + y
Variable Value

z ← add(10, 20) x 10

y 20

z 30

c. def square(x)

sq ← x * x Variable Value
return sq
x 5

z ← square(5) sq 25

z 25

d. def cube(x)

cb ← x * x * x Variable Value
return cb
side 4

side ←4 x 4

z ← cube(side) cb 64

62
Successful Pseudocoding
© Code for Schools, [Link]

z 64

e. def quadruple(x)
return x * x * x * x
Variable Value

a ←3 a 3

z ← quadruple(a) z 81

f. def hypotenuse(a, b)

c ← 𝑎 +𝑏
2 2 Variable Value
return c side1 3

side1 ←3 side2 4

a 3
side2 ← side1 + 1
b 4
z ← hypotenuse(side1, side2)
c 5

z 5

63
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:
Complete a trace table for each of the pseudocode functions below.
Code Trace Table

1. def sub(x, y)

diff ←x - y
return diff

z ← sub(100, 90)

2. def sub(x, y)
return x - y

z ← sub(10, 9)

3. def double(x)

db ← x + x
return db

z ← double(9)

4. def halve(x)
return x / 2

num← 10
z ← halve(num)

64
Successful Pseudocoding
© Code for Schools, [Link]

5. def energy(m)
return m * 3000000002

mass ←5
z ← energy(mass)

6. def area(l, w)

a ← l * w
return a

x ←3
y ← 4

z ← area(x, y)

7. def perimeter(l, w)
return 2 * (l + w)

x ←3
y ← 4

z ← perimeter(x, y)

65
Successful Pseudocoding
© Code for Schools, [Link]

8. def min(x, y)

result ←x
if y < x

result y←
return result

z ← min(10, 5)

66
Successful Pseudocoding
© Code for Schools, [Link]

6B - Writing Functions
For the exercises below, write the pseudocode to fulfill the requirements

Examples:

Requirements Pseudocode

a. Write a function: def third(x)


●​ named: third return x / 3
●​ with a parameter: x
●​ which returns x / 3
answer ← third(9)
Then:
●​ call the function
●​ supply it an argument of 9,
●​ and assign the returned value to the
variable answer

b. Write a function: def multiply(a, b)


●​ named: multiply return a * b
●​ with 2 parameters: a, b
●​ which returns the product of a and b product ← multiply(-2, 6)
Then:
●​ call the function
●​ supply it arguments of -2 and 6
●​ and assign the returned value to the
variable product

c Write a function: def average(x, y)


●​ named: average
●​ with 2 parameters: x, y sum ← x + y
●​ which returns the average of x and y return sum / 2

Then: z ← average(4, 6)
●​ call the function
●​ supply it arguments of 4 and 6
●​ and assign the returned value to the
variable z

67
Successful Pseudocoding
© Code for Schools, [Link]

d. Write a function: def isPositive(x)


●​ named: isPositive if x >= 0
●​ with 1 parameters: x return True
●​ which returns True if x is 0 or positive, else
False otherwise return False

Then:
p ← isPositive(10)
●​ call the function
●​ supply it an argument of 10 -----Alternative-----
●​ and assign the returned value to the
variable p def isPositive(x)

result← True
if x < 0

result ←False
return result

p ← isPositive(10)
e. Write a function: def max(a, b)
●​ named: max
●​ with 2 parameters: a, b result←a
●​ which returns a if a is larger than b, b if b > a
otherwise
result ←b
Then: return result
●​ call the function
●​ supply it arguments of -3, -4
●​ and assign the returned value to the c ← max(-3, -4)
variable c
-----Alternative-----

def max(a, b)
if a > b
return a
else
return b

c ← max(-3, -4)

68
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:
Write the pseudocode to fulfill the requirements
Requirements Pseudocode

1. Write a function: def circumference(r)


●​ named: circumference return 3.1416 * r * r
●​ with a parameter: r
●​ which returns the circumference of a
c ← circumference(2)
circle with radius, r
●​ You may use the value of 3.1416 as
an approximation for PI

Then:
●​ call the function
●​ supply it an argument of 2,
●​ and assign the returned value to the
variable c

2. Write a function: def divide(x, y)


●​ named: divide return x / y
●​ with 2 parameters: x, y
●​ which returns the quotient of x and y
quotient ← divide(5, -2)
Then:
●​ call the function
●​ supply it arguments of 5 and -2
●​ and assign the returned value to the
variable quotient

3. Write a function: def average(a, b, c)


●​ named: average return (a + b + c) / 3
●​ with 3 parameters: a, b, c
●​ which returns the average of a, b and d ← average(1, 2, 3)
c

Then:
●​ call the function
●​ supply it arguments of 1, 2, and 3
●​ and assign the returned value to the
variable d

69
Successful Pseudocoding
© Code for Schools, [Link]

4. Write a function: def canDrive(age)


●​ named: canDrive if age >= 18
●​ with 1 parameters: age return True
●​ which returns True if age is 18 or else
above, False otherwise return False

Then:
d ← canDrive(17)
●​ call the function
●​ supply it arguments of 17 -----Alternative-----
●​ and assign the returned value to the
variable d def canDrive(age)

result← True
if x < 8

result ←False
return result

d ← canDrive(17)
5. Write a function: def max(x, y, z)
●​ named: max if x >= y and x >= z
●​ with 2 parameters: x, y, z return x
●​ which returns the largest of the values else if y >= x and y >= z
in the parameters return y
else
Then: return z
●​ call the function
●​ supply it arguments of -3, -4, 0
biggest ← max(-3, -4, 0)
●​ and assign the returned value to the
variable biggest

70
Successful Pseudocoding
© Code for Schools, [Link]

6C - Multiple Functions Analysis


We may define multiple functions in our pseudocode, and call each function separately and
multiple times.

In some cases, we may also pseudocode within one function, call another function.

Examples:

Code Trace Table

a. def sub(x, y)

diff ←x - y Variable Value


return diff
x 6, 2
def double(x)
y 4
db ← x + x
diff 2
return db
a 2
a ← sub(6, 4) db 4
z ← double(a) z 8

b. def sub(x, y)

diff ←x - y Variable Value


return diff
x 10, 3
def double(x)
y 7
db ← x + x
diff 3
return db
db 6
z ← double(sub(10, 7)) z 6

71
Successful Pseudocoding
© Code for Schools, [Link]

d. def area(l, w)
return l * w
Variable Value
def perim(l, w)
return 2 * (l + w) length 4

def min(x, y) width 3

result ←x l 4
if y < x w 3
result y← a 12
return result
p 14
length←4 z 12

width ← 3

a ← area(length, width)

p ← perim(length, width)

z ← min(a, p)

e. def area(l, w)
return l * w
Variable Value
def perim(l, w)
return 2 * (l + w) a 4

def min(x, y) b 5

result ←x l 4
if y < x w 5
result y← z 18
return result

a ←4
b ← 5

z ← min(area(a, b), perim(a, b))

72
Successful Pseudocoding
© Code for Schools, [Link]

f. def square(x)

s ← x * x Variable Value
return s
r 2
def area_circle(r)
x 2
a ← 3.1416 * square(r)
s 4
return a
a 12.5664
z ← area_circle(2) z 12.5664

73
Successful Pseudocoding
© Code for Schools, [Link]

Chapter 7 - Introduction to Lists

7A - Initialising Lists
Lists are collections of values referenced by a single variable name.

For example, if we wanted to store 3 numbers: -45.2, 0.25, and 2.333, we could initialise 3
variables as follows:

number1 ← -45.2
number2 ← 0.25
number3 ← 2.333
Or we could use a list called names as follows:

numbers ← [-45, 0.25, 2]

We use square brackets [] to contain the values inside a list, and we use commas to separate
them.

Similarly, if we wanted to store 3 integers: 52, 21, 1 we could initialise 3 variables:

integer1 ← 52
integer2 ← 21
integer3 ←1
Or we could use a list called integers as follows:

integers ← [52, 21, 1]

74
Successful Pseudocoding
© Code for Schools, [Link]

Examples:
Write code that would initialise the following lists:

Initialisation goal Code

a. An empty list called heights.


heights ← []

b. A list of numbers called heights with the values: 1.5,


1.47, 1.83, 1.76 heights ← [1.5, 1.47, 1.83,
1.76]

c. A list of integers called ages with the values: 15, 19,


21, 65, 2 ages ← [15, 19, 21, 65, 2]

d. A list of floating point numbers called heights with the


values: 165.3, 182.45 heights ← [165.3, 182.45]

e. A list of booleans called hasPaid with the values: True,


True, False, True, True hasPaid ← [True, True,
False, True, True]

f. A list of: 3 lists of: integers called collection where


each of the inner list of integers contains the values: collection ← [[1, 2], [1,
1,2 2], [1, 2]]

75
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:
Write code that would initialise the following lists:

Initialisation goal Code

1. An empty list called prices.

2. A list of numbers called areas with the values: 25, 30, 4

3. A list of integers called scores with the values: 150, 0, 100, 200

4. A list of floating point numbers called temperatures with the values: 15.3, 42.0, 32.1

5. A list of booleans called registered with the values: False, True, True, False

6. A list of: 2 lists of: numbers called invoices where each of the inner list of numbers
contains the values: 100.0, 16.5

7. A list of: 3 lists called marks.

Each inner list contains 3 floating point numbers recording the marks of a student.

The details of each inner list are below:

100.0, 99.2, 95.7


76.2, 70.5, 78.0
85.0, 65.4, 72.2

76
Successful Pseudocoding
© Code for Schools, [Link]

7B - List Elements

Definition: The values inside a list are called ‘elements’.

Examples:
Consider the following lists:

lengths = [23, 12, 10, 0, 22, 31]

1)​ What is the name of the list?​ ​ lengths


2)​ What is the first element in the list? ​ 23
3)​ What is the last element in the list? ​ 31
4)​ What is the third element in the list?​ 10
5)​ What is the 5th element in the list?​ 22

dimensions = [[23, 12], [11, 13], [10, 5]]

1)​ What is the name of the list?​ ​ ​ ​ ​ dimensions


2)​ What is the 2nd element in the list?​ ​ ​ ​ [11, 13]
3)​ What is the last element in the list?​ ​ ​ ​ [10, 5]
4)​ What is the 1st element of the 2nd element in the list?​ 11
5)​ What is the 2nd element of the 1st element in the list?​ 12
6)​ What is the 1st element in the last element in the list?​ 10

77
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:
Identify the elements in the following lists:

Question

1. ages = [17,16, 21, 65, 6, 11]

1)​ What is the name of the list?


2)​ What is the second element in the list?
3)​ What is the last element in the list?
4)​ What is the fourth element in the list?
5)​ What is the 2nd last element in the list?

2. squares = [[2, 4], [3, 9], [4, 16]]

1)​ What is the name of the list?


2)​ What is the first element in the list?
3)​ What is the second element in the list?
4)​ What is the second element of the last element in the list?
5)​ What is the first element of the first element in the list?
6)​ What is the last element in the last element in the list?

78
Successful Pseudocoding
© Code for Schools, [Link]

7C - Appending to Lists

Lists can be expanded after they are initialised by ‘appending’ elements to them.

Appending an element will add that element to the end of the list.

So if we initialised a list as follows:

areas ← [24, 4, 6]

We can add the element 18 to the end of it by using the following pseudocode instruction:

[Link](18)

And the list will be updated to:


[24, 4, 6, 18]

79
Successful Pseudocoding
© Code for Schools, [Link]

Examples:
Write down the code which will fulfil the requirements.
Then write down the final values in the list.

Requirement Code Final List value

a. Initialise an empty list called weights. weights ←


[]
[0.22, 1.45,
1.23]
Append the element 0.22. [Link](0.22)
Append the element 1.45. [Link](1.45)
Append the element 1.23. [Link](1.23)

b. Initialise an empty list called ages. ages ← []


[17, 21, 7]
Append the element 17. [Link](17)
Append the element 21. [Link](21)
Append the element 7. [Link](7)

c. Initialise an empty list called pairs. pairs ← []


[[1, 4],
[2, 8],
Append the element [1, 4] [Link]([1, 4]) [4, 16]]
Append the element [2, 8] [Link]([2, 8])
Append the element [4, 16] [Link]([4, 16])

d. Initialise a list called scores as [200, scores ←


[200, 300]
[200, 300, 300,
250]
300] [Link](300)
Append the element 300. [Link](250)
Append the element 250

e. Initialise a list called cubes as cubes ← [[2, 8], [4, 64]]


[[2, 8],
[4, 64],
[ [2, 8], [4, 64] ] [Link]([-3, -9]) [-3, -9]]
Append the element [-3, -9]

f. Initialise an empty list called nums. nums ← []


[1, 2, 3, 4…
100]
Using a loop, append all the integers
from 1 to 100. i ← 0
while i <= 100
[Link](i)
num ← num + 1

g. Initialise a list called countdown as countdown ← [100 , 99]


[100, 99, 98, …
1]
[100, 99]
Using a loop, append all the integers i ← 98
from 98 down to 1. while i >= 1
[Link](i)
num ← num - 1

80
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:
Write down the code which will fulfil the requirements.
Then write down the final values in the list.

Requirement Code Final List Value

1. Initialise an empty list called pi_digits.


Append the element 3.
Append the element 1.
Append the element 4.

2. Initialise an empty list called scores.


Append the element 87.
Append the element 93.
Append the element 67..

3. Initialise an empty list called coords.


Append the element [-2, -4]
Append the element [0, 0]
Append the element [3, 4]

4. Initialise a list called ages as [20, 14]


Append the element 30.
Append the element 22.

5. Initialise a list called students as


[ [145, 25], [146, 20] ]
Append the element [147, 22]

6. Initialise an empty list called nums.


Using a loop, append all the integers from 100 to 150.

7. Initialise a list called countdown as [200, 198]


Using a loop append all the integers from 196 down to 0, counting down by 2.

81
Successful Pseudocoding
© Code for Schools, [Link]

7D - List Indices

Because lists now combine all the values inside it into just one variable (with one name), we
need a way to refer to individual elements inside the list.

Definition: An index refers to the position of the element in the list. In our pseudocode
convention, the first position has an index of 0.

Examples:
Consider the list and questions in the previous exercise

plots ← [4, 3, 1, 5, 0, 2]

1)​ What is the index of the first element?​ ​ ​ ​ 0


2)​ What is the index of the last element?​ ​ ​ ​ 5
3)​ What is the index of the 5th element?​ ​ ​ ​ 4
4)​ What is the index of the element with the value of 5?​ ​ 3

Exercises:

Question

1. scores = [78, 100, 65, 59, 98, 45]

1)​ What is the index of the first element?


2)​ What is the index of the last element?
3)​ What is the index of the 3rd element?
4)​ What is the index of 98?
5)​ What is the index of the highest score in the list?

2. exams_scores = [ 99.2, 97.5, 80.3, 75.4, 99.8, 56 ]

1)​ What is the index of the first element?


2)​ What is the index of the last element?
3)​ What is the index of the 2nd element?
4)​ What is the index of 75.4?
5)​ What is the index of the element with the lowest value?
6)​ What is the index of the element with the highest value?

82
Successful Pseudocoding
© Code for Schools, [Link]

7E - Indexing Lists

To read an element from a list, we need to refer to it using its index. The index is contained
between the [] brackets after the name of the list.

As with any variable, if we wish to read from it, it must be on the RHS of the assignment

operator ( ←).
Remember that the first element has the index of 0. This will trip you up, guaranteed! It even
trips up the most experienced of programmers sometimes!

Note that the [] is being used for a different purpose here than when it was used to initialise the
list (see above section).

volumes ← [8, 125, 64.8]


In the above code the square brackets are used to initialise the list. You can tell this is the case
because the brackets and the values inside it appear to the RHS of the assignment operator.

volumes [2]
In the above code the square brackets are used to index the list. You can tell this is the case
because the value inside the brackets is an integer and it does not appear to the RHS of the
assignment operator.

Examples:
Consider the following list:

scores ← [100, 94, 76, 45, 67, 82]


Write down the values of the following elements:
1)​ scores[1]​ 94
2)​ scores[5]​ 82
3)​ scores[0]​ 100​

Write down the expression to index the following elements in the list:
1)​ 100​ ​ scores[0]
2)​ 67​ ​ scores[4]
3)​ 76​ ​ scores[2]

83
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:

Question

1.
marks ← [78, 100, 65, 59, 98, 45]
Write down the values of the following elements:
1)​ marks[2]
2)​ marks[0]
3)​ marks[5]

2.
marks ← [78, 100, 65, 59, 98, 45]
Write down the expression to index the following elements in the list:
1)​ 100
2)​ 59
3)​ 78

3.
diameters ← [ 3.4, 0, 1.2, 2.1, 3.5, 0.33 ]
Write down the values of the following elements:
1)​ members [1]
2)​ members [5]
3)​ members [0]

4.
diameters ← [ 3.4, 0, 1.2, 2.1, 3.5, 0.33 ]
Write down the expression to index the following elements in the list:
1)​ 0.33
2)​ 0
3)​ 1.2

84
Successful Pseudocoding
© Code for Schools, [Link]

7F - Indexing Lists with Expressions

The index can also be an expression that evaluates to an integer.

Consider the following list:

points ← [2, 0, 3, 4, 1, 5]
x ←5
points[x]
Will also be the same as points[5]

y←6
x←2
points[y / x]
Will evaluate to points[3]

In terms of order of operations, the index operator [] is at the same level as the parentheses ().

Note that list elements can also be used as indices.

​ points ← [2, 0, 3, 4, 1, 5]
points[points[2]]

We evaluate the inner indexing operation first


points[points[2]]
points[3]
Then we evaluate the remaining index operation.
4

85
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:
Evaluate the index and write down the value of the element indexed

Question

1.
ids ← [2, 4, 1, 5, 3, 0]
n=2
ids[n]

2.
ids ← [2, 4, 1, 5, 3, 0]
n = 10
ids[n/2]

3.
ids ← [2, 4, 1, 5, 3, 0]
ids[ids[0]]

4.
ids ← [2, 4, 1, 5, 3, 0]
ids[ids[1] / 2]

5.
ids ← [2, 4, 1, 5, 3, 0]
attempts ← [3, 0, 1, 1, 1, 2]

id ← ids[5]
attempts [id]

6.
ids ← [2, 4, 1, 5, 3, 0]
attempts = [3, 0, 1, 1, 1, 2]
attempts [ids[3]]

86
Successful Pseudocoding
© Code for Schools, [Link]

7G - Looping Through Lists (Analysis)

We can use loops to inspect elements in a list by using the loop variable as the index into the
list.

Note: the print() function will output its argument on a display.


E.g. print(3.14159) will display 3.14159, and
print(1 + 3) will display 4.

Examples:
In the exercises below, walk through the code, fill in the trace table and attempt to determine
what the code is doing.

Note:
All questions below apply to the following list:

ages ← [17, 6, 21, 65, 6]


Code Trace Table Purpose

a. n←0 Displays all the


while n < 5 elements in the list in
Variable Value Output
print(ages[n]) order.
n←n+1 n 0, 1, 2, 3, 17
4, 5 6
21
ages[n] 17, 6, 21, 65
65, 6 6

b. n←0 Determines whether or


not the element 21 is
found ← False Variable Value Output
in the list.
while n < 5
if ages[n] == 21 n 0, 1, 2, 3, True
found ← True 4, 5
n←n+1 ages[n] 17, 6, 21,
print(found) 65, 6

False,
found True

87
Successful Pseudocoding
© Code for Schools, [Link]

Code Trace Table Purpose

c. i←0 Sums all the


elements in the
total ← 0 Variable Value Output
ages list and
while i < 5
n 0, 1, 2, 3, 115 displays the sum.
total ← total + ages[i]
4, 5
i←i+1
print(total) ages[n] 17, 6, 21,
65, 6

0, 17, 23,
total 44, 109,
115

d. i←0 Determines the


maximum value in
v ← ages[0] Variable Value Output
the list.
while i < 5
if ages[i] > v n 0, 1, 2, 3, 65
v ← ages[i] 4, 5
i←i+1 ages[n] 17, 6, 21,
print(v) 65, 6

17, 21, 65
v

e. n←0 Determines the


second highest
v1 ← ages[0] Variable Value Output
value in the list.
while n < 5
if ages[i] > v1: n 0, 1, 2, 3, 21
v1 ← ages[i] 4, 5, 0, 1,
2, 3, 4, 5
n←n+1
ages[n] 17, 6, 21,
n←1 65, 6, 17,
v2 ← ages[0] 6, 21, 65,
while n < 5 6
if ages[i] > v2 and ages[i] < v1
v2 ← ages[i] v1 17, 21,
65
n←n+1 v2
print(v2) 17, 21

88
Successful Pseudocoding
© Code for Schools, [Link]

Code Trace Table Purpose

f. a ← [] Creates a new list,


a, which contains
n←0 Variable Value Output
every 2nd element
while n < 5
a [17, 21, 6] in the ages list.
[Link](ages[n])
n←n+2
n 0, 1, 2, 3, 4, 5,
n←0 0, 1, 2, 3
while n < 3:
print(a[n]) ages[n] 17, 6, 21, 65, 6
n←n+1
17, 21, 6
a[n]

g. i←0 Detects whether


any element is
dup ← False Variable Value Output
duplicated in the list
while i < 5
j←i+1 i 0,1,2,3,4,5 True
while j < 5:
j 1, 2, 3, 4, 5, 2, 3,
if ages[i] == ages[j]
4, 5, 3, 4, 5, 4, 5,
dup ← True 5
j←j+1
i←i+1 dup False, True
print(dup)
ages[i] 17, 6, 21, 65, 6

ages[j] 6, 21, 65, 6, 21,


65, 6, 65, 6, 6

89
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:
In the exercises below, walk through the code, fill in the trace table and attempt to determine
what the code is doing.

Note:
All questions below apply to the following list:
ages ← [17, 6, 21, 65, 6]

Code Trace Table Purpose

1. n←4
while n >= 0:
print(ages[n])
n←n-1

2. n←0
found ← False
while n < 5:
if ages[n] == -32
found ← True
n←n+1
print(found)

3. i←0
a←0
while i < 5
a ← a+ ages[i]
i←i+1
a←a/5
print(a)

4. i←0
v ← ages[0]
while i < 5
if ages[i] < v
v ← ages[i]
i←i+1
print(v)

90
Successful Pseudocoding
© Code for Schools, [Link]

Code Trace Table Purpose

5. n←0
v1 ← ages[0]
while n < 5
if ages[i] < v1:
v1 ← ages[i]
n←n+1

n←0
v2 ← ages[0]
while n < 5:
if ages[i] < v2 and ages[i] > v1
v2 ← ages[i]
n←n+1

print(v2)

6. r ← []
n←4
while n >= 0
[Link](ages[n])
n←n-1

7. i←0
b ← False
while i < 5
j←i+1
while j < 5:
if ages[i] + ages[j] == 86
b ← True
j←j+1
i←i+1
print(b)

91
Successful Pseudocoding
© Code for Schools, [Link]

7H - Looping Through lists (Writing Code)

In the exercises below, write down the code to fulfill the requirements. You may wish to draw up
your own trace table if you wish to help.

Note:
All questions below apply to the following list:

scores ← [3, -3, 1, 5, 1, -3, 3]

Requirement Code

1. Display every 2nd element in the list

2. Determine whether or not an integer stored in the number variable exists in the list
(use a loop).

3. Create a new list, third, which contains only the first 3 elements.
Use a loop to do this.

4. Calculate the product of all the elements in the list.

5. Determine whether or not all the elements in the list are in ascending order (smallest to
largest).

6. Check whether the elements in the list is a palindrome - ie. the list is the same when
the elements are reversed
I.e. in this case, it is.

7. Determine whether there are any pair of numbers which sum to 0.

8. Find what is the largest product of any 2 numbers in the list.

92
Successful Pseudocoding
© Code for Schools, [Link]

7I - Updating lists (Analysis)

To change specific elements in a list, we can assign to an indexed element in the list

Remember this means that the indexed element must appear on the LHS of the assignment
operator (=).

marks[2] = 23

As with any variable, it can assigned to an expression.

marks[0] =10 + 3 + 4

temperatures = [24, 23, 20, 19, 34, 19]

temperatures[3] = temperatures[5]

_ = Read
_ = Write

temperatures[3] = temperatures[5]
temperatures[3] = 19

Examples:
In the exercises below, walk through the code, fill in the trace table and attempt to determine
what the code is doing.

Note:
All questions below apply to the following list:
IDs ← [4, 3, 1, 2]

Code Trace Table Purpose

a. n←0 Sets all the elements in the


while n < 4 list to 0.
Variable Value
IDs[n] ← 0
n←n+1 n 0, 1, 2, 3, 4

IDs [4,3,1,2]
[0,3,1,2]
[0,0,1,2]
[0,0,0,2]
[0,0,0,0]

93
Successful Pseudocoding
© Code for Schools, [Link]

Code Trace Table Purpose

b. n←0 Sets all the elements in the


while n < 3 list to the value of the last
Variable Value
element.
IDs[n] ← IDs[3]
n←n+1 n 0, 1, 2, 3, 4

IDs [4,3,1,2]
[2,3,1,2]
[2,2,1,2]
[2,2,2,2]

c. i←0 Reverses the order of


while i < 2 elements in the list.
Variable Value
j←3-i
temp ← IDs[i] i 0, 1, 2
IDs[i] ← IDs[j] j 3, 2
IDs[j] ← temp
i←i+1 temp 4, 3

IDs [4,3,1,2]
[2,3,1,4]
[2,1,3,4]

d. x←0 Sorts the list in ascending


while x < 3 element order.
Variable Value
y←x+1
while y < 4 x 0, 1, 2
if IDs[y] < IDs[x]:
temp ← IDs[x] y 1, 2, 3,
2, 3, 3
IDs[x] ← IDs[y]
IDs[y] ← temp temp 4,3,4,3,4
y←y+1
IDs [4,3,1,2]
x←x+1 [3,4,1,2]
[1,4,3,2]
[1,3,4,2]
[1,2,4,3]
[1,2,3,4]

94
Successful Pseudocoding
© Code for Schools, [Link]

Exercises:
In the exercises below, walk through the code, fill in the trace table and attempt to determine
what the code is doing.

Note:
All questions below apply to the following list:
IDs ← [4, 3, 1, 2]

Code Trace Table Purpose

1. i←3
while i >= 0
IDs[i] ← 1
i←i-1

2. n←0
while n < 4
IDs[n] ← - IDs[n]
n←n+1

3. i=0
temp ← IDs[0]
while i < :
IDs[i] ← IDs[i + 1]
i←i+1
IDs[3] ← temp

4. i←3
temp ← IDs[3]
while i > 0
IDs[i] ← IDs[i - 1]
i←i-1
IDs[0] ← temp

5. x←0
while x < 3
y←x+1
while y < 4
if IDs[y] > IDs[x]:
temp ← IDs[x]
IDs[x] ← IDs[y]
IDs[y] ← temp
y←y+1
x←x+1

95
Successful Pseudocoding
© Code for Schools, [Link]

Code Trace Table Purpose

6. x←0
swapped ← True
while swapped == True
x←0
swapped ← False
while x < 3
if IDs[x] > IDs[x + 1]:
temp ← IDs[x]
IDs[x] ← IDs[x + 1]
IDs[x + 1] ← temp
swapped ← True
x←x+1

96
Successful Pseudocoding
© Code for Schools, [Link]

7J - Updating lists (Writing Code)

In the exercises below, write down the code to fulfill the requirements. You may wish to draw up
your own trace table if you wish to help.

Note:
All questions below apply to the following list:

scores ← [3, -3, 1, 5, 1, -3, 3]

Requirement Code

1. Determine the highest valued element in the list.


Copy this value to all the elements.

2. Determine the median value of the list.

3. Extension: Update the list so that all the negative numbers occur before the positive
ones. There is no need to do a complete sort.

4. Extension:
Write code to sort a list of numbers consisting only of 0s, 1s, and 2s without using a
nested loop.

97
Successful Pseudocoding
© Code for Schools, [Link]

Appendix A - Pseudocode to Python


Below are some notes on how to convert the pseudocode conventions used this in text into
Python code:
●​ = instead of ←
●​ : at the end of IF and WHILE conditions
●​ ELIF instead of else if
●​ : at the end of ELIF and else
●​ Spaces between lines don’t matter
●​ * instead of ✕
●​ Must explicitly use * to multiply variables together.
○​ E.g. ab as a multiplication between variables, a and b, in mathematics will need
to be converted to a * b
○​ Same for multipliers before and after parentheses
●​ ** is the index (raised to the power of) operator, not ^
○​ E.g. xy needs to be converted to x ** y
●​ Variable name rules:
○​ Can’t start with a number
○​ Alphanumeric allowed
○​ No spaces
○​ Only special symbol allowed is underscore _

Other pseudocode conventions used elsewhere:


●​ No need for END WHILE, or END IF, or END FOR keywords to indicate that a block of
pseudocode is complete. In Python, unindenting indicates the end of the previous
indented block of code.
●​ No need for the THEN keyword after the IF condition
●​ = may be used for equality comparison operator, remember to convert to == for equality
comparison in Python

98
Successful Pseudocoding
© Code for Schools, [Link]

Solutions to Exercises
2A

Assignment Statement Valid?

1.
d ←0 True

2.
x ← 30.0 - 2 True

3. 0→x False
(the assignment operator is in the
wrong direction)

4. (20 - 8) / 3 → y False
(the assignment operator is in the
wrong direction)

5.
x + 2 ←4 False
(LHS can only contain a single variable)

6.
z + x ← (10 + 2) + 3
False
(LHS can only contain a single variable)

7.
g ← ((10 / 2) ✕ (3 + 5)) - 3.3
True

8. a / 3 False
(No assignment operator)

9. x + z + 3 False
(No assignment operator)

10.
perimeter ← 2 * 10 + 2 * 5 True

99
Successful Pseudocoding
© Code for Schools, [Link]

2B

Assignment Statement Final values of x and y

1.
y ← -3 x: -6
y: -3

x ← -6

2.
y ← 0.4 x: 0.2
y: 0.4

x ← y / 2

3.
y ← -3 + 2 x: 1
y: -1

x ← y + 2

2C

Pseudocode Trace-table

1.
a ← 10
Variable Value
b ← 20
a 10, 20
a ← b
b 20
b ← a

2.
x ← 10
Variable Value
y ← 20
x 10, 20
z ← x
y 20, 10
x ← y z 10
y ← z

100
Successful Pseudocoding
© Code for Schools, [Link]

3.
x ←8
Variable Value
y ← x - x / 2
x 8, 12
x ← y + x
y 4, 144
y ← x * x

4.
x ←1
Variable Value
y ← 1
x 1, 3
z ← x + y
y 1, 5
x ← y + z z 2, 8
y ← x + z

z ← x + y

2D

Pseudocode Trace-table

1.
a ← 10
Variable Value
a ← a / 2
a 10, 5

2.
x ←8
Variable Value
y ← (x + 4) / 3
x 8
y ← y * 3
y 4, 12, 8
y ← y - 4

101
Successful Pseudocoding
© Code for Schools, [Link]

3.
x ←4
Variable Value
y ← 8
x 4, 8
y ← x + y
y 8, 12, 4
x ← y - x

y ← y - x

2E

Goal Code

1. ●​ Assign the value -20 to the variable, a


●​ Halve the value of a and assign it to the a← -20
variable b
b ← a / 2

2. ●​ Assign the value 0.6 to the variable, d


●​ Calculate the area of a circle with the d← 0.6
diameter given by the value in the
variable d a ← 3.14159 * d * d / 4
●​ Assign the result to the variable, a

3. ●​
●​
Assign the value 2 to the variable l
Assign the value 3 to the variable w l ←2
●​ Assign the value 4 to the variable d
●​ Calculate the volume of a rectangle with w ← 3
a length given by l, a width given by w,
and a depth given by d d ← 4
●​ Assign the result to the variable v
v ← l * w * d

4. ●​ Assign the value 10 to the variable, x


●​ Double the value of x and assign it back x ← 10
to x
●​ Triple this value and assign it back to x x ← x * 2

x ← x * 3

5. ●​ Assign the value 180 to the variable, h


●​ Assign the value 80 to the variable, w h ← 180
●​ Calculate the BMI given with a height (in
cms) given by h, and a weight (in kgs) w ← 80
given by w
○​ BMI = kgs / m2 bmi ← w / ((h / 100) * (h / 100))
●​ Assign the answer to the variable, bmi

102
Successful Pseudocoding
© Code for Schools, [Link]

3A

Expression Evaluation

1. 5 == 2 False

2. 10 < 10 False

3. 2 != 2 False

4. 32 >= 32 True

5. (5 + 3) < 8 False

6. 7 + 2 * 2 == (11 - 0) True

7. True == False False

8. 4.1 >= 4.01 True

9. False False

10. not True False

11. not (1 <= 0) True

3B

Expression Evaluation

1. 3 <=3 and 4 <= 4 True

2. 8 > 0 or 1 < 0 True

3. 10 == (9 + 1) and 3 < 5 True

4. 1 > 0 and 6 > 2 True

5. 50 < 34 or 34 > 43 False

6 10 == 10 and 10 < 10 False

7. True and False False

8. False or True True

9. (10 > 5 and 4 > 1) or (50 >= 50) True

10. True or False True

103
Successful Pseudocoding
© Code for Schools, [Link]

11. True and True True

12. False or False False

3C

Pseudocode Trace-table

1.
age ← 18
Variable Value
result ← x < 18
age 18

result False

2.
date ← 31/01/2001
Variable Value
return_by ← 28/02/2001
date 31/01/2001
late ← date > return_by
return_by 28/02/2001

late False

3.
height ← 180
Variable Value
height ← height / 100
height 180, 1.80
six_feet ← height >= 1.83
six_feet False

4.
t ← 200.0
Variable Value
t ← (t - 32.0) * 5/9
t 200.0,
boiling ← t >= 100 93.33

boiling False

104
Successful Pseudocoding
© Code for Schools, [Link]

5.
hours ← 150
Variable Value
age ← 19
hours 150
test_passed ← True
age 19
licensed ← hours > 120 and age > 18 and test_passed True

test_passed licensed True

6.
distance ← 1500
Variable Value
distance ← distance / 1000
distance 1500, 1.5
fast_food ← True
fast_food True
rating ← 4 rating 4
order ← distance < 2.0 and rating >= 4 order False
and not fast_food

105
Successful Pseudocoding
© Code for Schools, [Link]

3D

Requirements Pseudocode

1. ●​ A student obtained a score of 16 out of


30 score← 16 / 30
●​ Write pseudocode to:
○​ Convert this score to a score← score * 100
percentage
○​ Assign a boolean value to the pass ← score >= 50
variable, pass, indicating
whether or not the percentage
was greater than or equal to 50

2. ●​ A rectangle has a length of 3, and a


width of 5 length ←7
●​ Write pseudocode to:
○​ Determine the area width ← 8
○​ Assign a boolean value to the
variable, is_large, indicating area ← length * width
whether or not the rectangle
has an area greater than 15 is_large ← area > 56

4A

Code Trace Table

1.
y ← -3
if y <= 0 Variable Value

y ←y*2 y -3, -6

2.
x ← 1
if x >= 1 Variable Value

x ←x+2 x 1, 3, -7

x ← x - 10

106
Successful Pseudocoding
© Code for Schools, [Link]

3.
x ←4
if x > 4 and x < 6 Variable Value

←x-1
x x 4, 2

x ← x / 2

4.
z ←10
if z > 0 and z < 50 Variable Value

←z/2
z z 10, 5, 3

z ← z - 2 * How to check if z is between 0


and 50 (exclusive). Notice how
the boolean condition is
repeated.

5.
t ← 21
Variable Value
fan_on ← True
t 21
if t >= 20 and fan_on == False

fan_on ← True fan_on True

6.
t ← 24
Variable Value
fan_on ← False
t 24
if t >= 20 and fan_on == False

fan_on ←
True fan_on False, True
if t <= 16 and fan_on == True

fan_on ← False
7.
y ←1
if y >= 0 Variable Value

y ←
-y y 1, -1, 10
if y <= 0

y ← y * -10

107
Successful Pseudocoding
© Code for Schools, [Link]

8.
x ←10
if x > 5 and x < 15 Variable Value

x ←
-x x -10, -10, 0, 3
if x <= 0

← x + 10
x

x ← x + 3

9.
day_hours ← 10
Variable Value
night_hours ← 2
day_hours 10, 11
is_night ← False
night_hours 2
if is_night and night_hours < 120

night_hours ←
night_hours + 1
is_night False

if not is_night and day_hours <


120

day_hours ← day_hours + 1
10.
x ←8
Variable Value
y ← 10
x 8
z ← 2
y 10
min ← x z 2
if y < x and y < z
min 8, 2
min y ←
if z < x and z < y

min ←z

108
Successful Pseudocoding
© Code for Schools, [Link]

11.
x ←8
Variable Value
y ← 10
x 8
z ← 2
y 10
max ← x z 2
if y > x and y > z
max 8, 10
max y←
if z > x and z > y

max ←z

4B

Requirements Pseudocode

1. ●​ Assign 58 to the variable, mark


●​ Assign False to the variable, credit mark ← 58
●​ If score is greater than or equal to 60,
then assign the value True to the credit ← False
variable credit if mark >= 60

credit ← True
2. ●​ Assign False to the variable, in_WA
●​ Assign 12 to the variable, time in_WA← True
●​ If in_WA then subtract 1 from the
variable time time ← 12
if in_WA

time ← time - 1

109
Successful Pseudocoding
© Code for Schools, [Link]

3. ●​
●​
Assign 18 to john_age
Assign 20 to jane_age john_age ← 18
●​ Assign 22 to jack_age
●​ Calculate the middle age and assign it jane_age ← 20
to the variable mid_age
jack_age ← 22

mid_age ← john_age
if jane_age >= jack_age and
jane_age <= john_age

mid_age ←jane_age
if jane_age >= john_age and
jane_age <= jack_age

mid_age ←jane_age
if jack_age >= jane_age
and jack_age <= john_age

mid_age ←jack_age
if jack_age >= john_age
and jack_age <= jane_age

mid_age ← jack_age
4C

Code Trace Table

1.
y← -2
if y > 0 Variable Value

y ←y+1 y -2, -4
else

y ←y-2
2.
z← 20
if z >= 30 Variable Value

z ← z - 20 z 20, 40, 10
else

z ← z + 20

110
Successful Pseudocoding
© Code for Schools, [Link]

z ←z/4
3.
x ←2
if x >= 2 Variable Value

x ←
x + 2 x 2, 4, 8
else if x < 0

x ←x-2
else

←4
x

x ← x * 2

4.
y ←0
if y > 10 Variable Value

y ←y+2 y 0, 4, 6, 3

y ← y + 4
else if y > 5

y ←y-2
y ← y + 4
else

←y+4
y

y ← y + 2

y ← y * 0.5

111
Successful Pseudocoding
© Code for Schools, [Link]

5.
a ← 16
Variable Value
b ← 20
a 16, 17
if a > 20 and b < 30

a ←b+2 b 20, 15

b ← a + 4
else if a > 10 and b < 30

a ←b-3
b ← a - 2
else if a > 0 and b < 50

a ←b+4
b ← a + 2
else

a ←b-4
b ← a - 2

6.
x ←2
if x >= 1 Variable Value

x ←
x - 1 x 2, 3
else if x >= 2

x ←
x + 1
else if x >= 3

x ←x-2
else

x ←0

112
Successful Pseudocoding
© Code for Schools, [Link]

7.
z ← 2
if z >= 2 Variable Value

z ←z + 1 z 2, 3, 2, 20
if z >= 3

z ←z - 1
if z >= 4

z ←z-2
else

z ← z * 10
4D

Requirements Pseudocode

1. ●​ Assign 59 to the variable, score


●​ If score is greater than or equal score ← 59
to 85, then assign the value 4 to
the variable GPA if score >= 85
●​ If score is between 70 inclusive
and 85 exclusive, then assign GPA ←4
the value 3 to the variable GPA else if score >= 70 and
●​ If score is between 60 inclusive score < 85
and 70 exclusive, then assign
the value 2 to the variable GPA GPA ←3
●​ If score is between 50 inclusive else if score >= 60 and
and 60 exclusive, then assign score < 70
the value 1 to the variable GPA
●​ If the score is below 50, assign GPA ←2
the value 0 to the variable GPA else if score >= 50 and
score < 60

GPA ←1
else

GPA ←0

113
Successful Pseudocoding
© Code for Schools, [Link]

2. ●​ Assign -3 to the variable, x


●​ Is x is greater than 2, assign x2 x ← -3
to the variable y
●​ If x is less than -2, assign -x2 to if x > 2
the variable y
●​ If x is between -2 inclusive and 2 y ←
x * x
inclusive, assign 0 to the else if x < -2
variable y
y ← -x * x
else

y ←0
3. ●​
●​
Assign 2 to the variable, x
Assign 6 to the variable, y x ←2
●​ Assign 3 to the variable, z
●​ Determine the which value is the y ← 6
product of the other 2 values
and assign that value into the z ← 3
variable, multiple
●​ If no values are a multiple of the multiple ←
-1
other 2, then assign -1 to the if x == y * z
variable, multiple
multiple x←
else if y == x * z

multiple y←
else if z == x * y

multiple ←z

114
Successful Pseudocoding
© Code for Schools, [Link]

4. ●​
●​
Assign 4 to the variable, w
Assign 2 to the variable, x w ←4
●​ Assign 6 to the variable, y
●​ Assign 3 to the variable, z x ← 2
●​ Determine the which is the
largest value and assign that y ← 6
value into the variable, max
z ← 3

max ← w
if x > w and x > y and x > z

max x←
else if y > w and y > x and y > z

max y←
else if z > w and z > x and z > y

max ←z

5A

Code Trace Table

1.
x← 3
while x > 0 Variable Value

x ←x-1 x 3, 2, 1, 0

2.
x← 0
while x <= 6 Variable Value

x ←x+2 x 0, 2, 4, 6, 8

115
Successful Pseudocoding
© Code for Schools, [Link]

3.
i ← 10
Variable Value
t ← 0
i 10, 11, 12, 13, 14,
while i <= 15
15, 16
t ←t+i t 10, 21, 33, 46, 60,
i ← i + 1
75

4.
a ←1
Variable Value
b ← 2
a 0, 1, 3, 8, 21, 55
while a <= 21

a ←a+b b 1, 2, 5, 13, 34, 89

b ← a + b

5.
x ←0
Variable Value
sum ← 0
x 0, 0.5, 1, 1.5, 2, 2.5
while x <= 2

y ← -x + 2x2 y 0, 0.75, 1, 0.75, 0

sum ← sum + y
sum 0, 0.75, 1.75, 2.5

x ← x + 0.5

6.
x ←0
Variable Value
min ← ∞
x 0, 0.5, 1, 1.5, 2, 2.5
while x <= 2

y ←x2 - 2x y 0, -0.75, -1, -0.75, 0


if y < min min ∞, -0.75, -1
min ←y
x ← x + 0.5

116
Successful Pseudocoding
© Code for Schools, [Link]

7.
x ←0
Variable Value
max ← -∞
x 0, 0.5, 1, 1.5, 2, 2.0
while x <= 2

y ←-x3 + 3x y 0, 1.375, 2, 1.125, -2


if y > max min -∞, 0, 1.375, 2
max ←y
x ← x + 0.5
8.
a ←2
Variable Value
b ← 1
a 2, 1, 3, 4
L ← 0
b 1, 3, 4, 7
n ← 1 L 0, 2, 1, 3, 4, 7
while n < 6
if n == 1 n 1, 2, 3, 4, 5, 6
L ←a
else if n == 2

L ←b
else

L ←a+b
a ← b

b ← L

n ← n + 1

(What is the name of this


sequence?)

117
Successful Pseudocoding
© Code for Schools, [Link]

5B

Requirements Pseudocode

1. ●​ Find the sum of all the even numbers


between 20 and 90 i ← 20
sum ← 0
while <= 80

sum← sum + i
i ← i + 2

2. ●​ Find the sum of the first 10 cube


numbers (ie. 1 + 8 + 27 + … + 1000) i ←1
sum ← 0
while i <= 10

sum← sum + i * i * i
i ← i + 1

3. ●​ Calculate 20! (20 factorial)


i ← 20
factorial 1←
while i >= 1

factorial ← factorial * i
i ←i-1
4. ●​ Find the minimum value of
y = sin x + cos x for x ∈ [0, 4], x ∈ Z x ←0
min ← ∞
while x <= 4

y ←sin x + cos x
if y < min

min ←y
x ←x+1

118
Successful Pseudocoding
© Code for Schools, [Link]

5. ●​ Find the sum of y values for


y = sin x + cos x for x ∈ [0, 4], x ∈ Z x ←0
sum ← 0
while x <= 4

y ← sin x + cos x
sum ← sum + y

x ← x + 1

6 ●​ The tribonacci numbers are like the


Fibonacci numbers, but instead of a ←0
starting with two predetermined terms,
the sequence starts with three b ← 0
predetermined terms and each term
afterwards is the sum of the preceding c ← 1
three terms.
●​ The first few tribonacci numbers are: t ← 0
0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81
●​ Find the 20th tribonacci number n ← 1
while n <= 20
if n == 1

t a←
else if n == 2

t b←
else if n == 3

t ←c
else

←a+b+c
t

a ← b

b ← c

c ← t

n ← n + 1

119
Successful Pseudocoding
© Code for Schools, [Link]

5C
Exercises:

Requirements Pseudocode

1. loge2 =
n ←1
sum ← 0

sign ← 1
●​ Calculate ln 2 using the above
series up to 200 terms
denom ← 1
while n <= 200

← sign * 1 / denom
term

sum ← sum + term

sign ← -sign

denom ← denom + 1

n ← n + 1

ln2 ← sum

120
Successful Pseudocoding
© Code for Schools, [Link]

2.
●​ Calculate
3
4
using the a ← -2
b ← -1
bisection method

c ← (a + b) / 2

f_c ← c - 43

accuracy ← |f_c|

answer ← a
while accuracy > 0.01

f_a ←a 3
- 4

f_b ← b 3
- 4
if f_a * f_c < 0

b ←c
else

a←c
c ← (a + b) / 2

f_c ← c - 4 3

accuracy ← |f_c|

answer ← c

121
Successful Pseudocoding
© Code for Schools, [Link]

3. ●​ Using the Trapezoidal rule for


integration,​ a ←2

calculate the area under the b ← 5
curve ​
y = 9 - 0.1x2 ​ n ← 6

between x = 2 and x = 5 using step ← (b - a) / n


6 trapeziums
sum ← 0

x ← a
while x < b

f_x ← 9 - 0.1x 2

x1 ← x + step

f_x1 ← 9 - 0.1x1 2

sum ← sum+(f_x+f_x1)/2*step

x ← x1
—--------------- OR —-------------
a ←2
b ← 5
f_a ← 9 - 0.1a 2

f_b ← 9 - 0.1b 2

n ← 6
step ← (b - a) / n
sum ← 0

x ← a + step
while x < b

← 9 - 0.1x
f_x 2

sum ← sum + 2 * f_x

x ← x + step

sum ← (sum + f_a + f_b) * step/2

122
Successful Pseudocoding
© Code for Schools, [Link]

6A

Code Trace Table

1. def sub(x, y)

diff ← x - y Variable Value


return diff
x 100

z ← sub(100, 90) y 90

diff 10

z 10

2. def sub(x, y)
return x - y
Variable Value

z ← sub(10, 9) x 10

y 9

z 1

3. def double(x)

db ← x + x Variable Value
return db
x 9

z ← double(9) db 18

z 18

4. def halve(x)
return x / 2
Variable Value

num← 10 num 10

z ← halve(num) x 10

z 5

123
Successful Pseudocoding
© Code for Schools, [Link]

5. def energy(m)
return m * 3000000002
Variable Value
mass ←5 mass 5
z ← energy(mass) m 5

z 4.5 x 1017

6. def area(l, w)

a ← l * w Variable Value
return a
x 3

x ←3 y 4

y ← 4
l 3

z ← area(x, y)
w 4

a 12

z 12

7. def perimeter(l, w)
return 2 * (l + w)
Variable Value

x ←3 x 3

y ← 4 y 4

z ← perimeter(x, y)
l 3

w 4

z 14

124
Successful Pseudocoding
© Code for Schools, [Link]

8. def min(x, y)

result ←x Variable Value


if y < x
x 10
result ←y
y 5
return result
result 10, 5
z ← min(10, 5) z 5

6B
Requirements Pseudocode

1. Write a function: def circumference(r)


●​ named: circumference return 3.1416 * r * r
●​ with a parameter: r
●​ which returns the circumference of a
c ← circumference(2)
circle with radius, r
●​ You may use the value of 3.1416 as
an approximation for PI

Then:
●​ call the function
●​ supply it an argument of 2,
●​ and assign the returned value to the
variable c

2. Write a function: def divide(x, y)


●​ named: divide return x / y
●​ with 2 parameters: x, y
●​ which returns the quotient of x and y
quotient ← divide(5, -2)
Then:
●​ call the function
●​ supply it arguments of 5 and -2
●​ and assign the returned value to the
variable quotient

125
Successful Pseudocoding
© Code for Schools, [Link]

3. Write a function: def average(a, b, c)


●​ named: average return (a + b + c) / 3
●​ with 3 parameters: a, b, c
●​ which returns the average of a, b and d ← average(1, 2, 3)
c

Then:
●​ call the function
●​ supply it arguments of 1, 2, and 3
●​ and assign the returned value to the
variable d

4. Write a function: def canDrive(age)


●​ named: canDrive if age >= 18
●​ with 1 parameters: age return True
●​ which returns True if age is 18 or else
above, False otherwise return False

Then:
d ← canDrive(17)
●​ call the function
●​ supply it arguments of 17 -----Alternative-----
●​ and assign the returned value to the
variable d def canDrive(age)

result← True
if x < 8

result ←False
return result

d ← canDrive(17)
5. Write a function: def max(x, y, z)
●​ named: max if x >= y and x >= z
●​ with 2 parameters: x, y, z return x
●​ which returns the largest of the values else if y >= x and y >= z
in the parameters return y
else
Then: return z
●​ call the function
●​ supply it arguments of -3, -4, 0
biggest ← max(-3, -4, 0)
●​ and assign the returned value to the
variable biggest

126
Successful Pseudocoding
© Code for Schools, [Link]

7A
1.
prices ← []
2.
areas ← [25, 30, 4]
3.
scores ← [150, 0, 100, 200]
4.
temperatures ← [15.3, 42.0, 32.1]
5.
registered ← [False, True, True, False]
6.
invoices ← [ [100.0, 16.5], [100.0, 16.5] ]
7.
marks ← [ [100.0, 99.2, 95.7], [76.2, 70.5, 78.0], [85.0, 65.4, 72.2] ]
7B
1. 1)​ ages
2)​ 16
3)​ 11
4)​ 65
5)​ 16

2. 1)​ squares
2)​ [2, 4]
3)​ [3, 9]
4)​ 16
5)​ 2
6)​ 16

127
Successful Pseudocoding
© Code for Schools, [Link]

7C
1.
pi_digits ← []
[Link](3)
[Link](1)
[Link](4)

2.
scores ← []
[Link](87)
[Link](93)
[Link](67)

3.
coords ← []
[Link]([-2, -4])
[Link]([0, 0])
[Link]([3, 4])

4.
ages ← [20, 14]
[Link](30)
[Link](22)

5.
students ←[ [145, 25], [146, 20] ]
[Link]([147, 22])

6.
nums ← []
i ← 100
while i <= 150
[Link](i)
i ←i+1
7.
countdown ← [200, 198]
n ← 196
while n >= 0
[Link](n)
n ←n-2

128
Successful Pseudocoding
© Code for Schools, [Link]

7D
1. 1)​ 0
2)​ 5
3)​ 2
4)​ 4
5)​ 1

2. 1)​ 0
2)​ 5
3)​ 1
4)​ 3
5)​ 5
6)​ 4

7E
1. 1)​ 65
2)​ 78
3)​ 45

2. 1)​ marks[1]
2)​ marks[3]
3)​ marks[0]

3. 1)​ 0
2)​ 0.33
3)​ 3.4

4. 1)​ members[5]
2)​ members[0]
3)​ members[2]

7F
1. 1

2. 0

3. 1

4. 1

5. 3

7. 2

129
Successful Pseudocoding
© Code for Schools, [Link]

7G
1.
Variable Value Output

n 4, 3, 2, 1, 0, -1 6, 65, 21, 6, 17

ages[n] 6, 65, 21, 6, 17

Displays all the elements in the list in reverse order.

2.
Variable Value Output

n 0, 1, 2, 3, 4, 5 False

ages[n] 17, 6, 21, 65, 6

found False

Searches for the element -32.

3.
Variable Value Output

i 0, 1, 2, 3, 4, 5 23

ages[i] 17, 6, 21, 65, 6

a 17, 23, 44,


109, 115, 23

Calculates the average of all the elements in the list.

4.
Variable Value Output

i 0, 1, 2, 3, 4, 5 6

ages[i] 17, 6, 21, 65, 6

v 17, 6

Determines the lowest element in the list.

130
Successful Pseudocoding
© Code for Schools, [Link]

5.
Variable Value Output

n 0, 1, 2, 3, 4, 5, 17
0, 1, 2, 3, 4, 5

ages[i] 17, 6, 21, 65, 6,


17, 6, 21, 65, 6

v1 17, 6

v2 17

Determines the 2nd lowest element in the list.

6.
Variable Value Output

n 4, 3, 2, 1, 0, -1

ages[i] 6, 65, 21, 6, 17

r [6]
[6, 65]
[6, 65, 21]
[6, 65, 21, 6]
[6, 65, 21, 6, 17]

Creates a new list, r, which contains the elements in ages in reversed order.

131
Successful Pseudocoding
© Code for Schools, [Link]

7.
Variable Value Output

i 0, 1, 2, 3, 4 True

j 1, 2, 3, 4, 5,
2, 3, 4, 5,
3, 4, 5,
4, 5,
5

ages[i] 17, 6, 21, 65

ages[j] 6, 21, 65, 6,


21, 65, 6,
65, 6,
6

b False, True

Determines whether any pair of elements sum to 86.

7H

1. n←0
while n < 7
print(scores[n])
n←n+2

2. i←0
found ← False
while i < 7
if number == scores[i]
found ← True
i←i+1
print(found)

3. third ← []
n←0
while n < 3
[Link](scores[n])
n←n+1

132
Successful Pseudocoding
© Code for Schools, [Link]

4. i←0
product ← 1
while i < 7
product ← product * scores[i]
i←i+1
print(product)

5. ascending ← True
n←0
while n < 6
if scores[n + 1] < scores[n]
ascending ← False
n←n+1
print(ascending)

6. palindrome ← True
n←0
while n < 3
if scores[n] != scores[6 - n]
palindrome ← False
n←n+1
print(palindrome)

7. zero_sum ← False
x←0
while x < 6
y←x+1
while y < 7
if scores[x] + scores[y] == 0
zero_sum ← True
y←y+1
x←x+1
print(zero_sum)

133
Successful Pseudocoding
© Code for Schools, [Link]

8. max_product ← scores[0] * scores[1]


x←0
while x < 6:
y←x+1
while y < 7
if scores[x] * scores[y] > max_product
max_product ← scores[x] * scores[y]
y←y+1
x←x+1
print(max_product)

134
Successful Pseudocoding
© Code for Schools, [Link]

7I
1.
Variable Value

i 3, 2, 1, 0, -1

IDs [4,3,1,2]
[4,3,1,1]
[4,3,1,1]
[4,1,1,1]
[1,1,1,1]

Overwrites all the list elements with the value 1, from the highest index down to the first
index.

2.
Variable Value

n 0, 1, 2, 3, 4

IDs [4,3,1,2]
[-4,3,1,2]
[-4,-3,1,2]
[-4,-3,-1,2]
[-4,-3,-1,-2]

Negates all the integers in the list.

3.
Variable Value

i 0, 1, 2, 3

temp 4

IDs [4,3,1,2]
[3,3,1,2]
[3,1,1,2]
[3,1,2,2]
[3,1,2,4]

“Rotates” all the elements in the list one position to the left.

135
Successful Pseudocoding
© Code for Schools, [Link]

4.
Variable Value

i 3, 2, 1, 0

temp 2

IDs [4,3,1,2]
[4,3,1,1]
[4,3,3,1]
[4,4,3,1]
[2,4,3,1]

“Rotates” all the elements in the list one position to the right.

5.
Variable Value

x 0, 1, 2, 3

y 1, 2, 3, 4,
2, 3, 4,
3, 4,
4

temp 1

IDs [4,3,1,2]
[4,3,2,1]

Sorts the list in descending order.


(This sorting algorithm is known as a selection sort).

136
Successful Pseudocoding
© Code for Schools, [Link]

6.
Variable Value

x 0, 1, 2, 3, 4,
0, 1, 2, 3, 4,
0, 1, 2, 3, 4

swapped True, False, True,


False, True, False

temp 4,3

IDs [4,3,1,2]
[3,4,1,2]
[3,1,4,2]
[3,1,2,4]
[1,3,2,4]
[1,2,3,4]

Sorts the list in ascending order.


(This sorting algorithm is known as a bubble sort).

137
Successful Pseudocoding
© Code for Schools, [Link]

7J

1. #find the max value first

max_value ← scores[0]
i←1
while i < 7
if scores[i] > max_value
max_value ← scores[i]
i←i+1

# then copy it to the rest

i←0
while i < 7
scores[i] ← max_value:
i←i+1

2. # sort the list first

x←0
while x < 6
y←x+1
while y < 7
if scores[y] > scores[x]
temp ← scores[x]
scores[x] ← scores[y]
scores[y] ← temp
y←y+1
x←x+1

# the median is just the middle value

print(scores[3])

138

You might also like