Category: Higher

Higher – SDD – Reading a file into an array

The code below allows us to read a file into an array then process the data. In this case the processing is just to see how many of each of the numbers (1-6) there were in the file.

def getData(array):
    txtFile=open("diceRolls.txt","r")
    for x in range(len(array)):
        array[x]=int(txtFile.readline().strip())
    
    txtFile.close()
    return array

def countOcc(array,target):
    total=0
    for number in array:
        if number==target:
            total=total+1
            
    return total

def main():
    dice=[0 for x in range(1000)]
    dice=getData(dice)
    for x in range(1,7):
        print("Number of",x,"'s=",countOcc(dice,x))
        
main()

The text file is the result of 1000 D6 rolls.
Can you modify the program to work out which number has the largest number of occurrences?

Higher Revision Resources

Thanks to Mr. M. Hay for collating these

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.

Higher – Random Number Array Function

The class was asked to create a function that generated 100 number dice throws and stored them in an array.

This function is used to create an array of random integers. The size of the array and the magnatude of the values is set when the function is called.


def randomNumbers(size,min,max):
    # creates an array of (size) random numbers between min and max
    import random
    numbers=[]
    for loop in range(size):
        numbers.append(random.randint(min,max))
    return numbers

rndNumbers=randomNumbers(100,1,6)