Решение на Телефонна любов от Петър Тодоров

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

Към профила на Петър Тодоров

Резултати

  • 9 точки от тестове
  • 0 бонус точки
  • 9 точки общо
  • 34 успешни тест(а)
  • 3 неуспешни тест(а)

Код

valid_numbers = [0, 2, 3, 4, 5, 6, 7, 8, 9]
time_out = -1
table = {0:" ", 2: "ABC", 3: "DEF", 4: "GHI", 5: "JKL", 6: "MNO", 7: "PQRS", 8: "TUV", 9: "WXYZ"}
def nums_to_text(nums):
res = ""
n = len(nums)
if n == 0:
return res
curr_number = nums[0]
i = 1
while i <= n:
sequence_length = 1
while i < n and not nums[i] == time_out and (nums[i] == curr_number or nums[i] not in valid_numbers):
if nums[i] == curr_number:
sequence_length += 1
i += 1
if curr_number in valid_numbers and not curr_number == 0:
if sequence_length >= len(table[curr_number]):
sequence_length = sequence_length % len(table[curr_number])
res += table[curr_number][sequence_length - 1]
elif curr_number == 0:
res += " " * sequence_length
if i >= n:
break
curr_number = nums[i]
i += 1
return res
def text_to_nums(string):
res = []
string = string.upper()
for letter in string:
num = 0
sequence = 0
for key, val in table.items():
if letter in val:
num = key
sequence = val.find(letter) + 1
break
if len(res) > 0 and num == res[-1]:
res.append(-1)
res += [num for i in range(sequence)]
return res
def nums_to_angle(nums):
res = 0
for i in range(len(nums)):
res += nums[i] * 30
if nums[i] == 0:
res += 300
return res % 360
def angles_to_nums(angles):
angles = [(round(angle/30)) % 12 for angle in angles]
angles = [angle % 10 for angle in angles if 0 < angle < 11]
return angles
def is_phone_tastic(word):
return nums_to_angle(text_to_nums(word)) % len(word) == 0

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

.......FF.E..........................
======================================================================
ERROR: test_empty_input (test.TestIsPhonetastic)
Test with empty 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
ZeroDivisionError: integer division or modulo by zero

======================================================================
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, 2, 8, 0, 9] != [5, 1, 2, 4, 9, 1, 8, 0, 9]

First differing element 5:
2
1

- [5, 1, 2, 4, 9, 2, 8, 0, 9]
?                 ^

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


======================================================================
FAIL: test_round_angle_direction (test.TestAnglesToNums)
Test with an angle requiring explicit rounding to floor.
----------------------------------------------------------------------
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: [2] != [1]

First differing element 0:
2
1

- [2]
+ [1]

----------------------------------------------------------------------
Ran 37 tests in 0.360s

FAILED (failures=2, errors=1)

История (2 версии и 6 коментара)

Петър обнови решението на 31.10.2022 20:10 (преди над 1 година)

+important_numbers = [0, 2, 3, 4, 5, 6, 7, 8, 9]
+time_out = -1
+table = {0:" ", 2: "ABC", 3: "DEF", 4: "GHI", 5: "JKL", 6: "MNO", 7: "PQRS", 8: "TUV", 9: "WXYZ"}
+
+
+def nums_to_text(nums):
+ res = ""
+ n = len(nums)
+ if n == 0:
+ return res
+ curr_number = nums[0]
+ i = 1
+ while i < n:

Този while има няколко сложни реда, които бих рефакторирал. Не е нужно да го правиш сега, но се опитай да прилагаш по-лесен за четене код. Ако не знам какво прави, би ми отнело време да го разбера, а можеш да го напишеш и по-упростено.

