Решение на Телефонна любов от Цветелина Чакърова

Обратно към всички решения

Към профила на Цветелина Чакърова

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 37 успешни тест(а)
  • 0 неуспешни тест(а)

Код

def normalize_count(count, digit):
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):
return angle % 360
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, num in enumerate(nums):
if num == -1 or num == 1:
continue
count += 1
if index != len(nums) - 1:
if num != nums[index + 1]:
count_and_digit = normalize_count(count, nums[index-1]) * 10 + num
text += num_to_letter[count_and_digit]
count = 0
else:
if num == 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 + num
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, letter in enumerate(upper_text):
if index != 0:
if letter_to_num[upper_text[index - 1]][0] == letter_to_num[letter][0]:
nums.append(-1)
nums.extend(letter_to_num[letter])
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:
if num != -1:
angle_sum += num_to_angle[num]
return normalize_angle(angle_sum)
def angles_to_nums(angles):
angle_to_num = {
30: 1, 60: 2, 90: 3, 120: 4, 150: 5,
180: 6, 210: 7, 240: 8, 270: 9, 300: 0,
}
normalized_angles = [normalize_angle(angle) for angle in angles]
nums = []
for angle in normalized_angles:
for key in angle_to_num:
if -15 <= key - angle <= 14:
nums.append(angle_to_num[key])
return nums
def is_phone_tastic(word):
if len(word) == 0:
return False
else:
return nums_to_angle((text_to_nums(word))) % len(word) == 0

Лог от изпълнението

.....................................
----------------------------------------------------------------------
Ran 37 tests in 0.313s

OK

История (8 версии и 4 коментара)

Цветелина обнови решението на 29.10.2022 22:33 (преди над 1 година)

+def normalize_count(count, digit):
+ if digit == 7 or digit == 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)):
+ 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

Цветелина обнови решението на 31.10.2022 19:14 (преди над 1 година)

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:
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)):
- if nums[index] == -1 or nums[index] == 1:
+ for index, num in enumerate(nums):
+ if num == -1 or num == 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]
+ if num != nums[index + 1]:
+ count_and_digit = normalize_count(count, nums[index-1]) * 10 + num
text += num_to_letter[count_and_digit]
count = 0
else:
- if nums[index] == nums[index - 1]:
+ if num == 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]
+ count_and_digit = 10 + num
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

Цветелина обнови решението на 31.10.2022 19:39 (преди над 1 година)

def normalize_count(count, digit):
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:
- return angle % 360
- else:
- while angle < 0:
- angle += 360
- return angle
+ return angle % 360
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, num in enumerate(nums):
if num == -1 or num == 1:
continue
count += 1
if index != len(nums) - 1:
if num != nums[index + 1]:
count_and_digit = normalize_count(count, nums[index-1]) * 10 + num
text += num_to_letter[count_and_digit]
count = 0
else:
if num == 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 + num
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
+

Цветелина обнови решението на 31.10.2022 19:53 (преди над 1 година)

def normalize_count(count, digit):
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):
return angle % 360
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, num in enumerate(nums):
if num == -1 or num == 1:
continue
count += 1
if index != len(nums) - 1:
if num != nums[index + 1]:
count_and_digit = normalize_count(count, nums[index-1]) * 10 + num
text += num_to_letter[count_and_digit]
count = 0
else:
if num == 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 + num
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)):
+ for index, letter in enumerate(upper_text):
if index != 0:
- if letter_to_num[upper_text[index - 1]][0] == letter_to_num[upper_text[index]][0]:
+ if letter_to_num[upper_text[index - 1]][0] == letter_to_num[letter][0]:
nums.append(-1)
- nums.extend(letter_to_num[upper_text[index]])
+ nums.extend(letter_to_num[letter])
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
-

Цветелина обнови решението на 31.10.2022 20:21 (преди над 1 година)

def normalize_count(count, digit):
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):
return angle % 360
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, num in enumerate(nums):
if num == -1 or num == 1:
continue
count += 1
if index != len(nums) - 1:
if num != nums[index + 1]:
count_and_digit = normalize_count(count, nums[index-1]) * 10 + num
text += num_to_letter[count_and_digit]
count = 0
else:
if num == 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 + num
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, letter in enumerate(upper_text):
if index != 0:
if letter_to_num[upper_text[index - 1]][0] == letter_to_num[letter][0]:
nums.append(-1)
nums.extend(letter_to_num[letter])
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):
+ angle_to_num = {
+ 30: 1, 60: 2, 90: 3, 120: 4, 150: 5,
+ 180: 6, 210: 7, 240: 8, 270: 9, 300: 0,
+ }
+
normalized_angles = [normalize_angle(angle) for angle in angles]
+ rounded_angles = [30, 60, 90, 120, 150, 180, 210, 240, 270, 300]
+
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)
+ for rounded_angle in rounded_angles:
+ if -15 <= rounded_angle - angle <= 14:
+ nums.append(angle_to_num[rounded_angle])
return nums
def is_phone_tastic(word):
return nums_to_angle((text_to_nums(word))) % len(word) == 0

