Тина обнови решението на 01.11.2022 17:01 (преди над 2 години)
+def nums_to_text(all_numbers):
+ letters = {("2", 1): "A", ("2", 2): "B", ("2", 3): "C", ("3", 1): "D", ("3", 2): "E", ("3", 3): "F", ("4", 1): "G", ("4", 2): "H", ("4", 3): "I", ("5", 1): "J", ("5", 2): "K", ("5", 3): "L", ("6", 1): "M", ("6", 2): "N", ("6", 3): "O", ("7", 1): "P", ("7", 2): "Q", ("7", 3): "R", ("7", 4): "S", ("8", 1): "T", ("8", 2): "U", ("8", 3): "V", ("9", 1): "W", ("9", 2): "X", ("9", 3): "Y", ("9", 4): "Z"}
+ numbers = []
+ result = ""
+ for num in all_numbers:
+ if num < 0 or num == 1 or num > 9:
+ if len(numbers) != 0:
+ numbers_to_string = ""
+ for n in numbers:
+ if n == 0:
+ continue
+ numbers_to_string += str(n)
+ if len(numbers_to_string) != 0:
+ num_count = 0
+ if numbers_to_string[0] == "7" or numbers_to_string[0] == "9":
+ if len(numbers_to_string) % 4 == 0:
+ num_count = 4
+ else:
+ num_count = len(numbers_to_string) % 4
+ else:
+ if len(numbers_to_string) % 3 == 0:
+ num_count = 3
+ else:
+ num_count = len(numbers_to_string) % 3
+ result += letters[(numbers_to_string[0], num_count)]
+ numbers.clear()
+ elif len(numbers) == 0 or numbers[-1] == num:
+ numbers.append(num)
+ elif len(numbers) != 0 and numbers[-1] != num and num != -1:
+ numbers_to_string = ""
+ for n in numbers:
+ if n == 0:
+ result += " "
+ continue
+ numbers_to_string += str(n)
+ if len(numbers_to_string) != 0:
+ num_count = 0
+ if numbers_to_string[0] == "7" or numbers_to_string[0] == "9":
+ if len(numbers_to_string) % 4 == 0:
+ num_count = 4
+ else:
+ num_count = len(numbers_to_string) % 4
+ else:
+ if len(numbers_to_string) % 3 == 0:
+ num_count = 3
+ else:
+ num_count = len(numbers_to_string) % 3
+ result += letters[(numbers_to_string[0], num_count)]
+ numbers.clear()
+ numbers.append(num)
+ numbers_to_string = ""
+ for n in numbers:
+ if n == 0:
+ result += " "
+ continue
+ numbers_to_string += str(n)
+ if len(numbers_to_string) != 0:
+ num_count = 0
+ if numbers_to_string[0] == "7" or numbers_to_string[0] == "9":
+ if len(numbers_to_string) % 4 == 0:
+ num_count = 4
+ else:
+ num_count = len(numbers_to_string) % 4
+ else:
+ if len(numbers_to_string) % 3 == 0:
+ num_count = 3
+ else:
+ num_count = len(numbers_to_string) % 3
+ result += letters[(numbers_to_string[0], num_count)]
+ return result
+#print(nums_to_text([2, 2, 1, 1, 2, 1]))
+
+def text_to_nums(text):
+ letters = {" ": (0, 1), "a": (2, 1), "b": (2, 2), "c": (2, 3), "d": (3, 1), "e": (3, 2), "f": (3, 3), "g": (4, 1), "h": (4, 2), "i": (4, 3), "j": (5, 1), "k": (5, 2), "l": (5, 3), "m": (6, 1), "n": (6, 2), "o": (6, 3), "p": (7, 1), "q": (7, 2), "r": (7, 3), "s": (7, 4), "t": (8, 1), "u": (8, 2), "v": (8, 3), "w": (9, 1), "x": (9, 2), "y": (9, 3), "z": (9, 4)}
+ text = text.lower()
+ result =[]
+ for letter in text:
+ letter_first, letter_second = letters[letter]
+ for i in range(letter_second):
+ result.append(letter_first)
+ return result
+#print(text_to_nums('asl pls'))
+
+def nums_to_angle(numbers):
+ angle = 0
+ for num in numbers:
+ if num == 0:
+ angle += 300
+ else:
+ angle += num * 30
+ return angle % 360
+#print(nums_to_angle([1, 5, 9]))
+
+def angles_to_nums(angles):
+ result = []
+ for angle in angles:
+ while angle < 0:
+ angle += 360
+ if angle > 360:
+ angle = angle % 360
+ if angle % 30 < 15:
+ angle -= angle % 30
+ else:
+ angle -= angle % 30
+ angle += 30
+ if angle == 0:
+ continue
+ else:
+ result.append(int(angle / 30))
+ return result
+#print(angles_to_nums([16, 14, 90, -120, 570, -660]))
+
+def is_phone_tastic(word):
+ word_translated = text_to_nums(word)
+ angle = nums_to_angle(word_translated)
+ return angle % len(word) == 0
+#print(is_phone_tastic('GOD'))
Ако запазим същата логика, но се опитаме да махнем дублиращият се код, можем да получим следното: