Add exercise 5

This commit is contained in:
Oystein Kristoffer Tveit 2020-10-07 13:13:24 +02:00
parent 72478e8199
commit 0c07bfaf9d
8 changed files with 283 additions and 62 deletions

View File

@ -0,0 +1,15 @@
def f(tol):
g = lambda k: 1 + 1/k**2
iterationCount = 2
result = 2
while True:
prevResult = result
result *= g(iterationCount)
if result - prevResult < tol:
return (result, iterationCount)
iterationCount += 1
if __name__ == "__main__":
result, iterationCount = f(0.01)
print(f'Produktet ble {"{:.2f}".format(result)} etter {iterationCount} iterasjoner.')

View File

@ -0,0 +1,14 @@
def f(tol, count=0):
g = lambda x: 1 + 1/x**2
product = g(count+1)
# Base case
if product < 1 + tol:
return (product, count)
newProd, newCount = f(tol, count + 1)
return (product * newProd, newCount) # Nest in the last count value without modification
if __name__ == "__main__":
result, recursionCount = f(0.01)
print(f'Rekursjonsdybden er {recursionCount}\nProduktet ble {"{:.2f}".format(result)}')

View File

@ -0,0 +1,9 @@
from local import is_leap_year
def weekday_newyear(year):
daySum = sum([366 if is_leap_year(year) else 365 for year in range(year)])
return (daySum - 2) % 7
if __name__ == "__main__":
days = ['man', 'tir', 'ons', 'tor', 'fre', 'lor', 'son']
_ = [ print(f'{year} {days[weekday_newyear(year)]}') for year in range(1900, 1920) ]

View File

@ -0,0 +1,2 @@
def is_workday(day):
return day < 5

View File

@ -0,0 +1,15 @@
from itertools import islice, cycle
from local import is_leap_year
from a import weekday_newyear
from b import is_workday
def workdays_in_year(year):
firstDay = weekday_newyear(year)
cycler = islice( cycle(range(7)), firstDay, None)
days = [next(cycler) for day in range((366 if is_leap_year(year) else 365))]
workdays = [day for day in days if day < 5]
return len(workdays)
if __name__ == "__main__":
_ = [print(f'{year} har {workdays_in_year(year)} arbeidsdager') for year in range(1900, 1920)]

View File

@ -0,0 +1,8 @@
def is_leap_year ( year ):
if year % 400 == 0:
return True
elif year % 100 == 0:
return False
elif year % 4 == 0:
return True
return False

View File

