Evaluating Programs (Edexcel GCSE Computer Science)

Revision Note

Robert Hampton

Written by: Robert Hampton

Reviewed by: James Woodhouse

Fitness for Purpose & Efficiency

  • In the exam students should be able to demonstrate logical reasoning and use test data to evaluate a programs fitness for purpose & efficiency, this includes:

    • Number of compares

    • Number of passes through a loop

    • Use of memory

Number of compares

Python code

def find_largest_number(numbers):

"""

Finds the largest number in a list

Inputs:

numbers: A list of numbers

Returns:

The largest number in the list

"""

# Initialize largest number to the first element of the list

largest = numbers[0]

# Iterate through the list, comparing each element with the current largest

for number in numbers:

if number > largest:

largest = number

return largest

# ------------------------------------------------------------------------

# Main program

# ------------------------------------------------------------------------

numbers = [10, 2, 5, 8, 15, 1]

largest_number = find_largest_number(numbers)

print(f"The largest number is: {largest_number}")

  • This program uses a linear search algorithm to find the largest number

  • It iterates through each item in the list, comparing each element with the current largest value

  • For every comparison after the largest number is found, the comparison is unnecessary, making this method inefficient

  • As the number of elements grow, this method becomes increasingly more inefficient

  • This could be solved in Python by using a built in function such as max()which is optimised for finding the largest number

Number of passes through a loop

Python code

def find_target(data, target):

"""

This function finds the target element in a list using a nested loop

Inputs:

data: A list of elements.

target: The element to search for.

Returns:

The index of the target element in the list, or -1 if not found.

"""

for i in range(len(data)): # Loop through each element (outer loop)

for j in range(len(data)): # Loop through each element again (inner loop)

if data[j] == target:

return j

return -1

# ------------------------------------------------------------------------

# Main program

# ------------------------------------------------------------------------

data = [5, 2, 8, 1, 3]

target = 3

index = find_target(data, target)

if index != -1:

print(f"Target {target} found at index {index}")

else:

print(f"Target {target} not found")

  • This program uses nested loops to search for a value in a list of values

  • The outer loop iterates through each element in the list, the inner loop also iterates through each element

  • The inner loop checks if the current element is equal to the target

  • This approach is inefficient because the inner loop iterates through the entire list for every element in the outer loop

  • This program could be optimised by using the in operator

Use of memory

Python code (inefficient)

def calculate_pi(iterations):

"""

This function calculates an approximation of pi using a list

Inputs:

iterations: The number of iterations for the pi approximation.

Returns:

An approximation of pi.

"""

# Initialise a list to store all calculations

pi_data = []

for i in range(iterations):

# Perform calculation and append to the list

pi_data.append(i * 3.14)

# Calculate the average of elements

pi = sum(pi_data) / len(pi_data)

return pi

# ------------------------------------------------------------------------

# Main program

# ------------------------------------------------------------------------

iterations = 1000000

pi_approx = calculate_pi(iterations)

print(f"Pi approximation: {pi_approx}")

Python code (efficient)

def calculate_pi(iterations):

"""

This function calculates an approximation of pi

Inputs:

iterations: The number of iterations for the pi approximation.

Returns:

An approximation of pi.

"""

# Initialise variables to accumulate sum

numerator = 0

denominator = 0

for i in range(iterations):

# Perform calculation and update variables directly

numerator = numerator + (1 if i % 2 == 0 else -1) * (i + 1)

denominator = denominator + 2 * (i + 1)

# Calculate pi using the accumulated values

pi = 4 * numerator / denominator

return pi

# ------------------------------------------------------------------------

# Main program

# ------------------------------------------------------------------------

iterations = 1000000

pi_approx = calculate_pi(iterations)

print(f"Pi approximation: {pi_approx}")

  • In the first program, pi is approximated by carrying out multiple calculations and storing the results in a list

  • This approach can lead to large lists being created, consuming large amounts of memory

  • In the second program, the same result is achieved by using two variables: numerator and denominator to accumulate the necessary values for the pi calculation

  • This approach avoids a large list and directly updates variables, leading to significantly lower memory usage

Last updated:

You've read 0 of your 5 free revision notes this week

Sign up now. It’s free!

Join the 100,000+ Students that ❤️ Save My Exams

the (exam) results speak for themselves:

Did this page help you?

Robert Hampton

Author: Robert Hampton

Expertise: Computer Science Content Creator

Rob has over 16 years' experience teaching Computer Science and ICT at KS3 & GCSE levels. Rob has demonstrated strong leadership as Head of Department since 2012 and previously supported teacher development as a Specialist Leader of Education, empowering departments to excel in Computer Science. Beyond his tech expertise, Robert embraces the virtual world as an avid gamer, conquering digital battlefields when he's not coding.

James Woodhouse

Author: James Woodhouse

Expertise: Computer Science

James graduated from the University of Sunderland with a degree in ICT and Computing education. He has over 14 years of experience both teaching and leading in Computer Science, specialising in teaching GCSE and A-level. James has held various leadership roles, including Head of Computer Science and coordinator positions for Key Stage 3 and Key Stage 4. James has a keen interest in networking security and technologies aimed at preventing security breaches.