From ec970373aa73c1091ff9635dddf63a90d760e05f Mon Sep 17 00:00:00 2001 From: Trygve Date: Thu, 21 Sep 2023 16:46:23 +0200 Subject: [PATCH] Ferdig med oppgave 2 uke 2 --- uke2.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/uke2.py b/uke2.py index 49ddd1b..7f96c14 100644 --- a/uke2.py +++ b/uke2.py @@ -1,3 +1,5 @@ +import re + # Task 1 class Party: def __init__(self, fylke, code, name, voters, votes): @@ -45,8 +47,30 @@ def table_from_votes(file_path, num=None): 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')) + 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() \ No newline at end of file