@ -1,4 +1,4 @@
# from common.inputChecking.choiceInput import choiceInput from common.inputChecking.boolInput import boolInput
from os import get_terminal_size, system from os import get_terminal_size, system
from math import ceil from math import ceil
@ -37,13 +37,6 @@ class Card:
{' ' if self.id != 10 else ''}{self.num} {' ' if self.id != 10 else ''}{self.num}
""" """
def emptyCard():
result = Card(1,'spade')
result.num = '?'
result.sym = '?'
return result
class CardHandler: class CardHandler:
def __init__(self, cards, printSep=5, aceValue = 1): def __init__(self, cards, printSep=5, aceValue = 1):
self.cards = cards self.cards = cards
@ -53,6 +46,11 @@ class CardHandler:
def addCard(self, card): def addCard(self, card):
self.cards.append(card) self.cards.append(card)
def generateNewCard(self):
cardTypes = range(1,14)
cardColors = ['spade', 'heart', 'diamond', 'club']
self.addCard(Card(random.choice(cardTypes), random.choice(cardColors)))
def _concatenateCards(self, cards): def _concatenateCards(self, cards):
""" """
Concatenate several card objects into an list of sublists Concatenate several card objects into an list of sublists
@ -71,7 +69,7 @@ class CardHandler:
def printCards(self, cardsPerLine): def printCards(self, cardsPerLine):
""" """
Print cardsPerLine cards per line, horizontally aligned Print <cardsPerLine> cards per line, horizontally aligned
""" """
splitCards =[[] for _ in range(ceil(len(self.cards)/cardsPerLine))] splitCards =[[] for _ in range(ceil(len(self.cards)/cardsPerLine))]
@ -100,7 +98,7 @@ class CardHandler:
@property @property
def cardSum(self): def cardSum(self):
values = { cardValues = {
'A': self.aceValue, 'A': self.aceValue,
'2': 2, '2': 2,
'3': 3, '3': 3,
@ -115,46 +113,14 @@ class CardHandler:
'Q': 10, 'Q': 10,
'K': 10 'K': 10
} }
return sum([values[card.num] for card in self.cards]) return sum([cardValues[card.num] for card in self.cards])
def animator(self):
while True:
frame = 1
yield frame
# def emptyCard():
# ┌─────────┐ result = Card(1,'spade')
# │? │ result.num = '?'
# │ │ result.sym = '?'
# │ │ return result
# │ ? │
# │ │
# │ │
# │ ?│
# └─────────┘
#
#
# ┌────────┐
# │? │
# │ │
# │ │
# │ ? │
# │ │
# │ │
# │ ?│
# └────────┘
#
#
# ┌───────┐
# │? │
# │ │
# │ │
# │ ? │
# │ │
# │ │
# │ ?│
# └───────┘
#
class Blackjack: class Blackjack:
@ -162,31 +128,87 @@ class Blackjack:
self.handler = CardHandler([]) self.handler = CardHandler([])
self.emptyHandler = CardHandler([emptyCard(), emptyCard()]) self.emptyHandler = CardHandler([emptyCard(), emptyCard()])
self.dealerHandler = CardHandler([]) self.dealerHandler = CardHandler([])
self.gameResults = [0,0]
self.reset() self.reset()
def generateNewCard(self):
cardTypes = range(1,14)
cardColors = ['spade', 'heart', 'diamond', 'club']
return Card(random.choice(cardTypes), random.choice(cardColors))
def generateNewCards(self): def generateNewCards(self):
self.dealerHandler.cards = [ self.generateNewCard() for _ in range(2) ] self.dealerHandler.cards = []
self.handler.cards = [ self.generateNewCard() for _ in range(2) ] self.handler.cards = []
for _ in range(2):
self.dealerHandler.generateNewCard()
self.handler.generateNewCard()
def determineAceValue(self): def determineAceValue(self):
if self.dealerHandler.cardSum <= 9: containsAce = any([True for card in self.dealerHandler.cards if card.id == 1])
self.handler.aceValue = 13 if self.dealerHandler.cardSum < 10 and containsAce: # 9 + 1 = 10, 9 + 13 = 22
self.dealerHandler.aceValue = self.handler.aceValue = 13
else:
self.dealerHandler.aceValue = self.handler.aceValue = 1
def reset(self): def reset(self):
self.generateNewCards() self.generateNewCards()
self.determineAceValue() self.determineAceValue()
def printCards(self): def youWin(self):
self.gameResults[0] += 1
print("""
__ __
/\ \ /\ \ __
\ `\`\\\\/'/ ___ __ __ __ __ __/\_\ ___
`\ `\ /' / __`\/\ \/\ \ /\ \/\ \/\ \/\ \ /' _ `\
`\ \ \/\ \L\ \ \ \_\ \\ \\ \ \_/ \_/ \ \ \/\ \/\ \
\ \_\ \____/\ \____/ \ \___x___/'\ \_\ \_\ \_\\
\/_/\/___/ \/___/ \/__//__/ \/_/\/_/\/_/
""")
def youLose(self):
self.gameResults[1] += 1
print("""
__ __ ___
/\ \ /\ \ /\_ \
\ `\`\\\\/'/ ___ __ __ \//\ \ ___ ____ __
`\ `\ /' / __`\/\ \/\ \ \ \ \ / __`\ /',__\ /'__`\
`\ \ \/\ \L\ \ \ \_\ \ \_\ \_/\ \L\ \/\__, `\/\ __/
\ \_\ \____/\ \____/ /\____\ \____/\/\____/\ \____\\
\/_/\/___/ \/___/ \/____/\/___/ \/___/ \/____/
""")
def gameOver(self):
system('clear')
print('\nDEALERS CARDS\n')
self.dealerHandler.safePrintCards()
print('\nYOUR CARDS\n')
self.handler.safePrintCards() self.handler.safePrintCards()
print()
print('Ace value is', self.handler.aceValue)
cardSumNotExceeds21 = self.handler.cardSum < 22
playerSumBiggerThanDealer = self.handler.cardSum > self.dealerHandler.cardSum
if cardSumNotExceeds21 and playerSumBiggerThanDealer:
self.youWin()
else:
self.youLose()
print()
print(f'Wins: {self.gameResults[0]} Losses: {self.gameResults[1]}')
if boolInput(
'Do you want to play again? [y/n]: ',
yesNoLetters=['y','n'],
error=''
):
self.reset()
else:
exit(0)
def checkIfLost(self):
if self.handler.cardSum > 21:
self.gameOver()
return True
return False
def update(self): def update(self):
system('clear') system('clear')
print('\nDEALERS CARDS\n') print('\nDEALERS CARDS\n')
@ -194,9 +216,14 @@ class Blackjack:
print('\nYOUR CARDS\n') print('\nYOUR CARDS\n')
self.handler.safePrintCards() self.handler.safePrintCards()
print() print()
print('Ace value is', self.handler.aceValue)
print()
input('Continue?') if not self.checkIfLost():
self.handler.addCard(self.generateNewCard()) if not boolInput('Continue? [y/n]: ', yesNoLetters=('y','n')):
self.gameOver()
return
self.handler.generateNewCard()
def loop(self): def loop(self):
while True: while True:

