Ferdig med oppgave 2 uke 2
This commit is contained in:
parent
87636e526b
commit
ec970373aa
24
uke2.py
24
uke2.py
@ -1,3 +1,5 @@
|
|||||||
|
import re
|
||||||
|
|
||||||
# Task 1
|
# Task 1
|
||||||
class Party:
|
class Party:
|
||||||
def __init__(self, fylke, code, name, voters, votes):
|
def __init__(self, fylke, code, name, voters, votes):
|
||||||
@ -45,8 +47,30 @@ def table_from_votes(file_path, num=None):
|
|||||||
break;
|
break;
|
||||||
return(t)
|
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():
|
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'))
|
||||||
|
|
||||||
|
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__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
Loading…
Reference in New Issue
Block a user