Please ignore
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 – Test Post with graphics
List of things
- Thing 1
- Thing 2
- Thing 3
Well I wonder if blocks are the way to go? I guess I could turn on a drop cap or two,
Higher – DIV CSS recap lesson
Using the supplied flag sheet create a HTML document to display each flag. Flags should be created using <DIV>, use CSS to style, you can choose to use inline, internal, or external CSS.
Make use of float, background-color, borders, etc. as you see fit. I would say each flag should be about 300px long and 150px high.
Code for an example French flag shown after the break.
Higher Revision Resources
- Higher Computing Science – https://sites.google.com/rgc.aberdeen.sch.uk/rgc-highercomputing/home
- Codecademy – https://www.codecademy.com/learn/learn-python
- Programiz – https://www.programiz.com/python-programming
- W3Schools HTML – https://www.w3schools.com/html/default.asp
- W3Schools CSS – https://www.w3schools.com/css/default.asp
- HTML Tutorials – http://html.net/tutorials/
- Mozilla – https://developer.mozilla.org/en-US/docs/Learn/HTML
- HTML Periodic Table – https://www.madebymike.com.au/demos/html5-periodic-table/
- Khan Academy (CSS/JS) – https://www.khanacademy.org/computing/computer-programming/html-css-js
- BBC Bitesize Higher – https://www.bbc.com/bitesize/subjects/zxmh34j
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.
def linSearchV1(target,numbers): #Search for target in all of the numbers found=False for counter in range(len(numbers)): if numbers[counter]==target: found=True return found
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.
def linSearchV2(target,numbers): #Search for the target numbers until it is found counter=0 found=False while not found and counter!=len(numbers): if numbers[counter]==target: found=True else: counter=counter+1 return found
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)
Higher – Substring Operations
pre-defined functions (with parameters):
- to create substrings
In Python we can treat strings as arrays of characters. We do this by using
string[start:stop:step]
where
- start is the postion of the first character to be displayed.
- stop is the postion to stop displaying characters (not displayed).
- step is the step value, just like a for loop.