SHARE
TWEET

Untitled

a guest Mar 26th, 2020 60 in 24 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. CHAR_TO_INT_DICT = {
  2.                     "A": 10, "B": 11, "C": 12, "D": 13, "E": 14, "F": 15,
  3.                     "G": 16, "H": 17, "I": 18, "J": 19, "K": 20, "L": 21,
  4.                     "M": 22, "N": 23, "O": 24, "P": 25, "Q": 26, "R": 27,
  5.                     "S": 28, "T": 29, "U": 30, "V": 31, "W": 32, "X": 33,
  6.                     "Y": 34, "Z": 35,
  7.                     }
  8.  
  9. INT_TO_CHAR_DICT = {
  10.                     10: "A", 11: "B", 12: "C", 13: "D", 14: "E", 15: "F",
  11.                     16: "G", 17: "H", 18: "I", 19: "J", 20: "K", 21: "L",
  12.                     22: "M", 23: "N", 24: "O", 25: "P", 26: "Q", 27: "R",
  13.                     28: "S", 29: "T", 30: "U", 31: "V", 32: "W", 33: "X",
  14.                     34: "Y", 35: "Z",
  15.                     }
  16.  
  17. def change_number_system_to_decimal(string_of_number: str, base: int):
  18.     string_of_number = string_of_number.upper()  # unify number eg.Ac3 -> AC3
  19.     # change "AC13" into [10, 12, 1, 3]    
  20.     digitized_number = digitize_number(string_of_number)  
  21.     decimal_value = 0
  22.  
  23.     for int_of_char in digitized_number:
  24.         decimal_value *= base
  25.         decimal_value += int_of_char
  26.  
  27.     return decimal_value
  28.  
  29. # change "abc123" into [10, 11, 12, 1, 2, 3]
  30. def digitize_number(string_of_number: str):  
  31.     digitized_number = [None] * len(string_of_number)
  32.     for idx, chr in enumerate(string_of_number):
  33.         try:    
  34.             digitized_number[idx] = int(chr)
  35.         except ValueError:
  36.             digitized_number[idx] = CHAR_TO_INT_DICT[chr]
  37.     return digitized_number
  38.  
  39. def compress_number_to_string_number(array_of_number):
  40.     compressed_number = ""
  41.     for idx, chr in enumerate(array_of_number):
  42.         if chr < 10:
  43.             compressed_number += str(chr)
  44.         else:
  45.             compressed_number += INT_TO_CHAR_DICT[chr]
  46.     return compressed_number
  47.  
  48. def change_number_to_another_system(number_in_decimal: int, target_base: int):
  49.     new_number_digits = []
  50.     while number_in_decimal > target_base:
  51.         new_number_digits.append(number_in_decimal % target_base)
  52.         number_in_decimal //= target_base
  53.     new_number_digits.append(number_in_decimal)
  54.     new_number = compress_number_to_string_number(new_number_digits)[::-1]
  55.     return new_number
  56.  
  57.  
  58. string_of_number = input("First number: ")  # abc123 or ABC123
  59. base_system = int(input("Number system (2 <= int <= 26): "))  #   16
  60. target_system = int(input("Target number system (2 <= int <= 26): "))  # 17
  61.  
  62. number_in_decimal = change_number_system_to_decimal(string_of_number, base_system)
  63.  
  64. print(change_number_to_another_system(number_in_decimal, target_system))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top