From 366fe4df733f9787f91dfc87e78618dabfdcf680 Mon Sep 17 00:00:00 2001 From: Trygve Date: Fri, 20 Sep 2024 21:08:35 +0200 Subject: [PATCH] uke2 2024 --- uke2.py | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/uke2.py b/uke2.py index 7090b4b..3528e06 100644 --- a/uke2.py +++ b/uke2.py @@ -57,30 +57,27 @@ def table_from_votes(file_path, num=None): 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 print_encoding_info(char): + char_int = int.from_bytes(bytes(char, 'utf-8')) + print(f"Character: '{char}'") + if char_int < 129: + print(f"- ASCII representation: {format(char_int, 'b')}") + else: + print("- Not in ASCII range") + print(f"- UTF-8: {' '.join(format(x, 'b') for x in bytearray(char, 'utf-8'))}", end='') + print(f' ({len(bytearray(char, "utf-8"))} bytes)') + print('\n') + +def print_encoding_info_list(char_list): + for char in char_list: + print_encoding_info(char) 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)) + print_encoding_info_list(["2", "$", "å"]) if __name__ == '__main__': main() \ No newline at end of file