From 35b3ba952427c8ededd59fbfc71a7233a817fdbc Mon Sep 17 00:00:00 2001 From: h7x4 Date: Mon, 14 Sep 2020 18:40:56 +0200 Subject: [PATCH] Add exercises --- Exercise 3/11 - Doble lokker/11c.py | 45 +++++++++++++++++++++ Exercise 3/11 - Doble lokker/11d.py | 62 +++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 Exercise 3/11 - Doble lokker/11c.py create mode 100644 Exercise 3/11 - Doble lokker/11d.py diff --git a/Exercise 3/11 - Doble lokker/11c.py b/Exercise 3/11 - Doble lokker/11c.py new file mode 100644 index 0000000..73eb01d --- /dev/null +++ b/Exercise 3/11 - Doble lokker/11c.py @@ -0,0 +1,45 @@ +from math import sqrt + + +# O(√n) +def isPrime(n): + if n < 2: return False + if n == 2 or n == 3 or n == 5: return True + limit = int(sqrt(n)) + numberToCheck = 5 + while numberToCheck <= limit: + if n % numberToCheck == 0: return False + numberToCheck += 2 # Skip all even numbers + return True + + +# Would be O(log₂(n)), but isPrime(n) is used, +# thus it's O(√n) +def findAllFactors(n): + factors = [] + while not isPrime(n): + for i in range(2, int(n)): + if n % i == 0: + factors.append(i) + n = n / i + break + factors.append(int(n)) + return factors + + +def factorize(n): + factors = [] + if isPrime(n): + factors.append(n) + else: + factors = findAllFactors(n) + return factors + + +if __name__ == "__main__": + n = int(input('Skriv inn et positivt heltall: ')) + factors = factorize(n) + if len(factors) == 1: + print(f'{n} er et primtall') + else: + print(f'{n} = {" * ".join([str(tall) for tall in factorize(n)])}') \ No newline at end of file diff --git a/Exercise 3/11 - Doble lokker/11d.py b/Exercise 3/11 - Doble lokker/11d.py new file mode 100644 index 0000000..669d1f9 --- /dev/null +++ b/Exercise 3/11 - Doble lokker/11d.py @@ -0,0 +1,62 @@ +from random import randint + + +class multiplicationGame: + def __init__(self, min, max, tries): + self.min = min + self.max = max + self.tries = tries + self.updateProblem() + + def generateNewMultiplicationProblem(self) -> (int, int): + number = lambda: randint(self.min, self.max) + self.currentProblem = (number(), number()) + + def updateProblem(self): + self.generateNewMultiplicationProblem() + self.currentTries = self.tries + + def userWantsNewQuestion(self) -> bool: + while True: + answer = input( + 'Er det ønskelig med flere spørsmål? Skriv 1 for ja og 0 for nei: ') + if answer in ['1', '0']: + return bool(int(answer)) + else: + print('Skriv 1 for ja og 0 for nei') + + def checkIfUserWantsNewQuestion(self): + if not self.userWantsNewQuestion(): + exit(0) + + def wrongAnswer(self): + self.currentTries -= 1 + if self.currentTries == 0: + print( + 'Dessverre klarte du ikke dette regnestykket, men vent så får du et nytt et:)' + ) + self.checkIfUserWantsNewQuestion() + self.updateProblem() + else: + print(f'Dessverre ikke riktig. Du har {self.currentTries} forsøk igjen.') + + def correctAnswer(self): + print('Gratulerer, det er helt riktig!') + self.checkIfUserWantsNewQuestion() + self.updateProblem() + + def update(self): + answer = int(input(f'Hva blir {self.currentProblem[0]} * {self.currentProblem[1]}? ')) + if answer == self.currentProblem[0] * self.currentProblem[1]: + self.correctAnswer() + else: + self.wrongAnswer() + + def loop(self): + while True: + self.update() + + +if __name__ == "__main__": + game = multiplicationGame(min=0, max=10, tries=3) + game.loop()