Computer SC Paper 1
Computer SC Paper 1
Paper 1
Computational Thinking & Programming (Python)
Definitions
What is meant by:
• ‘Algorithm’
• ‘Abstraction’
• ‘Decomposition’
ICON
Linear Search
Find the number ‘45’ in the list below:
46, 59, 37, 71, 24, 36, 97, 90, 16, 33, 45, 55
Method:
Binary Search
16 Find ‘45’ in the list using Binary
24 Search. List the comparisons
33
done to find it.
36
37
45
46
55
59
71
90
97
Comparing Searching
Algorithms
Linear Search Binary Search
ICON
Bubble Sort
Use bubble sort to sort the list below:
31, 10, 25, 35, 39, 34, 33, 32
Merge Sort
31, 10, 25, 35, 39, 34, 33, 32 Use Merge Sort to sort the list.
Comparing Sorting
Algorithms
Bubble Sort Merge Sort
Data Types
When we use ‘data’ in algorithms it can be a / an:
• Integer
• Float / Real
• Strings
• Characters
• Booleans
ICON
Operations
Basic: Addition, Subtraction, Multiplication, Division
Constants
To store data, we often store it in a variable by declaring it and assigning
the value.
Example:
Outputs
Many algorithms will require a ‘user input’, as well as an ‘output’, like a
message.
Python functions:
• User inputs: input( )
• Output: print( )
Example:
Data structure: ICON
Lists
Variables can also store lists, like this:
example = [4, “eight”, 15.0, True, “t”, 42]
What do you notice, and why can lists be useful?
strings
• Concatenation
• Indexing
• Substrings
ICON
Conditions
Boolean Logic (link to Logic Gates in Paper 2):
• NOT
• AND
• OR
ICON
Selection
How to write an ‘if’, ‘elif’ and ‘else’ statement:
if (statement):
instructions
elif (statement):
instructions
else:
instructions
Iteration
Definite iteration: For loops
First task: create an algorithm that generates the ‘Fibbonaci Sequence’. To get the next
term, the previous two terms are added together (the first 2 terms are 1, and 1)
Your algorithm should produce the sequence 1, 1, 2, 3, 5, 8, 13, 21…
Try and use a ‘for loop’. These are written like: for i in range (0,6):
ICON
Iteration
Indefinite iteration – while loops
Look at the code below:
while True:
print(“message”)
ICON
Subroutines
Is there something you do everyday, or frequently?
Is there something that ‘triggers’ it, like a reminder?
Procedures
To make a procedure:
def cakeInstructions():
print (“Sift flour”)
print (“Add detergent”)
To use the procedure, call it:
Using parameters
Think of the procedure again, or your task.
Could the task change slightly?
Could it be done multiple times, or for others?
Functions
This is another type of subroutine, but is different to a
procedure, as it has:
An input, and an output (‘returns’ something)
Python already has some of its own functions:
Example: ‘len’
len (string)
Why use ICON
subroutines?
The ‘structured’ approach
Remember at the start, ‘decomposition’?
Errors
If you code on a computer, you will often run the program to see
if it works, or to check for errors.
Three types of errors can come up:
• Syntax
• Logic
• Runtime
ICON
Syntax Errors
mul = input()
double = mul * 2
Print (str(double)
Runtime Errors
This is where the syntax of the program works, but it still won’t run – it will crash / stop working at
a specific line.
Examples:
1 – Something that doesn’t exist
Lst = [2,5,8,4]
print(Lst[6])
Logic Errors
Look at the program below:
The program fully works, without any syntax or runtime errors, but not as
intended.
This is a logic error.
ICON
Avoiding errors
How to avoid each error:
• Syntax – read through the program again, whether you have typed it
(colour should appear on functions) or written on paper in the exam
hcf = count
count = count + 1
print(str(hcf))
Practice Questions
2) Programming Practice:
Write a Python program that simulates a children’s game called ‘Fizz Buzz’.
It involves counting up in whole numbers, starting at 1 (i.e. 1, 2, 3, 4…).
The program should:
• Keep asking the user to enter an integer, until it is in between 70 and 200
(inclusive)
• Output the numbers from 1 to what the user asked for
• But if the number is divisible by 3 (divides with no remainder), it should
output the string ‘Fizz’ instead of the number
• Or if the number is divisible by 5, it should output ‘Buzz’
• Or if the number is divisible by both 3 and 5, it should output ‘FizzBuzz’
[10 marks]
Solution
ICON
Programming
Secret(s) – my solution