+ sequence_length = 1
+ while i < n and not nums[i] == time_out and (nums[i] == curr_number or nums[i] not in important_numbers):
+ if nums[i] == curr_number:
+ sequence_length += 1
+ i += 1
+ if curr_number in important_numbers and not curr_number == 0:
+ if sequence_length >= len(table[curr_number]):
+ sequence_length = sequence_length % len(table[curr_number])
+ res += table[curr_number][sequence_length - 1]
+ elif curr_number == 0:
+ res += " " * sequence_length
+ if i >= n:
+ break
+ curr_number = nums[i]
+ i += 1
+
+ return res
+
+
+def text_to_nums(string):
+ res = []
+ string = string.upper()
+ for letter in string:
+ num = 0
+ sequence = 0
+ for key, val in table.items():
+ if letter in val:
+ num = key
+ sequence = val.find(letter) + 1
+ break
+ if len(res) > 0 and num == res[-1]:
+ res.append(-1)
+ res += [num for i in range(sequence)]
+ return res
+
+
+def nums_to_angle(nums):
+ res = 0
+ for i in range(len(nums)):
+ res += nums[i] * 30
+ if nums[i] == 0:
+ res += 300
+ return res % 360
+
+
+def angles_to_nums(angles):
+ angles = [round(angle/30) for angle in angles]
+ angles = [angle % 12 for angle in angles if not angle % 12 == 0]

Относно процентното деление на 12- първо съм разделил всеки ъгъл на 30, тоест 30->1 60->2 .... 360->12, съответно -240 -> -8. За да превърна тази -8 в 4, деля проценто на 12. Не съм се сетил първоначално да ползвам абсолютна стойност явно :D Но сега виждам, че за да игнорирам нулите трябва да е if not angle % 10 == 0

+ return angles
+
+
+def is_phone_tastic(word):
+ return nums_to_angle(text_to_nums(word)) % len(word) == 0
+

Петър обнови решението на 02.11.2022 23:44 (преди над 1 година)

-important_numbers = [0, 2, 3, 4, 5, 6, 7, 8, 9]
+valid_numbers = [0, 2, 3, 4, 5, 6, 7, 8, 9]
time_out = -1
table = {0:" ", 2: "ABC", 3: "DEF", 4: "GHI", 5: "JKL", 6: "MNO", 7: "PQRS", 8: "TUV", 9: "WXYZ"}
def nums_to_text(nums):
res = ""
n = len(nums)
if n == 0:
return res
curr_number = nums[0]
i = 1
- while i < n:
+ while i <= n:
sequence_length = 1
- while i < n and not nums[i] == time_out and (nums[i] == curr_number or nums[i] not in important_numbers):
+ while i < n and not nums[i] == time_out and (nums[i] == curr_number or nums[i] not in valid_numbers):
if nums[i] == curr_number:
sequence_length += 1
i += 1
- if curr_number in important_numbers and not curr_number == 0:
+ if curr_number in valid_numbers and not curr_number == 0:
if sequence_length >= len(table[curr_number]):
sequence_length = sequence_length % len(table[curr_number])
res += table[curr_number][sequence_length - 1]
elif curr_number == 0:
res += " " * sequence_length
if i >= n:
break
curr_number = nums[i]
i += 1
return res
def text_to_nums(string):
res = []
string = string.upper()
for letter in string:
num = 0
sequence = 0
for key, val in table.items():
if letter in val:
num = key
sequence = val.find(letter) + 1
break
if len(res) > 0 and num == res[-1]:
res.append(-1)
res += [num for i in range(sequence)]
return res
def nums_to_angle(nums):
res = 0
for i in range(len(nums)):
res += nums[i] * 30
if nums[i] == 0:
res += 300
return res % 360
def angles_to_nums(angles):
- angles = [round(angle/30) for angle in angles]
- angles = [angle % 12 for angle in angles if not angle % 12 == 0]
+ angles = [(round(angle/30)) % 12 for angle in angles]
+ angles = [angle % 10 for angle in angles if 0 < angle < 11]
return angles
def is_phone_tastic(word):
return nums_to_angle(text_to_nums(word)) % len(word) == 0