Add exercises
This commit is contained in:
parent
f451574fe9
commit
35b3ba9524
45
Exercise 3/11 - Doble lokker/11c.py
Normal file
45
Exercise 3/11 - Doble lokker/11c.py
Normal file
@ -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)])}')
|
62
Exercise 3/11 - Doble lokker/11d.py
Normal file
62
Exercise 3/11 - Doble lokker/11d.py
Normal file
@ -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()
|
Loading…
Reference in New Issue
Block a user