uke2 2024
This commit is contained in:
		
							parent
							
								
									081483ed4f
								
							
						
					
					
						commit
						366fe4df73
					
				
							
								
								
									
										35
									
								
								uke2.py
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								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() | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Trygve
						Trygve