Category: Implementation (algorithm specification)

Higher – Count Occurrences Standard Algorithm

The count occurrences algorithm is very similar to the linear search v1. However, it includes an incrementing counter keep track of how many items it finds.

def countOcc(target,numbers):
    #Count the number of times target is found in numbers
    count=0
    for number in numbers:
        if number==target:
            count=count+1
    return count

You will notice that I have used a FOR EACH in this example as there is no requirement to track the position of the elements. This algorithm can also be used to count how many numbers are above or below a target value by simply changing the operator in the IF statment.

Higher – Find Maximum and Find Minimum Standard Algorithms

We spent a lot of last week on Standard Algorithms, most of them have the structure of an IF inside a FOR loop. While it is possible to use a FOR EACH loop this would mean that we could not know what the position was of the value we were searching for. In the examples below the returned value (max or min) could be changed to the position to allow this to be used elsewhere in the program.

To find the largest number in an array we would use the “Find Maximum” algorithm

def findMax(numbers):
    # Find biggest number
    max=numbers[0]
    for counter in range(1,len(numbers)):
        if numbers[counter]>max:
            max=numbers[counter]
            #position=counter
    return max

To find the smallest number in an array we would use the “Find Minimum” algorithm

def findMin(numbers):
    #Find smallest number
    min=numbers[0]
    for counter in range(1,len(numbers)):
        if numbers[counter]<min:
            min=numbers[counter]
            #position=counter
    return min

Notice that both of these functions are very similar with only the operator changing. Did you also notice that the for loop starts from index 1? Why do you think this is?

Higher – Linear Search Standard Algorithm

A linear search is used to check if a value is in an array. There are two ways of doing this:-

Check every value in the list, even if you find the value immediatly.

Check until you find the value you are looking for, this is much more effient and can on average half the ammount of time taken for the program to run.

The same algorithms can be used to find a string in an array of strings or a character in a string.

N5 – Input Validation

Today we looked at the following problem

Write a program which will allow a user to input only a number in the range 0-100. An error message will be output if the number is invalid.

The program will output a message when the number input is valid (after the loop for checking the number).

We then worked through Analysis, Design, Implementation and Testing of the program.

Analysis

Input – a number

Process – check number is between 0 and 100

Output – a message about the numbers validity

Design

We looked at all three design methodologies and created designs in our jotter.

Pseudocode

  1. Declare Integer Number
  2. Get Number from user
  3. While Number < 0 OR Number > 100
  4.     Display “Sorry that number is not valid”
  5.     Get number from user
  6. End While
  7. Display “That is a valid number”

Flowchart

Structure Diagram

Implementation in Python


number=0 # declare number as type integer
number = int(input("Please enter a number from 0 - 100 "))
while number < 0 or number > 100:
    print("Sorry that number is not valid")
    number = int(input("Please enter a number from 0 - 100 "))
print("That is a valid number")

Testing

We tested the code with Normal, Extreme and Exceptional test data.