Commit 79936f48 authored by Joachim Birkelund Fredheim's avatar Joachim Birkelund Fredheim
Browse files

wip

parent b6843003
print("Arbeidsdager!")
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
print("Oppgave a)")
#TODO løs oppgave a her:
def weekday_newyear(year):
days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
counter = -1
for yr in range(1900,year+1):
if is_leap_year(yr-1):
counter+=2
else:
counter+=1
return days[counter%7]
for i in range(1900,1920):
print(i, weekday_newyear(i))
print("Oppgave b)")
#TODO løs oppgave b her:
def is_workday(day):
return True if day<5 else False
print(5, is_workday(5))
print(2, is_workday(2))
print("Oppgave c)")
#TODO løs oppgave c her:
def workdays_in_year(year):
days = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
cnt = 365 if not is_leap_year(year) else 366
starts = days.index(weekday_newyear(year))
workdays = 0
for i in range(0,cnt):
if is_workday((starts+i)%7):
workdays+=1
return workdays
for i in range(1900,1920):
print(i, "har", workdays_in_year(i),"arbeidsdager")
\ No newline at end of file
from hashlib import new
import random
from typing import Optional
deck = [] #Globals capitalised in accordance with pep8
def generate_deck():
global deck
cards = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13"] #Ace through king
suits = ["S", "D", "C", "H"] #Spades, Diamonds, Clubs and Hearts respectively
deck = []
for card in cards:
deck.extend([f"{s}{card}" for s in suits])
print("Not quite Blackjack!")
def draw_card(i: Optional[int] = 1):
"""Draws and returns a random card from the deck, removing it from the deck in the process"""
cards = []
for j in range(i):
card = random.choice(deck)
deck.remove(card)
cards.append(card)
return cards
def calculate_score(hand=None):
score = 0
for card in hand:
if int(card[1:])>9:
score+=10
elif int(card[1:])==1:
score += 11 if (score+11)<=21 else 1
else:
score += int(card[1:])
return score
#TODO løs oppgaven her:
def blackjack():
dealers_hand = []
your_hand = []
dealers_score = 0
your_score = 0
while True:
if False:
print(deck)
print(dealers_hand)
print(your_hand)
if len(dealers_hand)==0 and len(your_hand)==0:
dealers_hand = []
your_hand = []
dealers_hand.extend(draw_card())
print(f"Dealers cards are {dealers_hand[0]} and ?")
dealers_score = calculate_score(dealers_hand)
print(f"Their score is: {dealers_score}")
your_hand.extend(draw_card(2))
your_score = calculate_score(your_hand)
print(f"Your score is: {your_score}")
if your_score==21 & dealers_score!=21:
print("Winner Winner Chicken Dinner!")
exit()
elif your_score>21:
print("Bust! You lost.")
exit()
else:
new_card = input("Do you want another card? (Y/N) ")
if new_card.lower() == 'y':
new_card = draw_card().pop()
your_hand.append(new_card)
print(f"You drew {new_card}")
your_score = calculate_score(your_hand)
print(f"Your score is: {your_score}")
elif new_card.lower() == 'n':
new_card = draw_card().pop()
dealers_hand.append(new_card)
dealers_score = calculate_score(dealers_hand)
print(f"Dealers score is: {dealers_score}")
if dealers_score > your_score:
print("You lost")
exit()
elif dealers_score == your_score:
print("Tie!")
exit()
else:
print("You win")
exit()
if __name__ == '__main__':
generate_deck()
blackjack()
\ No newline at end of file
from math import sqrt
print("Varierte funksjoner")
print("Oppgave a)")
#TODO løs oppgave a her:
def divisable(a, b):
return False if b==0 else True if a%b==0 else False
# TESTKODE FOR divisable
print("Er",divisable(10,3), "skal være false")
print("Er",divisable(10,5), "skal være true")
print("Oppgave b)")
#TODO løs oppgave b her:
def isPrime(a):
for b in range(2,a):
if divisable(a, b):
return False
return True
# TESTKODE FOR isPrime
print("Er",isPrime(11), "skal være true")
print("Er",isPrime(15), "skal være false")
print("Oppgave c)")
#TODO løs oppgave c her:
def isPrime(a):
for b in range(2,(round(sqrt(a)+0.5))):
if b%2==0:
continue
if divisable(a, b):
return False
return True
print("Er",isPrime(11), "skal være true")
print("Er",isPrime(15), "skal være false")
\ No newline at end of file
from math import e
print("Sekantmetoden!")
print("Oppgave a)")
#TODO løs oppgave a her:
def f(x):
return (((x-12)*e**(x/2))-8*((x+2)**2))
def g(x):
return -x -(2*(x**2))-(5*(x**3))+(6*(x**4))
print(f(0))
print(g(1))
print("Oppgave b)")
#TODO løs oppgave b her:
def differentiate(x_k, x_k1, func):
# print(x_k, x_k1)
return (func(x_k)-func(x_k1))/(x_k - x_k1)
print(differentiate(9,10,f))
print("Oppgave c)")
#TODO løs oppgave c her:
def secant_method(x0, x1, func, tol):
while abs(x1-x0) > tol:
(x1, x0) = (x0 - func(x0)/differentiate(x0,x1,func),x1)
return x1, func(x1)
print(secant_method(11,13,f,0.00001))
print(secant_method(1,2,g,0.00001))
print(secant_method(0,1,g,0.00001))
\ No newline at end of file
print("litt sjakk!")
print("Denne løsningen krever Python 3.10 som nettopp kom ut siden den bruker den nye pattern matching funksjonen")
print("OBS:\nDenne løsningen krever Python 3.10 som nettopp kom ut siden den bruker den nye pattern matching funksjonen")
print("Oppgave a)")
#TODO løs oppgave a her:
def make_board(board_string):
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment