Кристиан обнови решението на 29.10.2022 14:40 (преди над 2 години)
+nums_to_letter = {
+ 2: "A",
+ 22: "B",
+ 222: "C",
+ 3: "D",
+ 33: "E",
+ 333: "F",
+ 4: "G",
+ 44: "H",
+ 444: "I",
+ 5: "J",
+ 55: "K",
+ 555: "L",
+ 6: "M",
+ 66: "N",
+ 666: "O",
+ 7: "P",
+ 77: "Q",
+ 777: "R",
+ 7777: "S",
+ 8: "T",
+ 88: "U",
+ 888: "V",
+ 9: "W",
+ 99: "X",
+ 999: "Y",
+ 9999: "Z",
+ 0: " "
+}
+
+letters_to_nums = {
+ "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),
+ " ": (0, 1)
+}
+
+
+def convert_occurances_of_digit_to_num(digit, occurances):
+ i = 0
В Python можеш да умножава низове:
return int(str(digit) * occurances)
+ num = ""
+ while i < occurances:
+ num += str(digit)
+ i += 1
+ return int(num)
+
+
+def convert_digits_to_letter(digit, occurances):
+ if digit > 9 or digit == 1 or digit < 0:
+ return ""
+ if occurances > 4 and (digit == 7 or digit == 9):
За проверка дали нещо е равно на други няколко неща, можеш (и е прпоръчително) да използваш in
:
digit in (7, 9)
+ occurances %= 4
+ elif occurances > 3 and (digit != 7 and digit != 9):
+ occurances %= 3
+ num = convert_occurances_of_digit_to_num(digit, occurances)
+ return nums_to_letter[num]
+
+
+def normalize_angle(angle):
+ if angle < 0:
Този if-else
е излишен. Просто можеш да сложиш двата while
-а.
+ while angle < 0:
+ angle += 360
+ else:
+ while angle > 359:
+ angle -= 360
+ return angle
+
+
+def nums_to_text(nums):
+ text = ""
+ last_digit = nums[0]
+ occurances = 0
+ for num in nums:
+ if num == last_digit:
+ occurances += 1
+ else:
+ letter = convert_digits_to_letter(last_digit, occurances)
+ text += letter
+ if num != -1:
+ last_digit = num
+ occurances = 1
+ else:
+ occurances = 0
+ text += convert_digits_to_letter(last_digit, occurances)
+ return text
+
+
+def text_to_nums(text):
+ nums = []
+ for letter in text:
+ letter_upper = letter.upper()
+ digit_and_occurances = letters_to_nums[letter_upper]
+ if digit_and_occurances is not None:
+ i = 0
+ while i < digit_and_occurances[1]:
+ nums.append(digit_and_occurances[0])
+ i += 1
+ return nums
+
+
+def nums_to_angle(nums):
+ angles_sum = 0
+ for num in nums:
+ if num == 0:
+ angles_sum += 300
+ else:
+ angles_sum += num * 30
+ return normalize_angle(angles_sum)
+
+
+def angles_to_nums(angles):
+ nums = []
+ for angle in angles:
+ angle_normalized = normalize_angle(angle)
+ if angle == 345:
+ nearest_angle = 330
+ else:
+ nearest_angle = 30 * round(angle_normalized / 30)
round закръглява половинките към четно число. https://docs.python.org/3/library/functions.html#round
Това не е приом на Python, а генерална промяна в доста езици за програмиране напоследък. Вземи го предвид.
+ if nearest_angle != 0 and nearest_angle != 360:
+ num = int(nearest_angle / 30)
+ nums.append(num)
+ return nums
+
+
+def is_phone_tastic(word):
+ word_to_nums = text_to_nums(word)
+ word_angle = nums_to_angle(word_to_nums)
+ if word_angle % len(word) == 0:
Можеш и
return word_angle % len(word) == 0
+ return True
+ else:
+ return False
Може и да греша, но не виждам да си имплементирал стойностите за timeout - "-1". Моля провери и се убеди, че имаш това изискване.