The attendance officer at OCR school has to analyse lots of register data on a daily basis.
Registers are stored in the following format:
register = ["P","P","N","P","P"]
1a) Write a program to create a register like the one above. You should complete the following steps:
-Ask the user for a five seperate marks
-These should be added to an array
-The array should be output at the end
[5 marks]
1 | register = [] |
2 | for x in range(5): |
3 | print("Enter a mark") |
4 | m = input() |
5 | register.append(m) |
6 | print(register) |
1b) Assume register is already defined and could contain any combination of marks. Print out how many "P" marks and how many "N" marks are in the array register.
[7 marks]
1 | n = 0 |
2 | p = 0 |
3 | for x in range(len(register)): |
4 | if register[x] == "N": |
5 | n = n + 1 |
6 | else: |
7 | p = p + 1 |
8 | print("P:", p) |
9 | print("N:", n) |
The amount of valid marks has expanded to include more than "N" and "P" and contains 6 different valid characters. The function isValidMark() takes 1 parameter and returns the string "valid" if it is valid and "invalid" if it is not.
1c) Take an input from the user and using the function, print "invalid" or "valid" if the character entered is valid.
Assume the function mentioned above is already defined and you do not have to create it yourself.
[4 marks]
1 | print("Enter a mark") |
2 | mark = input() |
3 | v = isValidMark(mark) |
4 | print(v) |
A computer science website is trying to teach its users about binary representation.
You can compress a string of repeated characters by stating the character and the number of repetitions.
e.g.
bbbbb -> b5
aaaaaaaaaaa -> a11
ccc -> c3
1a) Ask for an input from the user, assume it is entered in the first format at above. Print out the compressed version.
[4 marks]
1 | print("Enter a string") |
2 | i = input() |
3 | l = len(i) |
4 | l = str(l) |
5 | print(i[0] + l) |
A simple hexadecimal program needs to be created to help students understand when hexadecimal numbers uses letters intead of numbers.
1b) Create a program that does the following:
-Asks the user for a denary number between 0-15
-Outputs the hexadecimal alternative
[4 marks]
1 | print("Enter a hexadecimal digit") |
2 | i = input() |
3 | if i < 10: |
4 | print(i) |
5 | elif i == 10: |
6 | print("A") |
7 | elif i == 11: |
8 | print("B") |
9 | elif i == 12: |
10 | print("C") |
11 | elif i == 13: |
12 | print("D") |
13 | elif i == 14: |
14 | print("E") |
15 | elif i == 15: |
16 | print("F") |
The website wants to help students convert from denary to binary. Before the conversion, the website wants to check the number is binary.
1c) Create a function "isBinary" that takes one parameter, a binary number as a string. Return True if the number is binary and false if the number is not
[7 marks]
1 | def isBinary(string): |
2 | for x in range(len(string)): |
3 | if string[x] != "1" and string[x] != "0": |
4 | return False |
5 | return True |
The function binaryToDenary() takes one parameter, a binary number and returns the denary equivalent
1d) Create a program that does the following:
-Take an input from the user
-Check if it is a valid binary number using your function from 1c
-If the number isn't valid you should print out "Invalid"
-If the number is valid you should use the function "binaryToDenary" to print out the denary value
[7 marks]
1 | print("Enter a binary number") |
2 | i = input() |
3 | a = isBinary(i) |
4 | if a == False: |
5 | print("Invalid") |
6 | else: |
7 | n = binaryToDenary(i) |
8 | print(n) |
A factory produces a certain amount of toys each day.
1a) For each 42g of plastic they import, they can produce 3 toy cars. Create a program to take in an input (the number of grams of plastic) and output how many toy cars could be produced.
[3 marks]
1 | print("How many g of plastic are there?") |
2 | i = input() |
3 | i = i // 42 |
4 | i = i * 3 |
5 | print(i) |
To make a single teddy bear you need 30g of stuffing and 40g of cloth.
1b) Create a function called howManyBears that takes two parameters, the amount of stuffing and cloth (both in grams). Return the number of teddy bears you can create
[6 marks]
1 | def howManyBears(stuffing, cloth): |
2 | stuffing = stuffing // 30 |
3 | cloth = cloth // 40 |
4 | if stuffing < cloth: |
5 | return stuffing |
6 | else: |
7 | return cloth |
Every Friday the factory needs to reorder stock. Any item below 1000g needs to be reordered
The number of grams for each item is stored within an array "storage", see a sample of the array below
storage = [540,1421,200,4500,1100,100,...]1c) Write code to output how many items are under 1000 and need to be reordered
[5 marks]
1 | t = 0 |
2 | for x in range(len(storage)): |
3 | if storage[x] < 1000: |
4 | t = t + 1 |
5 | print(t) |
You are a software developer working for a company that sells pet supplies online. The company has recently decided to add a feature to their website that displays the top 5 most popular pet names.
The array of most popular pet names isn't always of length 5 and can be bigger or smaller.
1a) Create a procedure called topFive() that takes one parameter. The parameter could be an array of any size.
-If the array has more than 5 names, you should only print out the first five
-If the array has five names or fewer, all names should be printed out
[5 marks]
1 | def topFive(arr): |
2 | for x in range(len(arr)): |
3 | if x == 5: |
4 | break |
5 | print(arr[x]) |
The name array has been provided as the following:
petNames = ["Bella","Max","Charlie","Lucy","Daisy","Milo","Oliver"]
1b) Use your function above to print out the top 5 pet names. You can assume the array is already defined
[2 marks]
1 | topFive(petNames) |
The company has decided to read the names from a file instead. The file is called "petNames.txt". The file contains exactly 5 names.
1c) Write a program to print out all the names to the screen
[7 marks]
1 | f = open("petNames.txt","r") |
2 | lines = f.read().split("\n") |
3 | f.close() |
4 | for x in range(len(lines)): |
5 | print(lines[x]) |
A school has asked students to submit their GCSE option choices.
1a) Write a program that asks students if they want to pick History or Geography. Create a program that does the following:
-Asks for a student to pick either Geography or History
-Repeat 20 times
-Output the number of students who picked history and the number of students who picked geography
[6 marks]
1 | g = 0 |
2 | h = 0 |
3 | for x in range(10): |
4 | print("History or geography?") |
5 | i = input() |
6 | if i == "History": |
7 | h = h + 1 |
8 | elif i == "geography": |
9 | g = g + 1 |
10 | print("History: " + str(h)) |
11 | print("Geography: " + str(g)) |
Each student must pick at least 8 subjects. All the subjects that a student has picked is stored in an array
1b) Create a function called correctNumber that takes one parameter, an array and returns True if the number of subjects is correct or False if the number of subjects is incorrect
[5 marks]
1 | def correctNumber(arr): |
2 | if len(arr) >= 8: |
3 | return True |
4 | else: |
5 | return False |
Each student must pick english and maths.
1c) Create a function coresPicked. The function should take a single parameter, an array of subjects, and returns True if the array contains both "English" and "Maths".
[8 marks]
1 | def coresPicked(arr): |
2 | eng = False |
3 | maths = False |
4 | for x in range(len(arr)): |
5 | if arr[x] == "English": |
6 | eng = True |
7 | elif arr[x] == "Maths": |
8 | maths = True |
9 | if eng == True and Maths == True: |
10 | return True |
11 | else: |
12 | return False |
1d)Assume the array "subjects" is already defined. Using your functions from 1b and 1c, work out if the array is valid (meets the two conditions set out in the previous questions). If it is valid print out True otherwise print out False
[6 marks]
1 | a = correctNumber(subjects) |
2 | b = coresPicked(subjects) |
3 | if a == True and b == True: |
4 | print(True) |
5 | else: |
6 | print(False) |
A supermarket is closing early for Christmas and has a sale on some items in the shop. All the items on sale have a 50% discount.
1a) Write a function called newPrice that has two parameters, the first is the normal price of the item and the second is the string "y" or "n". "y" represents that the item is on sale and "n" means it is not. The function should return the new price if it is on sale or the original price if not.
[4 marks]
1 | def newPrice(original,sale): |
2 | if sale == "y": |
3 | return original * 0.5 |
4 | else: |
5 | return original |
At the supermarket tills many items are scanned and a final price is given.
1b)Write a program that completes the following:
-Asks for the price of the item
-Asks if the item is on sale
-Uses the function created above to work out the new price of the item
-Repeats steps 1,2 and 3 until a price of 0 is entered
-Outputs the total cose at the end
[8 marks]
1 | total = 0 |
2 | while True: |
3 | print("Enter a price") |
4 | price = input() |
5 | if price == 0: |
6 | break |
7 | print("Is the item on sale?") |
8 | sale = input() |
9 | c = newPrice(price,sale) |
10 | total = total + c |
11 | print(total) |
A games company is designing a tower defence game. Each monster is stored using an array:
arr = ["mon1",56]The first index stores the player's name and the second stores its health. Throughout the game the health changes and the above is only an example.
1a) Assuming the array is already defined but could be any value, write code to check if the monster is alive or dead. Your program should output "Alive" if the health is above 0 and dead if it is not.
[3 marks]
1 | if arr[1] > 0: |
2 | print("Alive") |
3 | else: |
4 | print("Dead") |
To help debug their code the developers write a function to output the monsters name and their current health in the following format:
mon1: 56
1b) Write a function called printMonster that takes one parameter, the array mentioned above, and outputs the name and health in the format above
[3 marks]
1 | def printMonster(a): |
2 | print(a[0] + ":" + a[1]) |
The developers are ready to test their game and want to create a feedback program. The program should do the following:
- Ask for a a rating between 1 and 5
- Repeat step 1 until "stop" is entered
- Output the total amount of ratings
- Output the average of all ratings input
1c) Create a program that matches the criteria above, you do not need to validate the input
[7 marks]
1 | ratingTot = 0 |
2 | amount = 0 |
3 | while True: |
4 | print("Enter a rating") |
5 | i = input() |
6 | if i == "stop": |
7 | break |
8 | amount = amount + 1 |
9 | ratingTot = ratingTot + i |
10 | print(amount) |
11 | print(ratingTot/amount) |