uke2 2024

This commit is contained in:
Trygve 2024-09-20 21:08:35 +02:00
parent 081483ed4f
commit 366fe4df73

35
uke2.py
View File

@ -57,30 +57,27 @@ def table_from_votes(file_path, num=None):
return(t) return(t)
# Task 2 # Task 2
def get_friends(text):
friends = [] def print_encoding_info(char):
for s in text: char_int = int.from_bytes(bytes(char, 'utf-8'))
names = re.findall(r'[A-Z]\w*', s) print(f"Character: '{char}'")
if len(names) != 2: if char_int < 129:
raise ValueError('String does not contain excactly two capitalized words') print(f"- ASCII representation: {format(char_int, 'b')}")
friends.append(names) else:
print("- Not in ASCII range")
t = '{:^20}\n'.format('Venner') print(f"- UTF-8: {' '.join(format(x, 'b') for x in bytearray(char, 'utf-8'))}", end='')
t += ("-"*len(t)+"\n") print(f' ({len(bytearray(char, "utf-8"))} bytes)')
for n in friends: print('\n')
t += (f'{n[0]:^10}-{n[1]:^10}\n')
return(t) def print_encoding_info_list(char_list):
for char in char_list:
print_encoding_info(char)
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'))
print(table_from_votes('2021-09-14_party distribution_1_st_2021.csv', 3)) print(table_from_votes('2021-09-14_party distribution_1_st_2021.csv', 3))
text = [ print_encoding_info_list(["2", "$", "å"])
'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()