Дейвид обнови решението на 29.10.2022 18:07 (преди над 2 години)
+'''
+ Homework 2 Python
+ Created by David Kamenov
+ First version; I will upload a new version with fixed some code quality issues
+'''
+
+# dictionary for function nums_to_text and text_to_nums
+phone_dic = {
+ ' ': 0,
+ 'A': 2,
+ 'B': 22,
+ 'C': 222,
+ 'D': 3,
+ 'E': 33,
+ 'F': 333,
+ 'G': 4,
+ 'H': 44,
+ 'I': 444,
+ 'J': 5,
+ 'K': 55,
+ 'L': 555,
+ 'M': 6,
+ 'N': 66,
+ 'O': 666,
+ 'P': 7,
+ 'Q': 77,
+ 'R': 777,
+ 'S': 7777,
+ 'T': 8,
+ 'U': 88,
+ 'V': 888,
+ 'W': 9,
+ 'X': 99,
+ 'Y': 999,
+ 'Z': 9999,
+}
+counter = 1
+
+# dictionary for function text_to_angle
+rotate_phone_dic = {
+ 1: 30,
+ 2: 60,
+ 3: 90,
+ 4: 120,
+ 5: 150,
+ 6: 180,
+ 7: 210,
+ 8: 240,
+ 9: 270,
+ 0: 300,
+ None: 330
+}
+
+# dictionary for function angle_to_nums
+not_round_degrees_dic = {
+ 1: list(range(16, 45)),
+ 2: list(range(46, 75)),
+ 3: list(range(76, 105)),
+ 4: list(range(106, 135)),
+ 5: list(range(136, 165)),
+ 6: list(range(166, 195)),
+ 7: list(range(196, 225)),
+ 8: list(range(226, 255)),
+ 9: list(range(256, 285)),
+ 0: list(range(286, 315))
+}
+
+
+def get_each_letter(i, digits_list, temp_num=0, text=''):
+ global counter
+
+ # if the key pressed on the phone is 7 or 9 it has 4 letters otherwise it has 3
+ if digits_list[i - 1] % 10 == 7 or digits_list[i - 1] % 10 == 9:
+ counter %= 4
+ if counter == 0:
+ counter = 4
+ else:
+ counter %= 3
+ if counter == 0:
+ counter = 3
+
+ # make the whole number from numerous digits
+ for j in range(0, counter):
+ temp_num = temp_num * 10 + digits_list[i - 1]
+ counter = 1
+
+ # Get the value of the key from the whole number
+ for key in phone_dic.keys():
+ if phone_dic[key] == temp_num:
+ text += key
+
+ return text
+
+
+def nums_to_text(digits_list):
+ # I could not find any other way except from using global counter because its value has to change in both
+ # functions otherwise the result of the programme is wrong without the right value of the counter maybe there is
+ # a better way to do it but only this came to my mind, and it works fine
+ global counter
+ final_string = ''
+ for i in range(1, len(digits_list)):
+ if digits_list[i] == digits_list[i - 1]:
+ counter += 1
+ else:
+ final_string += get_each_letter(i, digits_list)
+ else:
+ final_string += get_each_letter(len(digits_list), digits_list)
+
+ return final_string
+
+
+def text_to_nums(input_string):
+ nums_list = []
+ for i in range(0, len(input_string)):
+ letter_upper = input_string[i].upper()
+ letter_upper_next = -2 # one that does not exist
+ if i < len(input_string) - 1:
+ letter_upper_next = input_string[i + 1].upper()
+
+ if phone_dic[letter_upper] is not None:
+ for num in str(phone_dic[letter_upper]):
+ nums_list.append(int(num))
+
+ if i < len(input_string) - 1 and phone_dic[letter_upper] % 10 == phone_dic[letter_upper_next] % 10:
+ nums_list.append(-1)
+
+ return nums_list
+
+
+def nums_to_angle(digits_list):
+ result_angle = 0
+ for digit in digits_list:
+ if digit != -1:
+ result_angle += rotate_phone_dic[digit]
+
+ if result_angle > 359:
+ result_angle %= 360
+ return result_angle
+
+
+def angles_to_nums(degrees_list):
+ numbers_list = []
+ for degree in degrees_list:
+ degree %= 360
+ if degree <= 0:
+ degree += 360
+ for key in not_round_degrees_dic.keys():
+ if degree in not_round_degrees_dic[key]:
+ numbers_list.append(key)
+ return numbers_list
+
+
+def is_phone_tastic(text_string):
+ degrees = nums_to_angle(text_to_nums(text_string))
+ letters = len(text_string)
+
+ return degrees % letters == 0