uke2 2024
This commit is contained in:
parent
081483ed4f
commit
366fe4df73
33
uke2.py
33
uke2.py
@ -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 = []
|
|
||||||
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')
|
def print_encoding_info(char):
|
||||||
t += ("-"*len(t)+"\n")
|
char_int = int.from_bytes(bytes(char, 'utf-8'))
|
||||||
for n in friends:
|
print(f"Character: '{char}'")
|
||||||
t += (f'{n[0]:^10}-{n[1]:^10}\n')
|
if char_int < 129:
|
||||||
return(t)
|
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():
|
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()
|
Loading…
Reference in New Issue
Block a user