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

eksamen

parent c3a11326
# a
def calculate_percentage(votes):
dem = int(votes[0])*100/(int(votes[0]) + int(votes[1]))
rep = 100 - dem
return round(dem,2),round(rep,2)
# b
def return_total_votes(state_votes):
dem, rep = 0,0
for i in state_votes:
dem += int(i[0])
rep += int(i[1])
return (dem,rep)
# c
def update_state(dict, state, demvotes, repvotes):
if dict.get(state,0) != 0:
dict[state].append((demvotes,repvotes))
else:
dict[state] = [(demvotes, repvotes)]
return dict
# d
def read_from_file():
dikt = {}
with open('votes.txt','r') as f:
linje = f.readline()
while(linje):
liste = linje.split(',')
try:
dikt = update_state(dikt, liste[0], liste[1], liste[2])
except:
print(f'Linje kunne ikke tolkes: {linje}')
linje = f.readline().strip()
return dikt
# e
def get_ev_for_state(state):
electoral_votes = [["Arizona", 11], ["Nevada", 6], ["Pennsylvania", 20], ["Georgia", 16]]
for el in electoral_votes:
if el[0] == state:
return el[1]
# f
def get_actual_ev(state, dempercent, reppercent):
ev = get_ev_for_state(state)
if dempercent > reppercent:
return (ev,0)
else:
return (0,ev)
# g
def get_actual_ev_fair(state, dempercent, reppercent):
ev = get_ev_for_state(state)
dem_ev = round(dempercent*ev/100)
return (dem_ev,ev-dem_ev)
# h
def find_percent(state,votes):
dem,rep = 0,0
for el in votes[state]:
dem += int(el[0])
rep += int(el[1])
prosent_dem = dem*100/(dem+rep)
return (round(prosent_dem,2),round(100-prosent_dem,2))
def print_nation_results(votes):
dem,rep = 0,0
ev_norm_dem,ev_norm_rep = 0,0
ev_fair_dem,ev_fair_rep = 0,0
for key in votes:
stemmer = return_total_votes(votes[key])
dem += stemmer[0]
rep += stemmer[1]
prosent = find_percent(key,votes)
pro_pr_parti = get_actual_ev(key, prosent[0], prosent[1])
ev_norm_dem += pro_pr_parti[0]
ev_norm_rep += pro_pr_parti[1]
pro_pr_parti = get_actual_ev_fair(key, prosent[0], prosent[1])
ev_fair_dem += pro_pr_parti[0]
ev_fair_rep += pro_pr_parti[1]
print(f'Dems got {dem} votes, reps got {rep}')
print(f'With wta, dems got {ev_norm_dem} while reps got {ev_norm_rep} electoral votes')
print(f'With fair, dems got {ev_fair_dem} while reps got {ev_fair_rep} electoral votes')
print(f"Electoral votes for Nevada should be 6: {get_ev_for_state('Nevada')}.")
print(get_actual_ev('Nevada', 50.1,49.9))
fair = get_actual_ev_fair("Nevada", 50.1, 49.9)
print(f"Hvis vi fordeler etter prosent skal begge få 3: {fair}.")
votes = read_from_file()
print_nation_results(votes)
Arizona,66,103
Nevada,86,66
Arizona,26,145
Nevada, 57
Georgia,57,83
Georgia,70,120
Arizona,38,89
Nevada,90,93
Georgia,95,113
Georgia,31,104
Pennsylvania,32,125
Nevada,17,106
Pennsylvania,27,118
Georgia,52,113
Arizona,13,87
Arizona,37,102
Pennsylvania,13,65
Nevada,14,69
Georgia,65,86
Nevada,17,140
Arizona,83,135
Arizona,24,79
Nevada,65,97
Georgia,71,106
Arizona,43,141
Arizona,65,85
Georgia,40,132
Georgia,42,87
Arizona,17,92
Nevada,47,81
Arizona,70,109
Pennsylvania,56,90
Nevada,71,122
Georgia,62,136
Nevada,65,106
Nevada,78,81
Arizona,37,117
Arizona,58,103
Nevada,31,148
Pennsylvania,26,72
Georgia,56,107
Arizona,46,113
Nevada,26,83
Nevada,74,102
Pennsylvania,13,107
Nevada,73,121
Pennsylvania,65,108
Georgia,20,77
Arizona,36,61
Georgia,68,97
Georgia,60,47
Georgia,114,27
Pennsylvania,116,72
Arizona,139,42
Georgia,106,24
Pennsylvania,94,40
Pennsylvania,113,41
Pennsylvania,104,84
Georgia,107,87
Nevada,103,81
Nevada,131,81
Georgia,91,24
Nevada,88,82
Nevada,148,94
Nevada,135,56
Arizona,143,10
Arizona,109,13
Georgia,104,11
Georgia,62,90
Nevada,116,94
Nevada,83,55
Georgia,62,88
Georgia,111,83
Georgia,144,81
Arizona,117,94
Nevada,103,59
Georgia,85,50
Nevada,125,13
Georgia,147,11
Pennsylvania,88,20
Arizona,94,64
Georgia,66,63
Georgia,129,29
Nevada,113,92
Arizona,86,36
Georgia,118,19
Pennsylvania,77,39
Nevada,138,88
Arizona,79,61
Nevada,98,47
Pennsylvania,116,63
Nevada,130,75
Arizona,115,80
Pennsylvania,145,38
Georgia,104,86
Nevada,81,20
Arizona,100,14
Arizona,133,83
Georgia,70,91
\ No newline at end of file
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