2. Learning Intentions
By the end of this lesson learners will be able to write and utilise the following standard
algorithms
❏ Find Min and max
❏ Linear Search
❏ Counting Occurrences
❏ Write and debug the above algorithms
3. Input validation
Although covered at National 5
The purpose of this algorithm is to ensure that the user inputs data that is in a specified
format or range
4. Input validation Algorithm
GET input FROM KEYBOARD
WHILE input is invalid
SEND message TO DISPLAY
GET input FROM KEYBOARD
END WHILE
5. Input validation example
userscore = int(input("Enter your score: "))
while userscore<0 or userscore>9999:
print("Invalid score. Try again.")
userscore = int(input("Enter your score: "))
6. Find Min and Max Algorithm
The purpose of the Find Min and Find Max algorithms are to look through a list of
items and find the minimum and maximum values respectively.
It uses a variable to hold the current minimum and maximum value.
If a value is found that is lower or higher than the current lowest or highest then that
item is set to the new lowest value
7. Find Min Algorithm
SET Lowest = array(0)
FOR counter FROM 1 to length(array)
IF current item (array(counter)) < lowest
SET Lowest = array(counter)
SET Position = counter
END IF
END FOR
8. Find Min Example
minimum = scores[0]
for counter in range(1,len(times)):
if times[counter] < minimum:
minimum = times[counter]
print ("Fastest time is", minimum)
9. Find Max Algorithm
SET max = array(0)
FOR counter FROM 1 to LENGTH(array)
IF current item (array(counter)) > max
SET max = array(counter)
SET Position = counter
END IF
END FOR
10. Find Max Example
maximum= scores[0]
for counter in range(1,len(scores)):
if scores[counter] > maximum:
maximum= scores[counter]
print ("Best score is", maximum)
11. Linear Search Algorithm
The Linear Search algorithm looks through a list of items and finds if a value is in
the list.
It checks every item in the list and compares it to the search item
After the algorithm has traversed the entire list then it will usually display an output
message
12. Linear Search Algorithm
GET SearchItem FROM KEYBOARD
FOR counter FROM 0 TO length(array)
IF array(counter) = SearchItem
SEND “Item found” TO DISPLAY
END IF
END FOR
13. Linear Search Algorithm improvement?
But what if after we have processed the list we then need to display a message to the
user either showing
That their search it was found (and its position)
Or
A message stating that their item wasn’t in the list.
How do we achieve this?
14. Linear Search Algorithm + Flag
We can improve the algorithm by using a Boolean flag to store whether a match has
been found or not
This will only be used after the entire list has been searched.
15. Linear Search Algorithm + Flag
We can improve the algorithm by using a Boolean flag to store whether a match has
been found or not
This will only be used after the entire list has been searched.
16. Linear Search Example
choice = input("Please enter your colour: ")
for counter in range(0, len(colours)):
if colours[counter] == choice:
print("Your colour was found at position",counter)
17. Linear Search using found flag
found = False
choice = input("Please enter the item to search for: ")
for counter in range(len(array)):
if array[counter] == choice:
found = True
position = counter
if found == False:
print("No item found in list”)
else:
print(“Item details: ",array[position])
18. Linear Search Algorithm Using conditional loop
A further improvement would be to utilise a conditional loop and a found flag
This will allow the algorithm to stop traversing through the loop when a match has
been found
The condition will to be continue the loop until either an item has been found or
there are still items in the list
Meaning for more efficient code due to less operations required…unless the search
item is at the end of the list.
20. Counting Occurrences
The counting occurrences algorithm looks through a list of items and counts the
number of times (occurrences) that a match is found.
It again checks every item in the list and increments a counter when a match is
found.
It is important to only output the matches after the entire list has been traversed.
21. Counting Occurrences Algorithm
GET SearchItem FROM KEYBOARD
SET Total = 0
FOR counter = 0 TO Length(array)
IF array(counter) = SearchItem
SET Total = total +1
END IF
END FOR
22. Counting Occurrences Example
def CountPasses (passes):
occurences = 0
for counter in range(len(passes)):
if passes[counter].upper() == "PASS":
occurences += 1
print("There were",occurences,"passes")
#-------------Main Program-----------------
#set initial values
passes = ["pass","fail","pass","fail","pass"]
CountPasses(passes)
24. Further Research
There are other algorithms that can be used. Investigate the following:
1. Simple Sort
2. Selection Sort using two lists
3. Bubble Sort
4. Binary Search