Цветелина обнови решението на 31.10.2022 20:24 (преди над 1 година)

def normalize_count(count, digit):
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):
return angle % 360
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, num in enumerate(nums):
if num == -1 or num == 1:
continue
count += 1
if index != len(nums) - 1:
if num != nums[index + 1]:
count_and_digit = normalize_count(count, nums[index-1]) * 10 + num
text += num_to_letter[count_and_digit]
count = 0
else:
if num == 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 + num
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, letter in enumerate(upper_text):
if index != 0:
if letter_to_num[upper_text[index - 1]][0] == letter_to_num[letter][0]:
nums.append(-1)
nums.extend(letter_to_num[letter])
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):
angle_to_num = {
30: 1, 60: 2, 90: 3, 120: 4, 150: 5,
180: 6, 210: 7, 240: 8, 270: 9, 300: 0,
}
normalized_angles = [normalize_angle(angle) for angle in angles]
- rounded_angles = [30, 60, 90, 120, 150, 180, 210, 240, 270, 300]
-
nums = []
for angle in normalized_angles:
- for rounded_angle in rounded_angles:
- if -15 <= rounded_angle - angle <= 14:
- nums.append(angle_to_num[rounded_angle])
+ for key in angle_to_num:
+ if -15 <= key - angle <= 14:
+ nums.append(angle_to_num[key])
return nums
def is_phone_tastic(word):
return nums_to_angle((text_to_nums(word))) % len(word) == 0

Цветелина обнови решението на 02.11.2022 10:05 (преди над 1 година)

def normalize_count(count, digit):
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):
return angle % 360
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, num in enumerate(nums):
if num == -1 or num == 1:
continue
count += 1
if index != len(nums) - 1:
if num != nums[index + 1]:
count_and_digit = normalize_count(count, nums[index-1]) * 10 + num
text += num_to_letter[count_and_digit]
count = 0
else:
if num == 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 + num
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, letter in enumerate(upper_text):
if index != 0:
if letter_to_num[upper_text[index - 1]][0] == letter_to_num[letter][0]:
nums.append(-1)
nums.extend(letter_to_num[letter])
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]
+ if num != -1:
+ angle_sum += num_to_angle[num]
return normalize_angle(angle_sum)
def angles_to_nums(angles):
angle_to_num = {
30: 1, 60: 2, 90: 3, 120: 4, 150: 5,
180: 6, 210: 7, 240: 8, 270: 9, 300: 0,
}
normalized_angles = [normalize_angle(angle) for angle in angles]
nums = []
for angle in normalized_angles:
for key in angle_to_num:
if -15 <= key - angle <= 14:
nums.append(angle_to_num[key])
return nums
def is_phone_tastic(word):
return nums_to_angle((text_to_nums(word))) % len(word) == 0

Цветелина обнови решението на 03.11.2022 00:38 (преди над 1 година)

def normalize_count(count, digit):
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):
return angle % 360
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, num in enumerate(nums):
if num == -1 or num == 1:
continue
count += 1
if index != len(nums) - 1:
if num != nums[index + 1]:
count_and_digit = normalize_count(count, nums[index-1]) * 10 + num
text += num_to_letter[count_and_digit]
count = 0
else:
if num == 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 + num
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, letter in enumerate(upper_text):
if index != 0:
if letter_to_num[upper_text[index - 1]][0] == letter_to_num[letter][0]:
nums.append(-1)
nums.extend(letter_to_num[letter])
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:
if num != -1:
angle_sum += num_to_angle[num]
return normalize_angle(angle_sum)
def angles_to_nums(angles):
angle_to_num = {
30: 1, 60: 2, 90: 3, 120: 4, 150: 5,
180: 6, 210: 7, 240: 8, 270: 9, 300: 0,
}
normalized_angles = [normalize_angle(angle) for angle in angles]
nums = []
for angle in normalized_angles:
for key in angle_to_num:
if -15 <= key - angle <= 14:
nums.append(angle_to_num[key])
return nums
def is_phone_tastic(word):
- return nums_to_angle((text_to_nums(word))) % len(word) == 0
+ if len(word) == 0:
+ return False
+ else:
+ return nums_to_angle((text_to_nums(word))) % len(word) == 0