Решение на Телефонна любов от Кристиан Иванов

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

Към профила на Кристиан Иванов

Резултати

  • 5 точки от тестове
  • 0 бонус точки
  • 5 точки общо
  • 17 успешни тест(а)
  • 20 неуспешни тест(а)

Код

def nums_to_text(nums):
phone = {1: '', 11: '', 111: '', 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: ' '}
text = ''
number = nums[0]
for i in range(1, len(nums)):
if nums[i] == -1:
text += phone[number]
elif nums[i-1] == -1:
number = nums[i]
elif number % 10 == nums[i]:
number = number*10 + nums[i]

PEP8-compliant е да оставяш интервали около операторите. Няма да те лъжа, често и аз пропускам, често ми изглежда по-неугледно, но такава е конвенцията.

else:
text += phone[number]
number = nums[i]
text += phone[number]
return text
def text_to_nums(text):
phone = {'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]}
numbers = []
for letter in text:
numbers += phone[letter.upper()]
return numbers
def nums_to_angle(nums):
angle = 0
for num in nums:
angle += 30 * num
while angle > 360:
angle -= 360
return angle
def angles_to_nums(angles):
nums = []
for angle in angles:
if angle < 0:
angle += 360
if angle % 30 == 0:
nums.append(angle / 30)
elif round(angle / 30) > 0:
num = round(angle / 30)
nums.append(angle)
return nums
def is_phone_tastic(word):
size = len(word)
nums = text_to_nums(word)
angle = nums_to_angle(nums)
return angle % size == 0

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

..F.F.FFFFE..F...FF...EFFEE..EFF.FF..
======================================================================
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

======================================================================
ERROR: test_empty_input (test.TestNumsToText)
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
IndexError: list index out of range

======================================================================
ERROR: test_overflow_input (test.TestNumsToText)
Test with oveflowing number of presses.
----------------------------------------------------------------------
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
KeyError: 999999

======================================================================
ERROR: test_random_mixed_case (test.TestNumsToText)
Test for a random mixed case.
----------------------------------------------------------------------
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
KeyError: 555555

======================================================================
ERROR: test_starting_with_timeout (test.TestNumsToText)
Test with a sequence starting with a -1.
----------------------------------------------------------------------
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
KeyError: -1

======================================================================
FAIL: test_ignoring_over_330 (test.TestAnglesToNums)
Test that angles rounded over 330 are ignored.
----------------------------------------------------------------------
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: [320] != []

First list contains 1 additional elements.
First extra element 0:
320

- [320]
+ []

======================================================================
FAIL: test_multiple_angles (test.TestAnglesToNums)
Test with a couple of angles as 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: [20, 6.0, 56, 2.0, 61] != [1, 6, 2, 2, 2]

First differing element 0:
20
1

- [20, 6.0, 56, 2.0, 61]
+ [1, 6, 2, 2, 2]

======================================================================
FAIL: test_overflowing_angles (test.TestAnglesToNums)
Test with an overflowed 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: [98.0] != [2]

First differing element 0:
98.0
2

- [98.0]
+ [2]

======================================================================
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.0, 0.0, 1.0, 2.0, 135, 9.0, 11.0, 340, 24.0, 2925, 11034, 299792458, 384400] != [5, 1, 2, 4, 9, 1, 8, 0, 9]

First differing element 1:
0.0
1

First list contains 4 additional elements.
First extra element 9:
2925

- [5.0, 0.0, 1.0, 2.0, 135, 9.0, 11.0, 340, 24.0, 2925, 11034, 299792458, 384400]
+ [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: [45] != [1]

First differing element 0:
45
1

- [45]
+ [1]

======================================================================
FAIL: test_round_angle_easy_case (test.TestAnglesToNums)
Test with an angle requiring rounding - easy case.
----------------------------------------------------------------------
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: [85] != [3]

First differing element 0:
85
3

- [85]
+ [3]

======================================================================
FAIL: test_correct_mapping (test.TestNumsToAngles)
Test correct mapping for all numbers.
----------------------------------------------------------------------
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: 0 != 300

======================================================================
FAIL: test_normalizing_for_top_boundary (test.TestNumsToAngles)
Test normalizing when the sum is full circle.
----------------------------------------------------------------------
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: 360 != 0

======================================================================
FAIL: test_random_mixed_case (test.TestNumsToAngles)
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: 90 != 150

======================================================================
FAIL: test_ending_with_timeout (test.TestNumsToText)
Test with a sequence ending with a -1.
----------------------------------------------------------------------
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: 'oo' != 'o'
- oo
? -
+ o


======================================================================
FAIL: test_multiple_timeouts (test.TestNumsToText)
Test with multiple '-1's next to each other.
----------------------------------------------------------------------
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: 'jjjmppt' != 'jmpt'
- jjjmppt
? --   -
+ jmpt


======================================================================
FAIL: test_all_chars (test.TestTextToNums)
Test for correct mapping of all chars.
----------------------------------------------------------------------
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, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4,[121 chars]9, 0] != [2, -1, 2, 2, -1, 2, 2, 2, 3, -1, 3, 3, -1, 3[193 chars]9, 0]

