Back to 课程

Computer Science GCES EDEXCEL

0% Complete
0/0 Steps
  1. Decomposition And Abstraction Edexcel
    2 主题
  2. Algorithms Edexcel
    11 主题
  3. Truth Tables Edexcel
    3 主题
  4. Binary Edexcel
    6 主题
  5. Data Representation Edexcel
    4 主题
  6. Data Storage And Compression Edexcel
    2 主题
  7. Hardware Edexcel
    5 主题
  8. Software Edexcel
    3 主题
  9. Programming Languages Edexcel
    2 主题
  10. Networks Edexcel
    7 主题
  11. Network Security Edexcel
    2 主题
  12. Environmental Issues Edexcel
    1 主题
  13. Ethical And Legal Issues Edexcel
    3 主题
  14. Cybersecurity Edexcel
    2 主题
  15. Develop Code Edexcel
    6 主题
  16. Constructs Edexcel
    4 主题
  17. Data Types And Data Structures Edexcel
    5 主题
  18. Operators Edexcel
    1 主题
  19. Subprograms Edexcel
    2 主题
课 Progress
0% Complete

Exam code:1CP2

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

Responses

您的邮箱地址不会被公开。 必填项已用 * 标注