Slides badge

Algorithm Specification

Learning Intentions

  • Understand what an algorithm is

  • Describe, exemplify and implement the standard algorithms required for National 5

Success Criteria

  • Be able to describe the steps of the standard algorithms at National 5
  • Be able to implement the standard algorithms in Python

What is an algorithm?

  • An algorithm is a series of instructions that can be used to solve a problem.
  • A standard algorithm is an algorithm that is used commonly in the programming community.
  • For National 5 you need to know what the purpose of and how to implement the following standard algorithms:
    • Input validation
    • Running total
    • Traverse an array
  • In theory, a standard algorithm should be easily transferrable to another programming language.

Input validation

  • The input validation algorithm is used to check that some data entry is valid to a certain criteria or within a certain range.
  • An input validation algorithm works by asking the user for a value, checking it is valid and meets the rules then deciding whether to ask the user for the information again (until it is valid) or to accept the data

RECIEVE input FROM KEYBOARD

REPEAT

  RECIEVE input FROM KEYBOARD

UNTIL input > 10 AND input < 20

Input validation

  • To implement this in Python, we use the following code:
print("Please insert a number between 10 and 20")
userAnswer = int(input())

while not(userAnswer >= 10 and userAnswer <= 20):
  print("The number needs to be between 10 and 20")
  print("Please insert a number between 10 and 20")
  userAnswer = int(input())

print("You gave me", userAnswer)

Input validation

  • The following is an example of username authentication
print("Please insert your username")
username = input()

while username != "johnsmith":
  print("Username incorrect. Please retry.")
  username = input()

print("Welcome to the system")

Try creating a login program that asks the user for a username and password and, using input validation, keeps track of how many times they get the username and/or password wrong

 

The program should display how many times the person got it wrong at the end.

 

Username: johnsmith

Password: password

Task

A solution in Python

attempts = 1
print("Please insert your username")
username = input()
print("Please insert your password")
password = input()

while username != "johnsmith" and password != "password":
  print("Username or password incorrect. Please retry.")

  print("Please insert your username")
  username = input()
  print("Please insert your password")
  password = input()
  attempts = attempts + 1

print("You took", attempts, "attempts to login")

Running total

  • Running total is another algorithm you need to know how to implement at National 5. 
  • The running total algorithm works by going through an array and adding each item to the total variable.

Running total

  • In SQARL a running total algorithm looks like:

items = [11, 22, 33, 44, 55]

total = 0
FOR counter FROM 1 TO LENGTH(items) DO

  SET total TO total + 1
 
END FOR

SEND total TO DISPLAY

Running total in Python

  • In Python a running total algorithm looks like:
items = [34, 12, 54, 30, 90, 54, 43, 30, 94, 34]
total = 0

for i in range(0, len(items)):
  total = total + items[i]

print(total)

Copy the code from the previous running total. 

 

Modify the running total so that it counts all numbers above 50 and displays the total.

Task

Traversing an array

  • It is fairly common to need to traverse an array.
  • It's a very straightforward algorithm that will make things like displaying an array easier

FOR counter FROM 0 TO LENGTH(list) DO
 SEND list[counter] TO DISPLAY
END FOR

Traversing an array in Python

  • It is fairly common to need to traverse an array.
  • It's a very straightforward algorithm that will make things like displaying an array easier
items = [34, 12, 54, 30, 90, 54, 43, 30, 94, 34]
for i in range(0, len(items)):
  print(items[i])
  1. Write a program to take in 10 test marks and then calculate and display the average mark.
  2. A test is carried out with a 6/10 pass mark.
    1. Ask the user to enter 5 test marks and ensure the mark is between 5 and 10.
    2. Add a running total that increases each time a person passes the test
    3. Modify the program so that if the person passed it displays their name and mark.
  3. Ask the user for what day of the month and the number of the month they were born. Ensure that the day and the month are within a suitable range using input validation.

Programming tasks

Presentation Overview
Close
JB
Algorithm Specification
© 2020 - 2024 J Balfour
20:50 | 23-05-2024
Join Live Session
Start Remote
Save Progress
Slideshow Outline
Presenter Mode
Widget Screen
Canvas Controls
Random Selector
Timer
Volume Meter
Binary Converter
Python Editor
Show Knox 90
Provide Feedback
Help
!
Keywords
    DragonDocs Management
    Random selector
    Sections
      Binary conversion
      Denary to binary conversion
      Binary to denary conversion
      Feedback 👍
      Accessibility

      Apply a filter:

      ×
      All slideshow files