Решение на Телефонна любов от Симеон Йорданов

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

Към профила на Симеон Йорданов

Резултати

  • 7 точки от тестове
  • 0 бонус точки
  • 7 точки общо
  • 26 успешни тест(а)
  • 11 неуспешни тест(а)

Код

def nums_to_text(nums: list[int]):
word=""
skipper=0
table=[['a','b','c'],
['d','e','f'],
['g','h','i'],
['j','k','l'],
['m','n','o'],
['p','q','r','s'],
['t','u','v'],
['w','x','y','z']]
for i in range(len(nums)):
if(skipper>0): #I wanted to skip iterations
skipper-=1 #but "i+=5" didn't work
continue #there must be more elegant way
#but I don't know it :(
if(nums[i]==-1):
continue
count=1
while(i+1!=len(nums) and nums[i]==nums[i+1]):
i+=1
count+=1
skipper=count-1
count=count%len(table[nums[i]-2])
word+=table[nums[i]-2][count-1]
return word
def text_to_nums(text:str):
text=text.lower()
list_int:list[int]=[]
tmp:str=""
table={" ":"0","a":"2","b":"22","c":"222",
"d":"3","e":"33","f":"333",
"g":"4","h":"44","i":"444",
"j":"5","k":"55","l":"555",
"m":"6","n":"66","o":"666",
"p":"7","q":"77","r":"777","s":"7777",
"t":"8","u":"88","v":"888",
"w":"9","x":"99","y":"999","z":"9999"}
for letter in text:
tmp+=table[letter]
for letter in tmp:
data_int=ord(letter)-ord('0')
list_int.append(data_int)
return list_int
def nums_to_angle(nums:list[int]):
sum=0
for num in nums:
if(num==0):
sum+=300
else:
sum+=num*30
return sum%360
def angles_to_nums(angles:list[int]):
phone_nums:list[int]=[]
for angle in angles:
while(angle<0):
angle+=360
angle%=360
match angle:
case angle if(angle>=15 and angle<=44):
phone_nums.append(1)
case angle if(angle>=45 and angle<=74):
phone_nums.append(2)
case angle if(angle>=75 and angle<=104):
phone_nums.append(3)
case angle if(angle>=105 and angle<=134):
phone_nums.append(4)
case angle if(angle>=135 and angle<=164):
phone_nums.append(5)
case angle if(angle>=165 and angle<=194):
phone_nums.append(6)
case angle if(angle>=195 and angle<=224):
phone_nums.append(7)
case angle if(angle>=225 and angle<=254):
phone_nums.append(8)
case angle if(angle>=255 and angle<=359):
phone_nums.append(9)
return phone_nums
def is_phone_tastic(word:str):
list_int:list[int]=text_to_nums(word)

Избягвай стриктно да дефинираш типа на променливите си. В Python го считам за излишно. Точно този пример пък използва и променлива със същото име. Нищо не печелиш. А относно имената, използвай по-описателни. Говорихме след първото домашно, че 'list_int' не дава никаква информация, освен, че държи list от int.

angle=nums_to_angle(list_int)
return angle%len(word)==0

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

..F....FF.E.........F.....F.F.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_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: [9] != []

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

- [9]
?  -

+ []

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

First differing element 2:
1
2

First list contains 3 additional elements.
First extra element 9:
8

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

======================================================================
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: 'wabcdefghijklmnopqrstuvwxyzt' != 'abcdefghijklmnopqrstuvwxyz '
- wabcdefghijklmnopqrstuvwxyzt
? -                          ^
+ abcdefghijklmnopqrstuvwxyz 
?                           ^


======================================================================
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: 'wfmitrulzzz' != 'fmi rulzzz'
- wfmitrulzzz
? -   ^
+ fmi rulzzz
?    ^


======================================================================
FAIL: test_spaces_only (test.TestNumsToText)
Test for input of only whitespaces with or without -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: 'ttt' not found in ('   ', '      ')

======================================================================
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.357s

FAILED (failures=10, errors=1)

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

Симеон обнови решението на 02.11.2022 17:15 (преди над 1 година)

+def nums_to_text(nums: list[int]):
+
+ word=""
+ skipper=0
+ table=[['a','b','c'],
+ ['d','e','f'],
+ ['g','h','i'],
+ ['j','k','l'],
+ ['m','n','o'],
+ ['p','q','r','s'],
+ ['t','u','v'],
+ ['w','x','y','z']]
+
+ for i in range(len(nums)):
+
+ if(skipper>0): #I wanted to skip iterations
+ skipper-=1 #but "i+=5" didn't work
+ continue #there must be more elegant way
+ #but I don't know it :(
+ if(nums[i]==-1):
+ continue
+
+ count=1
+
+ while(i+1!=len(nums) and nums[i]==nums[i+1]):
+ i+=1
+ count+=1
+
+ skipper=count-1
+ count=count%len(table[nums[i]-2])
+ word+=table[nums[i]-2][count-1]
+
+ return word
+
+def text_to_nums(text:str):
+
+ text=text.lower()
+ list_int:list[int]=[]
+ tmp:str=""
+ table={" ":"0","a":"2","b":"22","c":"222",
+ "d":"3","e":"33","f":"333",
+ "g":"4","h":"44","i":"444",
+ "j":"5","k":"55","l":"555",
+ "m":"6","n":"66","o":"666",
+ "p":"7","q":"77","r":"777","s":"7777",
+ "t":"8","u":"88","v":"888",
+ "w":"9","x":"99","y":"999","z":"9999"}
+
+ for letter in text:
+ tmp+=table[letter]
+
+ for letter in tmp:
+ data_int=ord(letter)-ord('0')
+ list_int.append(data_int)
+
+ return list_int
+
+def nums_to_angle(nums:list[int]):
+
+ sum=0
+
+ for num in nums:
+
+ if(num==0):
+ sum+=300
+
+ else:
+ sum+=num*30
+
+ return sum%360
+
+def angles_to_nums(angles:list[int]):
+
+ phone_nums:list[int]=[]
+
+ for angle in angles:
+
+ while(angle<0):
+ angle+=360
+
+ angle%=360
+ match angle:
+
+ case angle if(angle>=15 and angle<=44):
+ phone_nums.append(1)
+
+ case angle if(angle>=45 and angle<=74):
+ phone_nums.append(2)
+
+ case angle if(angle>=75 and angle<=104):
+ phone_nums.append(3)
+
+ case angle if(angle>=105 and angle<=134):
+ phone_nums.append(4)
+
+ case angle if(angle>=135 and angle<=164):
+ phone_nums.append(5)
+
+ case angle if(angle>=165 and angle<=194):
+ phone_nums.append(6)
+
+ case angle if(angle>=195 and angle<=224):
+ phone_nums.append(7)
+
+ case angle if(angle>=225 and angle<=254):
+ phone_nums.append(8)
+
+ case angle if(angle>=255 and angle<=359):
+ phone_nums.append(9)
+
+ return phone_nums
+
+def is_phone_tastic(word:str):
+
+ list_int:list[int]=text_to_nums(word)

Избягвай стриктно да дефинираш типа на променливите си. В Python го считам за излишно. Точно този пример пък използва и променлива със същото име. Нищо не печелиш. А относно имената, използвай по-описателни. Говорихме след първото домашно, че 'list_int' не дава никаква информация, освен, че държи list от int.

+ angle=nums_to_angle(list_int)
+
+ return angle%len(word)==0