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

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

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

Резултати

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

Код

dict_mobile_phone = {2: ['A', 'B', 'C'], 3: ['D', 'E', 'F'], 4: ['G', 'H', 'I'], 5: ['J', 'K', 'L'], 6: ['M', 'N', 'O'],
7: ['P', 'Q', 'R', 'S'], 8: ['T', 'U', 'V'], 9: ['W', 'H', 'Y', 'Z'], 0: [' '], 1: []}
def nums_to_text(nums):
count = 0
index = 0
result = ""
length = len(nums)
for i in nums:
next_el = -5
if index < length - 1:
next_el = nums[index + 1]
index = index + 1
if i == next_el:
count = count + 1
elif i == -1:
count = 0
else:
temp = count
if i == 7 or i == 8:
temp = count % 4
else:
temp = count % 3
for el in dict_mobile_phone[i]:
if temp == 0:
result = result + el
break
else:
temp = temp - 1
count = 0
return result
def text_to_nums(text):
result = []
for char in text:
index = 0
for keys in dict_mobile_phone:

Ако обърнеш речника (направиш друг, в който размениш местата на ключ/стойност), ще спестиш един цикъл.
Това, разбира се, можеш да направиш динамично. Не е нужно да го дефинираш ръчно. Просто обхождаш старта дефиниция в началото на файла и правиш обратната, така че функциите ти после да могат да я използват.

for el in dict_mobile_phone[keys]:
index = index + 1
if char.upper() == el:
while index > 0:
result.append(keys)
index = index - 1
continue
index = 0
return result
def nums_to_angle(nums):
result = 0
for el in nums:
if 0 < el <= 9:
result = result + el * 30
elif el == 0:
result = result + 300
else:
continue
while result >= 360:
result = result - 360
return result
def angles_to_nums(angles):
result = []
for el in angles:
if 0 <= el < 360 and el % 30 == 0 and el < 330:
result.append(el // 30)
else:
while el < 0:
el = 360 + el
while el >= 360:
el = el - 360
if el % 30 == 0 and el < 330:
result.append(el // 30)
else:
temp = el % 30
if temp > 15:
el = 30 - temp + el
else:
el = el - temp
if el != 0 and el < 330:
result.append(el // 30)
return result
def is_phone_tastic(word):
text = text_to_nums(word)
angle = nums_to_angle(text)
length = len(word)
if angle % length == 0:
return True
else:
return False

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

.......F..E.........F....FF...FF.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

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

First differing element 1:
0
1

First list contains 2 additional elements.
First extra element 9:
10

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

======================================================================
FAIL: test_all_chars (test.TestNumsToText)
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: 'abcdefghijklmnopqrstuvwhyw ' != 'abcdefghijklmnopqrstuvwxyz '
- abcdefghijklmnopqrstuvwhyw 
?                        ^ ^
+ abcdefghijklmnopqrstuvwxyz 
?                        ^ ^


======================================================================
FAIL: 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
AssertionError: 'y' != 'x'
- y
+ x


======================================================================
FAIL: 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
AssertionError: 'fmi rulwww' != 'fmi rulzzz'
- fmi rulwww
+ fmi rulzzz


======================================================================
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,
-  9,
-  9,
   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,
   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.463s

FAILED (failures=8, errors=1)

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

Адриана обнови решението на 02.11.2022 17:24 (преди над 1 година)

+dict_mobile_phone = {2: ['A', 'B', 'C'], 3: ['D', 'E', 'F'], 4: ['G', 'H', 'I'], 5: ['J', 'K', 'L'], 6: ['M', 'N', 'O'],
+ 7: ['P', 'Q', 'R', 'S'], 8: ['T', 'U', 'V'], 9: ['W', 'H', 'Y', 'Z'], 0: [' '], 1: []}
+
+
+def nums_to_text(nums):
+ count = 0
+ index = 0
+ result = ""
+ length = len(nums)
+ for i in nums:
+ next_el = -5
+ if index < length - 1:
+ next_el = nums[index + 1]
+ index = index + 1
+ if i == next_el:
+ count = count + 1
+ elif i == -1:
+ count = 0
+ else:
+ temp = count
+ if i == 7 or i == 8:
+ temp = count % 4
+ else:
+ temp = count % 3
+ for el in dict_mobile_phone[i]:
+ if temp == 0:
+ result = result + el
+ break
+ else:
+ temp = temp - 1
+ count = 0
+ return result
+
+
+def text_to_nums(text):
+ result = []
+ for char in text:
+ index = 0
+ for keys in dict_mobile_phone:

Ако обърнеш речника (направиш друг, в който размениш местата на ключ/стойност), ще спестиш един цикъл.
Това, разбира се, можеш да направиш динамично. Не е нужно да го дефинираш ръчно. Просто обхождаш старта дефиниция в началото на файла и правиш обратната, така че функциите ти после да могат да я използват.

+ for el in dict_mobile_phone[keys]:
+ index = index + 1
+ if char.upper() == el:
+ while index > 0:
+ result.append(keys)
+ index = index - 1
+ continue
+ index = 0
+ return result
+
+
+def nums_to_angle(nums):
+ result = 0
+ for el in nums:
+ if 0 < el <= 9:
+ result = result + el * 30
+ elif el == 0:
+ result = result + 300
+ else:
+ continue
+ while result >= 360:
+ result = result - 360
+ return result
+
+
+def angles_to_nums(angles):
+ result = []
+ for el in angles:
+ if 0 <= el < 360 and el % 30 == 0 and el < 330:
+ result.append(el // 30)
+ else:
+ while el < 0:
+ el = 360 + el
+ while el >= 360:
+ el = el - 360
+ if el % 30 == 0 and el < 330:
+ result.append(el // 30)
+ else:
+ temp = el % 30
+ if temp > 15:
+ el = 30 - temp + el
+ else:
+ el = el - temp
+ if el != 0 and el < 330:
+ result.append(el // 30)
+ return result
+
+
+def is_phone_tastic(word):
+ text = text_to_nums(word)
+ angle = nums_to_angle(text)
+ length = len(word)
+ if angle % length == 0:
+ return True
+ else:
+ return False