Commit f1c093b3 authored by Børge Haugset's avatar Børge Haugset
Browse files

fikset noen småfeil

parent f62e4035
# a # Oppgave 3.1
def calculate_percentage(votes): def calculate_percentage(votes):
dem = int(votes[0])*100/(int(votes[0]) + int(votes[1])) dem = int(votes[0])*100/(int(votes[0]) + int(votes[1]))
rep = 100 - dem rep = 100 - dem
return round(dem,2),round(rep,2) return round(dem,2),round(rep,2)
# b
# Oppgave 3.2
def return_total_votes(state_votes): def return_total_votes(state_votes):
dem, rep = 0,0 dem, rep = 0,0
for i in state_votes: for i in state_votes:
...@@ -12,7 +13,8 @@ def return_total_votes(state_votes): ...@@ -12,7 +13,8 @@ def return_total_votes(state_votes):
rep += int(i[1]) rep += int(i[1])
return (dem,rep) return (dem,rep)
# c
# Oppgave 3.3
def update_state(dict, state, demvotes, repvotes): def update_state(dict, state, demvotes, repvotes):
if dict.get(state,0) != 0: if dict.get(state,0) != 0:
dict[state].append((demvotes,repvotes)) dict[state].append((demvotes,repvotes))
...@@ -20,7 +22,8 @@ def update_state(dict, state, demvotes, repvotes): ...@@ -20,7 +22,8 @@ def update_state(dict, state, demvotes, repvotes):
dict[state] = [(demvotes, repvotes)] dict[state] = [(demvotes, repvotes)]
return dict return dict
# d
# Oppgave 3.4
def read_from_file(): def read_from_file():
dikt = {} dikt = {}
with open('votes.txt','r') as f: with open('votes.txt','r') as f:
...@@ -34,14 +37,16 @@ def read_from_file(): ...@@ -34,14 +37,16 @@ def read_from_file():
linje = f.readline().strip() linje = f.readline().strip()
return dikt return dikt
# e
# Oppgave 3.5
def get_ev_for_state(state): def get_ev_for_state(state):
electoral_votes = [["Arizona", 11], ["Nevada", 6], ["Pennsylvania", 20], ["Georgia", 16]] electoral_votes = [["Arizona", 11], ["Nevada", 6], ["Pennsylvania", 20], ["Georgia", 16]]
for el in electoral_votes: for el in electoral_votes:
if el[0] == state: if el[0] == state:
return el[1] return el[1]
# f
# Oppgave 3.6
def get_actual_ev(state, dempercent, reppercent): def get_actual_ev(state, dempercent, reppercent):
ev = get_ev_for_state(state) ev = get_ev_for_state(state)
if dempercent > reppercent: if dempercent > reppercent:
...@@ -49,14 +54,19 @@ def get_actual_ev(state, dempercent, reppercent): ...@@ -49,14 +54,19 @@ def get_actual_ev(state, dempercent, reppercent):
else: else:
return (0,ev) return (0,ev)
# g
# Oppgave 3.7
def get_actual_ev_fair(state, dempercent, reppercent): def get_actual_ev_fair(state, dempercent, reppercent):
ev = get_ev_for_state(state) ev = get_ev_for_state(state)
dem_ev = round(dempercent*ev/100) dem_ev = round(dempercent*ev/100)
return (dem_ev,ev-dem_ev) return (dem_ev,ev-dem_ev)
# h
# Oppgave 3.8
# Hjelpefunksjon. Dere trenger ikke å lage en slik, men viser her at
# jeg har kjørt ut en del av koden til sin egen funksjon for å forenkle
# koden i hovedoppgaven. Se at den brukes under.
def find_percent(state,votes): def find_percent(state,votes):
dem,rep = 0,0 dem,rep = 0,0
for el in votes[state]: for el in votes[state]:
......
Supports Markdown
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