Цветелина обнови решението на 29.10.2022 22:33 (преди над 2 години)
+def normalize_count(count, digit):
+ if digit == 7 or digit == 9:
Прието е, а и смятам, че ще се съгласиш, че е по-четимо:
if digit in (7, 9)
+ if count % 4 == 0:
+ return 4
+ else:
+ return count % 4
+ elif digit == 0:
+ return 1
+ else:
+ if count % 3 == 0:
+ return 3
+ else:
+ return count % 3
+
+
+def normalize_angle(angle):
+ if angle >= 0:
Можеш да си спестиш if
-а като пуснеш while
във всеки случай, защото той просто няма да се изпълни, ако ъгълът не е по-малък от 0.
while angle < 0:
angle += 360
return angle % 360
Дали това въобще е нужно, е друг въпрос :)
+ return angle % 360
+ else:
+ while angle < 0:
+ angle += 360
+ return angle
+
+
+def nums_to_text(nums):
+
+ # kn: "L" -> k times pressing digit n equals letter L
+ num_to_letter = {
+ 12: "A", 22: "B", 32: "C",
+ 13: "D", 23: "E", 33: "F",
+ 14: "G", 24: "H", 34: "I",
+ 15: "J", 25: "K", 35: "L",
+ 16: "M", 26: "N", 36: "O",
+ 17: "P", 27: "Q", 37: "R", 47: "S",
+ 18: "T", 28: "U", 38: "V",
+ 19: "W", 29: "X", 39: "Y", 49: "Z",
+ 10: " ",
+ }
+
+ text = ""
+ count = 0
+
+ for index in range(len(nums)):
Когато ти е нужен индекса, както и самия елемент, можеш (и доста често ще го срещаш):
for index, num in enumerate(nums)
+ if nums[index] == -1 or nums[index] == 1:
+ continue
+
+ count += 1
+
+ if index != len(nums) - 1:
+ if nums[index] != nums[index + 1]:
+ count_and_digit = normalize_count(count, nums[index-1]) * 10 + nums[index]
+ text += num_to_letter[count_and_digit]
+ count = 0
+ else:
+ if nums[index] == nums[index - 1]:
+ count_and_digit = normalize_count(count, nums[index-1]) * 10 + nums[index - 1]
+ text += num_to_letter[count_and_digit]
+ else:
+ count_and_digit = 10 + nums[index]
+ text += num_to_letter[count_and_digit]
+
+ return text
+
+
+def text_to_nums(text):
+
+ letter_to_num = {
+ "A": [2], "B": [2, 2], "C": [2, 2, 2],
+ "D": [3], "E": [3, 3], "F": [3, 3, 3],
+ "G": [4], "H": [4, 4], "I": [4, 4, 4],
+ "J": [5], "K": [5, 5], "L": [5, 5, 5],
+ "M": [6], "N": [6, 6], "O": [6, 6, 6],
+ "P": [7], "Q": [7, 7], "R": [7, 7, 7], "S": [7, 7, 7, 7],
+ "T": [8], "U": [8, 8], "V": [8, 8, 8],
+ "W": [9], "X": [9, 9], "Y": [9, 9, 9], "Z": [9, 9, 9, 9],
+ " ": [0],
+ }
+
+ nums = []
+ upper_text = text.upper()
+ for index in range(len(upper_text)):
+ if index != 0:
+ if letter_to_num[upper_text[index - 1]][0] == letter_to_num[upper_text[index]][0]:
+ nums.append(-1)
+ nums.extend(letter_to_num[upper_text[index]])
+
+ return nums
+
+
+def nums_to_angle(nums):
+ num_to_angle = {
+ 1: 30, 2: 60, 3: 90, 4: 120, 5: 150,
+ 6: 180, 7: 210, 8: 240, 9: 270, 0: 300,
+ }
+
+ angle_sum = 0
+ for num in nums:
+ angle_sum += num_to_angle[num]
+
+ return normalize_angle(angle_sum)
+
+
+def angles_to_nums(angles):
+ normalized_angles = [normalize_angle(angle) for angle in angles]
+
+ nums = []
+ for angle in normalized_angles:
+ if 16 <= angle <= 45:
Бих се опитал да дефинирам това като по-компактен алгоритъм, но ако това е с цената на грешен резултат, съгласен съм да го оставиш така. Просто го имай предвид за следващия път.
+ nums.append(1)
+ elif 46 <= angle <= 75:
+ nums.append(2)
+ elif 76 <= angle <= 105:
+ nums.append(3)
+ elif 106 <= angle <= 135:
+ nums.append(4)
+ elif 136 <= angle <= 165:
+ nums.append(5)
+ elif 166 <= angle <= 195:
+ nums.append(6)
+ elif 196 <= angle <= 225:
+ nums.append(7)
+ elif 226 <= angle <= 255:
+ nums.append(8)
+ elif 256 <= angle <= 285:
+ nums.append(9)
+ elif 286 <= angle <= 315:
+ nums.append(0)
+
+ return nums
+
+
+def is_phone_tastic(word):
+ return nums_to_angle((text_to_nums(word))) % len(word) == 0