0% found this document useful (0 votes)
31 views41 pages

Computer SC Paper 1

Here are the answers: a) num1 count hcf 4 1 1 2 2 3 2 4 4 b) i) Line 7 - the if statement is selecting which code to run ii) Line 5 - the while loop is iterating until the condition is met

Uploaded by

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

Computer SC Paper 1

Here are the answers: a) num1 count hcf 4 1 1 2 2 3 2 4 4 b) i) Line 7 - the if statement is selecting which code to run ii) Line 5 - the while loop is iterating until the condition is met

Uploaded by

Matty P
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Computer Science

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

‘How’ did you find it?

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

Other: Integer Division, Modulus

Comparing: Equal to, Not equal to, Greater, Less…


Variables & ICON

Constants
To store data, we often store it in a variable by declaring it and assigning
the value.
Example:

Variables can be:


• Used later
• Changed

Constants are also like variables, but they CANNOT be changed.


Why use constants?
Inputs and ICON

Outputs
Many algorithms will require a ‘user input’, as well as an ‘output’, like a
message.
Python functions:
• User inputs: input( )

• Output: print( )

Prompts: Ask the user to input, and store in a variable

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?

Indexing – for Python, it starts at 0


print (example [1])
…will output ‘eight’
Working with ICON

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

(Example on the right)


ICON

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?

How would we do this with machines?


Remember – machines do what we ‘literally’ tell them to.
ICON

Procedures
To make a procedure:
def cakeInstructions():
print (“Sift flour”)
print (“Add detergent”)
To use the procedure, call it:

This is a procedure, as it doesn’t return anything based off an


output.
ICON

Using parameters
Think of the procedure again, or your task.
Could the task change slightly?
Could it be done multiple times, or for others?

We can use parameters to take into account for this:

def cakeInstructions (a):


flour = 100 * a
print (“Sift ”+str(flour)+“ g of flour”)
ICON

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’?

Other advantages with the structured approach:


• Code is easier to debug / correct / change
• Code is easier to maintain

Using Local variables


Programming ICON

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)

Why won’t this program work?


ICON

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])

2 – Incorrect data type


Const = input ( )
Double = Const * 2
print (str(Double))

What happens if the user inputs a string, like “yes”?


ICON

Logic Errors
Look at the program below:

num1 = input(“Enter a number:”)


num2 = input(“Enter another number:”)
sum = num1 + num2
print(“The sum of’’ + num1 + “and” + num2 + “is” + sum)

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

• Runtime – run through the program with trace tables.


Consider using ‘validation’

• Logic – run through the program with trace tables

For all errors, consider testing with different inputs.


Validation
This involves checking if an input is fine valid = False
to use in a program.
This prevents logic and runtime errors. while valid == False:
check = int(input())
Example 1: if check > 15:
A user will input an integer.
Create an algorithm that will check if it
valid = True
is greater than 15. else:
valid = False
If it isn’t make sure the user will keep
inputting new integers until this
condition is met.
Validation – for data types
This is another ‘layer’ for valid = False
validation. while valid == False:
try:
In the previous example, the
print(“Enter an integer”)
program will crash if the user
check = int(input())
enters a string, causing a
valid = True
‘ValueError’.
except ValueError:
valid = False
Example 2: Create an algorithm print(“Valid!”)
that keeps asking a user for an
input, until an integer is entered. (You may not need to do this, as you can
‘assume’ the correct data type is entered)
Validation task
Short task:
Ask the user repeatedly to
enter a number / integer,
until it is between 10 and
50 (inclusive).

You may assume an


integer is being entered.
[4 marks]
ICON

Testing for errors


Here is some code from earlier:
valid = False
while valid == False:
check = int(input())
if check > 15:
valid = True
else:
valid = False
How can we test this program?
Testing data:
• Normal
• Boundary
• Erroneous
Trace Tables
Here is some code: a b c
def TotalOut(a, b):
c=a+b
while a < c:
a=a+1
b=b-a
return str(b)

Complete the trace table (to the right) when


the subroutine call TotalOut(3, 4) is
made.
Practice Questions
1) Here is some code: The code tries to find the
highest common factor of both
numbers.
num1 = int(input()) This is the highest number that
num2 = int(input()) both numbers can be divided by
hcf = 1 without leaving a remainder.
count = 1 The program works when 4 and
while count < num1: 6 are inputted, returning 2.
if (num1 % count == 0) and (num2 % count == 0): However the program doesn’t
hcf = count work when 4 and 4 are inputted,
returning 2, rather than 4.
count = count + 1
print(str(hcf))
Practice Questions
num1 = int(input()) a) Complete the trace table below
num2 = int(input()) to show how the program
works when 4 is inputted for
hcf = 1 both ‘num1’ and ‘num2’ [3]
count = 1 num1 count hcf
while count < num1:
if (num1 % count == 0) and (num2 % count == 0):
hcf = count
count = count + 1
print(str(hcf))
Practice Questions
num1 = int(input()) b) i) State the line number where
selection is used [1]
num2 = int(input())
hcf = 1
count = 1
ii) State the line number where
while count < num1:
casting is first used [1]
if (num1 % count == 0) and (num2 % count == 0):
hcf = count
count = count + 1
iii) What type of iteration is used? [1]
print(str(hcf))
Practice Questions
num1 = int(input()) c) i) State the line number that causes
the problem described (when 4 and 4
num2 = int(input()) are inputted, 2 is returned, while 4 was
hcf = 1 expected) [1]
count = 1
while count < num1:
if (num1 % count == 0) and (num2 % count == 0): ii) Rewrite the line of code you stated
in part c) i) to solve the problem [1]
hcf = count
count = count + 1
print(str(hcf))
Practice Questions
num1 = int(input()) d) The code is used to define, or create
a subroutine named ‘hcf’.
num2 = int(input())
i) Why would ‘num1’ and ‘num2’ be
hcf = 1 parameters? [1]
count = 1
while count < num1: ii) Explain why it is an advantage to use
if (num1 % count == 0) and (num2 % count == 0): subroutines when coding. [3]

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy