diff --git a/school/a-level/y12 2022-2024/Dice Probabilities.py b/school/a-level/y12 2022-2024/Dice Probabilities.py new file mode 100644 index 0000000..84230b4 --- /dev/null +++ b/school/a-level/y12 2022-2024/Dice Probabilities.py @@ -0,0 +1,40 @@ +from random import randint + +class Dice: + def __init__(self, sides: int = 6): + self.__sides = sides + + def roll(self): + return randint(1, self.__sides) + +DICE_COUNT = 2 +DICE_SIDES = 6 +ROLL_COUNT = 1000 +FORMAT_STRING = ' {: ^10} | {: ^10} | {: ^10}' + +dice = Dice(DICE_SIDES) +frequencies = {} + +for _ in range(ROLL_COUNT): + totalRoll = 0 + + for _ in range(DICE_COUNT): + totalRoll += dice.roll() + + key = f'{totalRoll}' + + if key in frequencies: + frequencies[key] += 1 + else: + frequencies[key] = 1 + +header = FORMAT_STRING.format('Dice Value', 'Frequency', 'Percentage') +print(f'Rolling {DICE_COUNT} dice ({DICE_SIDES} sided) {ROLL_COUNT} times...\n') +print(header) +print('-' * (len(header) + 3)) + +frequencies = sorted(frequencies.items(), key=lambda k:int(k[0])) + +for value, frequency in frequencies: + percentage = (frequency * 100) / ROLL_COUNT + print(FORMAT_STRING.format(value, frequency, f'{percentage:.2f}%')) \ No newline at end of file diff --git a/school/a-level/y12 2022-2024/Floating Point to Denary.py b/school/a-level/y12 2022-2024/Floating Point to Denary.py new file mode 100644 index 0000000..4f965a7 --- /dev/null +++ b/school/a-level/y12 2022-2024/Floating Point to Denary.py @@ -0,0 +1,65 @@ +import re + +def twosComplementToDenary(bitPattern): + value = 0 + + for i in range(len(bitPattern) - 1, -1, -1): + placeValue = 2 ** (len(bitPattern) - i - 1) + if i == 0: placeValue *= -1 + + value += int(bitPattern[i]) * placeValue + + return value + +def twosComplementNormalisedToDenary(bitPattern): + wholePart, fractionalPart = bitPattern[0], bitPattern[-len(bitPattern)+1:] + value = -1 if int(wholePart) == 1 else 0 + + for i in range(len(fractionalPart), 0, -1): + placeValue = 2 ** -i + value += int(fractionalPart[i - 1]) * placeValue + + return value + +while True: + try: + binary = input('Please enter the bit pattern you would like to decode: ') + + if re.match('[^01]+', binary): + raise ValueError + + break + except ValueError: + print('Please ensure that you input a valid binary sequence!') + +while True: + try: + while True: + try: + mantissaBits = int(input('Please enter the amount of bits that the mantissa uses: ')) + break + except ValueError: + print('Please ensure that you input a valid integer!') + + while True: + try: + exponentBits = int(input('Please enter the amount of bits that the exponent uses: ')) + break + except ValueError: + print('Please ensure that you input a valid integer!') + + if exponentBits + mantissaBits != len(binary): + raise ValueError + + break + except ValueError: + print(f'The amount of bits you inputed for the mantissa and exponent do not add up to the length of your sequence - please make sure they span the entire range of the sequence! For reference, your sequence was {binary}') + +binary = str(binary) +mantissa = binary[:mantissaBits] +exponent = binary[-exponentBits:] + +denaryExponent = twosComplementToDenary(exponent) +denaryMantissa = twosComplementNormalisedToDenary(mantissa) + +print(denaryMantissa * (2 ** denaryExponent)) \ No newline at end of file diff --git a/school/a-level/y12 2022-2024/Golfed Floating Point to Denary.py b/school/a-level/y12 2022-2024/Golfed Floating Point to Denary.py new file mode 100644 index 0000000..bd9f5bc --- /dev/null +++ b/school/a-level/y12 2022-2024/Golfed Floating Point to Denary.py @@ -0,0 +1 @@ +l,s,m,r,i,z,c,n=len,sum,map,range,int,-1,lambda b:s(m(lambda x:-x[1] if x[0]==0 else x[1],[(k,i(b[k])*2**(l(b)-k-1)) for k in r(l(b)-1,-1,-1)])),lambda b:(f:=b[-l(b)+1:],(s([i(f[k-1])/2**k for k in r(l(f),0,z)]))+(z if i(b[0])==1 else 0))[1] diff --git a/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.2.5.PNG b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.2.5.PNG new file mode 100644 index 0000000..844cd83 Binary files /dev/null and b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.2.5.PNG differ diff --git a/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.2.6.PNG b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.2.6.PNG new file mode 100644 index 0000000..c84aa95 Binary files /dev/null and b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.2.6.PNG differ diff --git a/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.2.7.PNG b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.2.7.PNG new file mode 100644 index 0000000..85db6b9 Binary files /dev/null and b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.2.7.PNG differ diff --git a/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.2.8.PNG b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.2.8.PNG new file mode 100644 index 0000000..0306421 Binary files /dev/null and b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.2.8.PNG differ diff --git a/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.2.9.PNG b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.2.9.PNG new file mode 100644 index 0000000..696a5fa Binary files /dev/null and b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.2.9.PNG differ diff --git a/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.3.4.PNG b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.3.4.PNG new file mode 100644 index 0000000..63f3c8f Binary files /dev/null and b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.3.4.PNG differ diff --git a/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.3.5.PNG b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.3.5.PNG new file mode 100644 index 0000000..fbe5e84 Binary files /dev/null and b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.3.5.PNG differ diff --git a/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.4.2.3.PNG b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.4.2.3.PNG new file mode 100644 index 0000000..4821eac Binary files /dev/null and b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.4.2.3.PNG differ diff --git a/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.4.2.4.PNG b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.4.2.4.PNG new file mode 100644 index 0000000..5c7da17 Binary files /dev/null and b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.4.2.4.PNG differ diff --git a/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.4.2.5.PNG b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.4.2.5.PNG new file mode 100644 index 0000000..23b13d7 Binary files /dev/null and b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.4.2.5.PNG differ diff --git a/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.4.2.6.PNG b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.4.2.6.PNG new file mode 100644 index 0000000..59bbe0f Binary files /dev/null and b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.4.2.6.PNG differ diff --git a/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.4.2.7.PNG b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.4.2.7.PNG new file mode 100644 index 0000000..9736ff5 Binary files /dev/null and b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.4.2.7.PNG differ diff --git a/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.5.2.PNG b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.5.2.PNG new file mode 100644 index 0000000..d35107e Binary files /dev/null and b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/11.5.2.PNG differ diff --git a/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/Letter Frequency.py b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/Letter Frequency.py new file mode 100644 index 0000000..0b714ab --- /dev/null +++ b/school/a-level/y12 2022-2024/Homework/07-12-22 - Dictionaries/Letter Frequency.py @@ -0,0 +1,17 @@ +from typing import Dict +import re + +def letterFrequency(text: str) -> Dict[str, int]: + output = {} + text = re.sub('[^a-zA-Z]+', '', text) + + for letter in text: + if letter in output: + output[letter] += 1 + else: + output[letter] = 1 + + return output + +frequency = letterFrequency('I wish I wish with all my heart to fly with dragons in a land apart') +print(frequency) diff --git a/school/a-level/homework/fundementals/einstein.py b/school/a-level/y12 old school/fundementals/einstein.py similarity index 100% rename from school/a-level/homework/fundementals/einstein.py rename to school/a-level/y12 old school/fundementals/einstein.py diff --git a/school/a-level/homework/fundementals/indoor voice.py b/school/a-level/y12 old school/fundementals/indoor voice.py similarity index 100% rename from school/a-level/homework/fundementals/indoor voice.py rename to school/a-level/y12 old school/fundementals/indoor voice.py diff --git a/school/a-level/homework/fundementals/making faces.py b/school/a-level/y12 old school/fundementals/making faces.py similarity index 100% rename from school/a-level/homework/fundementals/making faces.py rename to school/a-level/y12 old school/fundementals/making faces.py diff --git a/school/a-level/homework/fundementals/playback speed.py b/school/a-level/y12 old school/fundementals/playback speed.py similarity index 100% rename from school/a-level/homework/fundementals/playback speed.py rename to school/a-level/y12 old school/fundementals/playback speed.py diff --git a/school/a-level/homework/fundementals/tip calculator.py b/school/a-level/y12 old school/fundementals/tip calculator.py similarity index 100% rename from school/a-level/homework/fundementals/tip calculator.py rename to school/a-level/y12 old school/fundementals/tip calculator.py diff --git a/school/a-level/y13 2021-2023/Homework/OOP 2 - Shopping Basket/Item.py b/school/a-level/y13 2021-2023/Homework/OOP 2 - Shopping Basket/Item.py new file mode 100644 index 0000000..ebb8138 --- /dev/null +++ b/school/a-level/y13 2021-2023/Homework/OOP 2 - Shopping Basket/Item.py @@ -0,0 +1,8 @@ +class Item: + # Constructor + def __init__(self, name, description, price, quantity): + self.name = name + self.description = description + self.price = price + self.initialQuantity = quantity + self.quantity = quantity \ No newline at end of file diff --git a/school/a-level/y13 2021-2023/Homework/OOP 2 - Shopping Basket/ShoppingBasket.py b/school/a-level/y13 2021-2023/Homework/OOP 2 - Shopping Basket/ShoppingBasket.py new file mode 100644 index 0000000..8d15e95 --- /dev/null +++ b/school/a-level/y13 2021-2023/Homework/OOP 2 - Shopping Basket/ShoppingBasket.py @@ -0,0 +1,82 @@ +from Item import Item + +class ShoppingBasket: + # Constructor + def __init__(self): + self.items = {} #A dictionary of all the items in the shopping basket: {item:quantity} + self.checkout = False + + # A method to add an item to the shopping basket + def addItem(self,item,quantity=1): + if item.quantity < quantity: + print(f'Invalid operation - there is not enough {item.name} in stock!') + elif quantity > 0: + item.quantity -= quantity + #Check if the item is already in the shopping basket + if item in self.items: + self.items[item] += quantity + else: + self.items[item] = quantity + else: + print("Invalid operation - Quantity must be a positive number!") + + # A method to remove an item from the shopping basket (or reduce it's quantity) + def removeItem(self,item,quantity=0): + if self.items[item] < quantity: + print(f'Invalid operation - there is not enough {item.name} in the basket!') + elif quantity<=0: + #Remove the item + self.items.pop(item, None) + else: + item.quantity += quantity + if item in self.items: + if quantity 0: + quantDifference = self.items[item] if item in self.items else 0 + quantDifference -= quantity + item.quantity += quantDifference + self.items[item] = quantity + else: + self.removeItem(item) + + # A method to view/list the content of the basket. + def view(self): + totalCost = 0 + print("---------------------") + for item in self.items: + quantity = self.items[item] + cost = quantity * item.price + print(" + " + item.name + " - " + str(quantity) + " x £" + '{0:.2f}'.format(item.price) + " = £" + '{0:.2f}'.format(cost)) + totalCost += cost + print("---------------------") + print(" = £" + '{0:.2f}'.format(totalCost)) + print("---------------------") + + # A method to calculate the total cost of the basket. + def getTotalCost(self): + totalCost = 0 + for item in self.items: + quantity = self.items[item] + cost = quantity * item.price + totalCost += cost + return totalCost + + # A method to empty the content of the basket + def reset(self): + for item in self.items: + item.quantity = item.initialQuantity + + self.items = {} + + # A method to return whether the basket is empty or not: + def isEmpty(self): + return len(self.items)==0 + diff --git a/school/a-level/y13 2021-2023/Homework/OOP 2 - Shopping Basket/main.py b/school/a-level/y13 2021-2023/Homework/OOP 2 - Shopping Basket/main.py new file mode 100644 index 0000000..a4f273f --- /dev/null +++ b/school/a-level/y13 2021-2023/Homework/OOP 2 - Shopping Basket/main.py @@ -0,0 +1,30 @@ +#Shopping Basket Class - www.101computing.net/shopping-basket-class/ +from Item import Item +from ShoppingBasket import ShoppingBasket + +tomatoSoup = Item("Tomato Soup","200mL can", 0.70, 3) +spaghetti = Item("Spaghetti","500g pack", 1.10, 4) +blackOlives = Item("Black Olives Jar","200g Jar", 2.10, 100) +mozarella = Item("Mozarella","100g", 1.50, 2) +gratedCheese = Item("Grated Cheese","100g",2.20, 1) + +myBasket = ShoppingBasket() + +myBasket.addItem(tomatoSoup, 4) +myBasket.addItem(blackOlives, 1) +myBasket.addItem(mozarella, 2) +myBasket.addItem(tomatoSoup, 6) + +myBasket.view() + +myBasket.removeItem(mozarella, 3) +print(mozarella.quantity) +myBasket.reset() +myBasket.view() +print(mozarella.quantity) + +myBasket.updateItem(tomatoSoup, 3) +myBasket.view() +myBasket.updateItem(tomatoSoup, 0) +myBasket.view() + diff --git a/school/a-level/y13 2021-2023/OOP/Bank Account Controller UML.graphml b/school/a-level/y13 2021-2023/OOP/Bank Account Controller UML.graphml new file mode 100644 index 0000000..79abfd4 --- /dev/null +++ b/school/a-level/y13 2021-2023/OOP/Bank Account Controller UML.graphml @@ -0,0 +1,121 @@ + + + + + + true + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + {"version":"2.0.0","theme":{"name":"light","version":"1.0.0"},"layout":"layout-uml","config":{"p_useDrawingAsSketch":false,"p_selectedElementsIncrementally":false,"p_nodeToNodeDistance":30,"p_automaticEdgeGroupingEnabled":false,"p_considerNodeLabels":true,"p_edgeLabeling":0,"p_orientation":0}} + + + 1 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/school/a-level/y13 2021-2023/OOP/Bank Account Controller UML.png b/school/a-level/y13 2021-2023/OOP/Bank Account Controller UML.png new file mode 100644 index 0000000..ea0eab3 Binary files /dev/null and b/school/a-level/y13 2021-2023/OOP/Bank Account Controller UML.png differ diff --git a/school/a-level/y13 2021-2023/OOP/Bank Account Controller.py b/school/a-level/y13 2021-2023/OOP/Bank Account Controller.py new file mode 100644 index 0000000..de1aa8d --- /dev/null +++ b/school/a-level/y13 2021-2023/OOP/Bank Account Controller.py @@ -0,0 +1,107 @@ +CONFIRMATION_VALIDATOR = lambda x: x.lower() not in ['yes', 'y', 'no', 'n'] + +class BankAccount: + def __init__(self): + self.__balance = 0 + + def getBalance(self): + return self.__balance + + def setBalance(self, newBalance): + self.__balance = newBalance + +class Controller: + OVERDRAWN_FEE = 5 + + def __init__(self, account): + self.__account = account + + def __overdrawnFee(self): + balance = self.fetchBalance() + + if balance < 0: + self.__account.setBalance(balance - Controller.OVERDRAWN_FEE) + + return balance < 0 + + def fetchBalance(self): + return self.__account.getBalance() + + def formatBalance(self): + balance = self.fetchBalance() + sign = '' if balance >= 0 else '-' + return f'{sign}£{abs(balance):.2f}' + + def payIn(self, amount): + self.__account.setBalance(self.fetchBalance() + amount) + + def withdraw(self, amount): + fee = self.__overdrawnFee() + self.__account.setBalance(self.fetchBalance() - amount) + return fee + +account = BankAccount() +controller = Controller(account) + +def fetchValue(caster, typeName, prompt, validator = None, validatorFailMessage = None): + while True: + try: + value = caster(input(prompt)) + + if validator: + if validator(value): + raise ValueError('validator') + + return value + except ValueError as e: + if str(e) == 'validator' and validatorFailMessage: + print(validatorFailMessage) + else: + print(f'Please make sure you input a valid {typeName}.') + +def displayBalance(): + print(f'You currently have a balance of {controller.formatBalance()}!') + +def menu(): + print("""Welcome to the bank! What would you like to do? + +1) View your current balance +2) Pay in money +3) Withdraw money +4) Exit""") + + choice = fetchValue(int, 'integer', 'Please make your selection: ', lambda x: (x > 4 or x <= 0)) + + if choice == 1: + displayBalance() + elif choice == 2: + value = fetchValue(float, 'float', 'Please enter the amount of money you would like to pay in: ') + controller.payIn(value) + displayBalance() + elif choice == 3: + value = fetchValue(float, 'float', 'Please enter the amount of money you would like to withdraw: ') + chargedFee = controller.withdraw(value) + + if chargedFee: + print(f'You are currently overdrawn! If you go through with this transaction, you will have to pay a fee of £{Controller.OVERDRAWN_FEE:.2f}, and your balance will become {controller.formatBalance()}.') + confirm = fetchValue(str, 'input', 'Would you like to proceed? (y/n)', CONFIRMATION_VALIDATOR) + + if confirm in ['n', 'no']: + controller.payIn(value + Controller.OVERDRAWN_FEE) + print('Your transaction has been cancelled!') + else: + displayBalance() + else: + displayBalance() + elif choice == 4: + exit() + + +if __name__ == '__main__': + while True: + menu() + + again = fetchValue(str, 'input', 'Would you like to go again? (y/n)', CONFIRMATION_VALIDATOR) + + if again in ['n', 'no']: + break \ No newline at end of file diff --git a/school/a-level/y13 2021-2023/OOP/Pest.py b/school/a-level/y13 2021-2023/OOP/Pest.py new file mode 100644 index 0000000..0579e28 --- /dev/null +++ b/school/a-level/y13 2021-2023/OOP/Pest.py @@ -0,0 +1,15 @@ +class Pest: + def __init__(self, legs, noise): + self.__legs = legs + self.__noise = noise + + def getLegs(self): + return self.__legs + + def getNoise(self): + return self.__noise + +dog = Pest(4, 'woof') +roach = Pest(6, 'click click click') + +print(roach.getNoise()) \ No newline at end of file