Compare commits
No commits in common. "d48d576891bfa9a98734ae8de1b1bc93e931f2ae" and "87636e526b89bc4e947101b72fc843d216e9779f" have entirely different histories.
d48d576891
...
87636e526b
40
uke2.py
40
uke2.py
@ -1,5 +1,3 @@
|
||||
import re
|
||||
|
||||
# Task 1
|
||||
class Party:
|
||||
def __init__(self, fylke, code, name, voters, votes):
|
||||
@ -27,15 +25,9 @@ def table_from_votes(file_path, num=None):
|
||||
# Combine all the local results into a list of national party results
|
||||
p_codes = []
|
||||
parties = []
|
||||
total_votes = 0
|
||||
# Not all parties are in all distrcts. Therofore i need to get the total amount of voters from a party that is everywhere
|
||||
for p in local_parties:
|
||||
if p.code == 'A':
|
||||
total_votes += p.voters
|
||||
# Put all the kommuneparties into fylker
|
||||
for p in local_parties:
|
||||
if p.code not in p_codes:
|
||||
party = Party('Alle', p.code, p.name, total_votes, p.votes)
|
||||
party = Party('Alle', p.code, p.name, p.voters, p.votes)
|
||||
parties.append(party)
|
||||
p_codes.append(p.code)
|
||||
else:
|
||||
@ -45,42 +37,16 @@ def table_from_votes(file_path, num=None):
|
||||
parties.sort(key=lambda x: x.percent(), reverse=True)
|
||||
|
||||
# Create the table
|
||||
t = '{:^8}|{:^8}|{:^10}\n'.format('Parti', 'Prosent', 'Stemmer')
|
||||
t = '{:^25}|{:^10}|{:^10}\n'.format('Parti', 'Prosent', 'Stemmer')
|
||||
t += ("-"*len(t)+"\n")
|
||||
for n, p in enumerate(parties):
|
||||
if p.percent() > 4.0:
|
||||
t += (f'\033[1m{p.code:^8}|{p.percent():^8.2f}|{p.votes:^10}\033[0m\n')
|
||||
else:
|
||||
t += (f'{p.code:^8}|{p.percent():^8.2f}|{p.votes:^10}\n')
|
||||
t += (f'{p.code:^25}|{p.percent():^10.2f}|{p.votes:^10}\n')
|
||||
if n == num:
|
||||
break;
|
||||
return(t)
|
||||
|
||||
# Task 2
|
||||
def get_friends(text):
|
||||
friends = []
|
||||
for s in text:
|
||||
names = re.findall(r'[A-Z]\w*', s)
|
||||
if len(names) != 2:
|
||||
raise ValueError('String does not contain excactly two capitalized words')
|
||||
friends.append(names)
|
||||
|
||||
t = '{:^20}\n'.format('Venner')
|
||||
t += ("-"*len(t)+"\n")
|
||||
for n in friends:
|
||||
t += (f'{n[0]:^10}-{n[1]:^10}\n')
|
||||
return(t)
|
||||
|
||||
def main():
|
||||
print(table_from_votes('2021-09-14_party distribution_1_st_2021.csv'))
|
||||
print(table_from_votes('2021-09-14_party distribution_1_st_2021.csv', 3))
|
||||
|
||||
text = [
|
||||
'Ali and Per and friends.',
|
||||
'Kari and Joe know each other.',
|
||||
'James has known Peter since school days.'
|
||||
]
|
||||
print(get_friends(text))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
Loading…
Reference in New Issue
Block a user