131
Exercise 5/task8.py Normal file
View File

@ -0,0 +1,131 @@
from common.inputChecking.typeCheck import validateInput as validateTypeInput
from common.inputChecking.boolInput import validateInput as validateBoolInput
from json import dumps as serialize
from json import loads as unserialize
from os.path import isfile
class ExitError(Exception):
pass
def boolInput(question, yesNoLetters=('j','n'), exitKeyword='hade'):
yesLetters = [yesNoLetters[0], yesNoLetters[0].capitalize()]
while True:
answer = input(question)
if answer == exitKeyword:
raise ExitError
if validateBoolInput(answer, yesNoLetters):
return answer in yesLetters
def inputTypeCheck(question, type, exitKeyword='hade'):
while True:
userInput = input(question)
if userInput == exitKeyword:
raise ExitError
if validateTypeInput(userInput, type):
return userInput
class DataSchema:
def __init__(
self,
genderIsMale = None,
age = None,
hasMultipleSubjects = None,
hasITGK = None,
avgStudyTime = None
):
self.genderIsMale = genderIsMale
self.age = age
self.hasMultipleSubjects = hasMultipleSubjects
self.hasITGK = hasITGK
self.avgStudyTime = avgStudyTime
class Survey:
def __init__(self, savePath='./results.json', resultsPath=''):
self.savePath = savePath
if resultsPath != '':
self.resultsPath = resultsPath
self.loadResults()
results = []
def loadResults(self):
try:
file = open(self.resultsPath, 'r')
dicts = unserialize(file.read())
self.results = [DataSchema(**dict) for dict in dicts]
except FileNotFoundError:
print('Fant ikke undersøkelsesdata ved', self.resultsPath)
except Exception as e:
print('Noe gikk galt under lasting av undersøkelsesdata')
print(e)
def printStats(self):
print('Resultat av undersøkelse!')
men = women = multipleChoice = itgk = studyHours = 0
for result in self.results:
if result.genderIsMale:
men += 1
else:
women += 1
multipleChoice += 1 if result.hasMultipleSubjects else 0
itgk += 1 if result.hasITGK else 0
studyHours += int(result.avgStudyTime)
print("Antall kvinner:", women)
print("Antall menn:", men)
print("Antall personer som tar fag:", multipleChoice)
print("Antall personer som tar ITGK:", itgk)
print("Antall timer i snitt brukt på lekser:", studyHours/len(self.results))
def saveResults(self):
serializedResults = serialize([result.__dict__ for result in self.results])
# Check whether file exists
try:
assert not isfile(self.savePath)
except:
if not boolInput(f'Det eksisterer allerede en fil ved {self.savePath}\nVil du overskrive filen? [j/n]: '):
return
# Try to write data to file.
try:
file = open(self.savePath, "w")
file.write(serializedResults)
file.close()
except:
if boolInput('Noe gikk galt under lagring av data. Vil du prøve igjen? [j/n]: '):
self.saveResults()
def end(self):
if len(self.results) > 0:
self.printStats()
self.saveResults()
exit(0)
def update(self):
schema = DataSchema()
print('Velkommen til spørreundersøkelsen!\n')
try:
schema.genderIsMale = boolInput('Hvilket kjønn er du? [f/m]: ', yesNoLetters=('m','f'))
schema.age = inputTypeCheck('Hvor gammel er du?: ', int)
schema.hasMultipleSubjects = boolInput('Tar du et eller flere fag? [j/n]: ')
hasITGKQuestion = 'Tar du ITGK? [j/n]: ' if int(schema.age) < 22 else 'Tar du virkelig ITGK? [j/n]: '
schema.hasITGK = boolInput(hasITGKQuestion)
schema.avgStudyTime = inputTypeCheck('Hvor mange timer bruker du daglig (i snitt) på lekser?: ', float)
self.results.append(schema)
except ExitError:
self.end()
print()
def loop(self):
while True:
self.update()
if __name__ == "__main__":
survey = Survey(resultsPath='./results.json')
survey.loop()