You are on page 1of 22

SLR 2.

1 Algorithms | Abstraction OCR GCSE (J277)

Decomposi
tion
Abstractio
n

Algorithmi
c thinking

Starter: What is Decomposition?


SLR 2.1 Algorithms | Decomposition OCR GCSE (J277)

Decomposition
Decomposition means breaking a
complex problem down into smaller,
more manageable parts.
Dealing with many different stages
of a problem at once is much more
difficult than breaking it down into a
number of smaller problems and
solving them one at time.
Learning objectives (L1-4)
● Understand what the three basic programming constructs are and how they
are used to control the flow of a program.

● Identify and correct common errors in computer programs.

● Be able to use inputs, outputs, arithmetic and string handling.

● Apply knowledge of computational thinking to solve complex problems.


Shopping list

1 shopping = ["Pasta", "Tomatoes", This is a shopping list.


2 "Onions", "Basil",
3 “Cheese"] Question .
4 print("Buy:") What do you think will be displayed on the screen
5 for item in shopping: when this program is executed?
6 print(item)
Buy:
Pasta
Tomatoes
Onions
Basil
Cheese
for loops: Syntax and use

for variable
in : list for loops are convenient for iterating over
block of
statements
the items of a list

Note for loops can be used for iterating over any


sequence of elements, e.g. strings.
Iterating over a list of dice rolls

1 rolls = [1, 4, 3, 6] This is a list of dice rolls.


2 for dice in rolls:
3 Question .
print(dice)
What do you think will be displayed on the screen
when this program is executed?

1
4
3
6
Iterating over a list of dice rolls

1 rolls = [1, 4, 3, 6] This is a list of dice rolls.


2 count = 0
3 Question .
for dice in rolls:
4 count = count + 1 What do you think will be displayed on the screen
5
when this program is executed?
print(count)
4
The count variable is initialised to 0 and is
incremented by 1 for each item in the list.
Iterating over a list of dice rolls

1 rolls = [1, 4, 3, 6] This is a list of dice rolls.


2 count = 0
3 Question .
for dice in rolls:
4 if dice > 3: What do you think will be displayed on the screen
5 count = count + 1 when this program is executed?
6 print(count) 2
The count variable is initialised to 0 and is
incremented by 1 for each item in the list that is
greater than 3.
Iterating over a list of dice rolls

1 rolls = [1, 4, 3, 6] This is a list of dice rolls.


2 selection = []
3 Question .
for dice in rolls:
4 if dice > 3: What do you think will be displayed on the screen
5 selection.append(dice) when this program is executed?
6 print(selection) [4, 6]
selection is initialised to an empty list [].
The value of each dice roll in rolls that is greater than
3 is appended to selection.
Iterating over a list of dice rolls: Walkthrough

1 rolls = [1, 4, 3, 6] State .


2 selection = [] rolls
3 for dice in rolls:
4 if dice > 3:
0 1
5 selection.append(dice) 1 4
6 print(selection) 2 3
3 6

selection
Iterating over a list of dice rolls: Walkthrough

1 rolls = [1, 4, 3, 6] State .


2 selection = [] rolls dice
3 for dice in rolls:
4 if dice > 3: False
0 1 1
5 selection.append(dice) 1 4
6 print(selection) 2 3
3 6

selection
Iterating over a list of dice rolls: Walkthrough

1 rolls = [1, 4, 3, 6] State .


2 selection = [] rolls dice
3 for dice in rolls:
4 if dice > 3: True
0 1 4
5 selection.append(dice) 1 4
6 print(selection) 2 3
3 6

selection
Iterating over a list of dice rolls: Walkthrough

1 rolls = [1, 4, 3, 6] State .


2 selection = [] rolls dice
3 for dice in rolls:
4 if dice > 3:
0 1 4
5 selection.append(dice) 1 4
6 print(selection) 2 3
3 6

selection
0 4
Iterating over a list of dice rolls: Walkthrough

1 rolls = [1, 4, 3, 6] State .


2 selection = [] rolls dice
3 for dice in rolls:
4 if dice > 3: False
0 1 3
5 selection.append(dice) 1 4
6 print(selection) 2 3
3 6

selection
0 4
Iterating over a list of dice rolls: Walkthrough

1 rolls = [1, 4, 3, 6] State .


2 selection = [] rolls dice
3 for dice in rolls:
4 if dice > 3: True
0 1 6
5 selection.append(dice) 1 4
6 print(selection) 2 3
3 6

selection
0 4
Iterating over a list of dice rolls: Walkthrough

1 rolls = [1, 4, 3, 6] State .


2 selection = [] rolls dice
3 for dice in rolls:
4 if dice > 3:
0 1 6
5 selection.append(dice) 1 4
6 print(selection) 2 3
3 6

selection
0 4
1 6
Iterating over a list of dice rolls: Walkthrough

1 rolls = [1, 4, 3, 6] State .


2 selection = [] rolls
3 for dice in rolls:
4 if dice > 3:
0 1
5 selection.append(dice) 1 4
6 print(selection) 2 3
3 6

selection
0 4
1 6
Task:
You need to complete:
• For Loops Worksheet 1
• Save it to your year 9 computing folder.
Answers

1. Numbers = []
2. for i in range(1000):
3. Numbers.append(i)
4. check = len(Numbers)
5. print("There are", check, "Numbers in your list")

6. Choice = int(input("What number do you want to check for"))


7. if Choice in Numbers:
8. print("True")
9. else:
10. print("False")
Answers

1. ListSize = int(input("What size list would you like?"))

2. Numbers = []
3. for i in range(ListSize):
4. Numbers.append(i)
5. check = len(Numbers)
6. print("There are", check, "Numbers in your list")

7. Choice = int(input("What number do you want to check for"))


8. if Choice in Numbers:
9. print(Choice, “has been found in your list”)
10. else:
11. print(Choice, “has not been found in your list”)
Plenary

What’s this for?

1 word = "lipogram" Question .


2 for item in word: What do you think will be displayed on the screen
3 print(item) when this program is executed?

l Note . You can use for to iterate over


i anything that has individual elements.
p In this case, for iterates over the characters
o of a string.
g
r
a
m
Plenary

What’s this for?

1 syllables = ["li", "po", "gram"] Explorer task .


2 for syllable in syllables: What do you think will be displayed on the screen
3 for letter in syllable: when this program is executed?
4 print(letter)
l
i
p
o
g
r
a
m

You might also like