First differing element 1:
2
-1

Second list contains 18 additional elements.
First extra element 57:
-1

  [2,
+  -1,
   2,
   2,
+  -1,
   2,
   2,
   2,
   3,
+  -1,
   3,
   3,
+  -1,
   3,
   3,
   3,
   4,
+  -1,
   4,
   4,
+  -1,
   4,
   4,
   4,
   5,
+  -1,
   5,
   5,
+  -1,
   5,
   5,
   5,
   6,
+  -1,
   6,
   6,
+  -1,
   6,
   6,
   6,
   7,
+  -1,
+  7,
+  7,
+  -1,
   7,
   7,
   7,
+  -1,
-  7,
-  7,
   7,
   7,
   7,
   7,
   8,
+  -1,
   8,
   8,
+  -1,
   8,
   8,
   8,
   9,
+  -1,
+  9,
+  9,
+  -1,
   9,
   9,
   9,
+  -1,
-  9,
-  9,
   9,
   9,
   9,
   9,
   0]

======================================================================
FAIL: test_complex_word (test.TestTextToNums)
Test with a complex word that requires -1.
----------------------------------------------------------------------
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: [4, 6, 6] != [4, 6, -1, 6]

First differing element 2:
6
-1

Second list contains 1 additional elements.
First extra element 3:
6

- [4, 6, 6]
+ [4, 6, -1, 6]
?        ++++


======================================================================
FAIL: test_mixed_casing (test.TestTextToNums)
Test for both lower and capital case.
----------------------------------------------------------------------
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, 2, 2, 3, 3] != [2, -1, 2, 2, 3, -1, 3]

First differing element 1:
2
-1

Second list contains 2 additional elements.
First extra element 5:
-1

- [2, 2, 2, 3, 3]
+ [2, -1, 2, 2, 3, -1, 3]
?     ++++         ++++


======================================================================
FAIL: test_random_mixed_case (test.TestTextToNums)
Test for a random mixed case.
----------------------------------------------------------------------
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: [8, 8, 8, 2, 7, 7, 7, 7, 5, 5, 6, 6, 6, 0, 0, 0, 5, 2, 2, 2, 2, 8, 2] not found in ([8, 8, 8, 2, 7, 7, 7, 7, 5, 5, 6, 6, 6, 0, -1, 0, -1, 0, 5, 2, -1, 2, 2, -1, 2, 8, 2], [8, 8, 8, 2, 7, 7, 7, 7, 5, 5, 6, 6, 6, 0, 0, 0, 5, 2, -1, 2, 2, -1, 2, 8, 2])

----------------------------------------------------------------------
Ran 37 tests in 0.394s

FAILED (failures=15, errors=5)

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

Кристиан обнови решението на 02.11.2022 10:07 (преди над 1 година)

+def nums_to_text(nums):
+ phone = {1: '', 11: '', 111: '', 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: ' '}
+
+ text = ''
+ number = nums[0]
+ for i in range(1, len(nums)):
+ if nums[i] == -1:
+ text += phone[number]
+ elif nums[i-1] == -1:
+ number = nums[i]
+ elif number % 10 == nums[i]:
+ number = number*10 + nums[i]

PEP8-compliant е да оставяш интервали около операторите. Няма да те лъжа, често и аз пропускам, често ми изглежда по-неугледно, но такава е конвенцията.

+ else:
+ text += phone[number]
+ number = nums[i]
+
+ text += phone[number]
+
+ return text
+
+
+def text_to_nums(text):
+ phone = {'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]}
+
+ numbers = []
+ for letter in text:
+ numbers += phone[letter.upper()]
+
+ return numbers
+
+
+def nums_to_angle(nums):
+ angle = 0
+
+ for num in nums:
+ angle += 30 * num
+
+ while angle > 360:
+ angle -= 360
+
+ return angle
+
+
+def angles_to_nums(angles):
+ nums = []
+ for angle in angles:
+ if angle < 0:
+ angle += 360
+
+ if angle % 30 == 0:
+ nums.append(angle / 30)
+ elif round(angle / 30) > 0:
+ num = round(angle / 30)
+ nums.append(angle)
+
+ return nums
+
+
+def is_phone_tastic(word):
+ size = len(word)
+
+ nums = text_to_nums(word)
+
+ angle = nums_to_angle(nums)
+
+ return angle % size == 0