Решение на Телефонна любов от Румен Тошев

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

Към профила на Румен Тошев

Резултати

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

Код

def nums_to_text(nums):
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"}
index1 = 0
translated_text = ""
while index1 < len(nums):

Бих те посъветвал да погледнеш другите (вече публични) решения. Има доста по-кратки начини за този алгоритъм. Да, твоят работи, но е доста усложнен.

if nums[index1] in [-1, 1]:
index1 += 1
continue
elif nums[index1] == 0:
translated_text += " "
index1 += 1
continue
else:
temp_letter = [nums[index1]]
index2 = index1 + 1
if index2 >= len(nums):
letter_with_nums = "".join(str(el) for el in temp_letter)
translated_text += nums_to_letter[letter_with_nums]
break
while index2 < len(nums):
if nums[index1] == nums[index2]:
if temp_letter[0] in [2, 3, 4, 5, 6, 8]:
if len(temp_letter) == 3:
temp_letter = []
elif temp_letter[0] in [7, 9]:
if len(temp_letter) == 4:
temp_letter = []
temp_letter.append(nums[index2])
index2 += 1
elif nums[index2] != nums[index1]:
break
if index1 + 1 == index2:
index1 += 1
else:
index1 = index2
letter_with_nums = "".join(str(el) for el in temp_letter)
translated_text += nums_to_letter[letter_with_nums]
return translated_text
def text_to_nums(text):
text_translated_to_nums = []
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]}
text_upper = [i.upper() for i in text]
for index in range(len(text)):
text_translated_to_nums.extend(letter_to_num[text_upper[index]])
if index != len(text)-1:
next_letter = text_upper[index + 1]
curr_letter = text_upper[index]
next_leter_ascii = ord(next_letter)
curr_leter_ascii = ord(curr_letter)
if 65 <= curr_leter_ascii <= 67 and 65 <= next_leter_ascii <= 67:
text_translated_to_nums.append(-1)
elif 68 <= curr_leter_ascii <= 70 and 68 <= next_leter_ascii <= 70:
text_translated_to_nums.append(-1)
elif 71 <= curr_leter_ascii <= 73 and 71 <= next_leter_ascii <= 73:
text_translated_to_nums.append(-1)
elif 74 <= curr_leter_ascii <= 76 and 74 <= next_leter_ascii <= 76:
text_translated_to_nums.append(-1)
elif 77 <= curr_leter_ascii <= 79 and 77 <= next_leter_ascii <= 79:
text_translated_to_nums.append(-1)
elif 80 <= curr_leter_ascii <= 83 and 80 <= next_leter_ascii <= 83:
text_translated_to_nums.append(-1)
elif 84 <= curr_leter_ascii <= 86 and 84 <= next_leter_ascii <= 86:
text_translated_to_nums.append(-1)
elif 87 <= curr_leter_ascii <= 90 and 87 <= next_leter_ascii <= 90:
text_translated_to_nums.append(-1)
return text_translated_to_nums
def nums_to_angle(nums):
sum_of_angels = 0
for element in nums:
if element in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
sum_of_angels += element*30
elif element == 0:
sum_of_angels += 300
while sum_of_angels > 359:
sum_of_angels -= 360
return sum_of_angels
def angles_to_nums(angles):
angles_to_nums = []
for angle in angles:
while angle < 0:
angle += 360
while angle > 359:
angle -= 360
if angle % 30 > 15:
angle += 30 - angle % 30
if angle == 360:
angle = 0
elif angle % 30 <= 15:
angle -= angle % 30
if angle in [0, 330]:
continue
else:
angles_to_nums.append(int(angle/30))
return angles_to_nums
def is_phone_tastic(word):
if word in ["", " "]:
return False
word_len = len(word) - word.count(' ')
letter_to_number = text_to_nums(word)
angle = nums_to_angle(letter_to_number)
while angle < 0:
angle += 360
while angle > 359:
angle -= 360
if angle % 30 > 15:
angle += 30 - angle % 30
if angle == 360:
angle = 0
elif angle % 30 <= 15:
angle -= angle % 30
return angle % word_len == 0

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

.......F.............................
======================================================================
FAIL: test_random_mixed_case (test.TestAnglesToNums)
Test with a random mixed input.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221020151654/lib/language/python/runner.py", line 67, in thread
    raise result
AssertionError: Lists differ: [5, 1, 2, 4, 9, 1, 8, 10, 9] != [5, 1, 2, 4, 9, 1, 8, 0, 9]

First differing element 7:
10
0

- [5, 1, 2, 4, 9, 1, 8, 10, 9]
?                       -

+ [5, 1, 2, 4, 9, 1, 8, 0, 9]

----------------------------------------------------------------------
Ran 37 tests in 0.432s

FAILED (failures=1)

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

Румен обнови решението на 03.11.2022 00:07 (преди над 1 година)

+def nums_to_text(nums):
+ 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"}
+ index1 = 0
+ translated_text = ""
+ while index1 < len(nums):

Бих те посъветвал да погледнеш другите (вече публични) решения. Има доста по-кратки начини за този алгоритъм. Да, твоят работи, но е доста усложнен.

+ if nums[index1] in [-1, 1]:
+ index1 += 1
+ continue
+ elif nums[index1] == 0:
+ translated_text += " "
+ index1 += 1
+ continue
+ else:
+ temp_letter = [nums[index1]]
+ index2 = index1 + 1
+ if index2 >= len(nums):
+ letter_with_nums = "".join(str(el) for el in temp_letter)
+ translated_text += nums_to_letter[letter_with_nums]
+ break
+ while index2 < len(nums):
+ if nums[index1] == nums[index2]:
+ if temp_letter[0] in [2, 3, 4, 5, 6, 8]:
+ if len(temp_letter) == 3:
+ temp_letter = []
+ elif temp_letter[0] in [7, 9]:
+ if len(temp_letter) == 4:
+ temp_letter = []
+ temp_letter.append(nums[index2])
+ index2 += 1
+ elif nums[index2] != nums[index1]:
+ break
+ if index1 + 1 == index2:
+ index1 += 1
+ else:
+ index1 = index2
+ letter_with_nums = "".join(str(el) for el in temp_letter)
+ translated_text += nums_to_letter[letter_with_nums]
+ return translated_text
+
+
+def text_to_nums(text):
+ text_translated_to_nums = []
+ 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]}
+ text_upper = [i.upper() for i in text]
+ for index in range(len(text)):
+ text_translated_to_nums.extend(letter_to_num[text_upper[index]])
+ if index != len(text)-1:
+ next_letter = text_upper[index + 1]
+ curr_letter = text_upper[index]
+ next_leter_ascii = ord(next_letter)
+ curr_leter_ascii = ord(curr_letter)
+ if 65 <= curr_leter_ascii <= 67 and 65 <= next_leter_ascii <= 67:
+ text_translated_to_nums.append(-1)
+ elif 68 <= curr_leter_ascii <= 70 and 68 <= next_leter_ascii <= 70:
+ text_translated_to_nums.append(-1)
+ elif 71 <= curr_leter_ascii <= 73 and 71 <= next_leter_ascii <= 73:
+ text_translated_to_nums.append(-1)
+ elif 74 <= curr_leter_ascii <= 76 and 74 <= next_leter_ascii <= 76:
+ text_translated_to_nums.append(-1)
+ elif 77 <= curr_leter_ascii <= 79 and 77 <= next_leter_ascii <= 79:
+ text_translated_to_nums.append(-1)
+ elif 80 <= curr_leter_ascii <= 83 and 80 <= next_leter_ascii <= 83:
+ text_translated_to_nums.append(-1)
+ elif 84 <= curr_leter_ascii <= 86 and 84 <= next_leter_ascii <= 86:
+ text_translated_to_nums.append(-1)
+ elif 87 <= curr_leter_ascii <= 90 and 87 <= next_leter_ascii <= 90:
+ text_translated_to_nums.append(-1)
+ return text_translated_to_nums
+
+
+def nums_to_angle(nums):
+ sum_of_angels = 0
+ for element in nums:
+ if element in [1, 2, 3, 4, 5, 6, 7, 8, 9]:
+ sum_of_angels += element*30
+ elif element == 0:
+ sum_of_angels += 300
+ while sum_of_angels > 359:
+ sum_of_angels -= 360
+ return sum_of_angels
+
+
+def angles_to_nums(angles):
+ angles_to_nums = []
+ for angle in angles:
+ while angle < 0:
+ angle += 360
+ while angle > 359:
+ angle -= 360
+ if angle % 30 > 15:
+ angle += 30 - angle % 30
+ if angle == 360:
+ angle = 0
+ elif angle % 30 <= 15:
+ angle -= angle % 30
+ if angle in [0, 330]:
+ continue
+ else:
+ angles_to_nums.append(int(angle/30))
+ return angles_to_nums
+
+
+def is_phone_tastic(word):
+ if word in ["", " "]:
+ return False
+ word_len = len(word) - word.count(' ')
+ letter_to_number = text_to_nums(word)
+ angle = nums_to_angle(letter_to_number)
+ while angle < 0:
+ angle += 360
+ while angle > 359:
+ angle -= 360
+ if angle % 30 > 15:
+ angle += 30 - angle % 30
+ if angle == 360:
+ angle = 0
+ elif angle % 30 <= 15:
+ angle -= angle % 30
+ return angle % word_len == 0
+