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

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

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

Резултати

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

Код

"""
1-> pass
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,x,y,z
0-> _ interval
-1 -> new char with the same num
"""
def get_letter(num, count):
nums_chars = {-1: ('',),
1: ('',),
0: (' ',),
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', 'X', 'Y', 'Z')
}
while count >= len(nums_chars[num]):
count -= len(nums_chars[num])
return nums_chars[num][count]
def nums_to_text(nums):
text = ""
c = 0

Кодът е сравнително прост и мога да се ориентирам какво е c. Все пак, count е малко "по-трудоемко" за писане, но е доста по-удобно за четене в последствие.

previous = 1
for n in nums:
if n == 1: # we need to add the previous letter
text += get_letter(previous, c)
c = 0
elif n == -1:
# if c != 0:
# print(f"We are on {n} and c={c}")
text += get_letter(previous, c)
# print(text)
c = 0
else:
if previous == n:
c += 1
# print(f"We are on {n} and c={c}")
else:
text += get_letter(previous, c)
# print(text)
c = 0
previous = n
# if c != 0:
text += get_letter(previous, c)
# print(text)
return text
def text_to_nums(text):
nums = []
char_nums = {'': (1,),
' ': (0,),
'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)
}
previous = ''
for ch in text:
if char_nums[ch.upper()][-1] == char_nums[previous.upper()][0]:
nums.append(-1)
for n in char_nums[ch.upper()]:
nums.append(n)
previous = ch
return nums
def normalization(angle):
while angle > 359:
angle -= 360
while angle < 0:
angle += 360
return angle
def nums_to_angle(nums):
angle = 0
base_angle = 30
for n in nums:
if not n == -1:
angle += n
angle *= base_angle
return normalization(angle)
def round_angle(angle):
angle = normalization(angle)
if 0 <= angle <= 15:
return 0
elif 15 < angle <= 45:
return 30
elif 45 < angle <= 75:
return 60
elif 75 < angle <= 105:
return 90
elif 105 < angle <= 135:
return 120
elif 135 < angle <= 165:
return 150
elif 165 < angle <= 195:
return 180
elif 195 < angle <= 225:
return 210
elif 225 < angle <= 255:
return 240
elif 255 < angle <= 285:
return 270
elif 285 < angle <= 315:
return 300
elif 315 < angle <= 345:
return 330
else:
return 0
def angles_to_nums(angles):
nums = []
for a in angles:
a = round_angle(a)
if a >= 300:
a = 0
if not a == 0:
a /= 30
nums.append(a)
return nums
def is_phone_tastic(word):
length = len(word)
nums = text_to_nums(word)
return (nums_to_angle(nums) % length) == 0
# print(angles_to_nums([10, -30, 300, 200])) # [7]
# print(nums_to_angle([2, 2, -1, 2])) # 180

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

.......F..E..F....F..................
======================================================================
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.0, 2.0, 4.0, 9.0, 1.0, 8.0, 9.0] != [5, 1, 2, 4, 9, 1, 8, 0, 9]

First differing element 7:
9.0
0

Second list contains 1 additional elements.
First extra element 8:
9

- [5.0, 1.0, 2.0, 4.0, 9.0, 1.0, 8.0, 9.0]
+ [5, 1, 2, 4, 9, 1, 8, 0, 9]

======================================================================
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_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

----------------------------------------------------------------------
Ran 37 tests in 0.450s

FAILED (failures=3, errors=1)

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

Теодора обнови решението на 02.11.2022 08:36 (преди над 1 година)

+"""
+1-> pass
+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,x,y,z
+0-> _ interval
+-1 -> new char with the same num
+"""
+
+
+def get_letter(num, count):
+ nums_chars = {-1: ('',),
+ 1: ('',),
+ 0: (' ',),
+ 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', 'X', 'Y', 'Z')
+ }
+ while count >= len(nums_chars[num]):
+ count -= len(nums_chars[num])
+ return nums_chars[num][count]
+
+
+def nums_to_text(nums):
+ text = ""
+ c = 0
+ previous = 1
+ for n in nums:
+ if n == 1: # we need to add the previous letter
+ text += get_letter(previous, c)
+ c = 0
+ elif n == -1:
+ # if c != 0:
+ # print(f"We are on {n} and c={c}")
+ text += get_letter(previous, c)
+ # print(text)
+ c = 0
+ else:
+ if previous == n:
+ c += 1
+ # print(f"We are on {n} and c={c}")
+ else:
+ text += get_letter(previous, c)
+ # print(text)
+ c = 0
+ previous = n
+
+ # if c != 0:
+ text += get_letter(previous, c)
+ # print(text)
+
+ return text
+
+
+def text_to_nums(text):
+ nums = []
+ char_nums = {'': (1,),
+ ' ': (0,),
+ '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)
+ }
+ previous = ''
+ for ch in text:
+ if char_nums[ch.upper()][-1] == char_nums[previous.upper()][0]:
+ nums.append(-1)
+ for n in char_nums[ch.upper()]:
+ nums.append(n)
+ previous = ch
+ return nums
+
+
+def normalization(angle):
+ while angle > 359:
+ angle -= 360
+ while angle < 0:
+ angle += 360
+ return angle
+
+
+def nums_to_angle(nums):
+ angle = 0
+ base_angle = 30
+ angle = sum(nums) * base_angle
+ return normalization(angle)
+
+
+def round_angle(angle):
+ angle = normalization(angle)
+ if 0 <= angle <= 15:
+ return 0
+ elif 15 < angle <= 45:
+ return 30
+ elif 45 < angle <= 75:
+ return 60
+ elif 75 < angle <= 105:
+ return 90
+ elif 105 < angle <= 135:
+ return 120
+ elif 135 < angle <= 165:
+ return 150
+ elif 165 < angle <= 195:
+ return 180
+ elif 195 < angle <= 225:
+ return 210
+ elif 225 < angle <= 255:
+ return 240
+ elif 255 < angle <= 285:
+ return 270
+ elif 285 < angle <= 315:
+ return 300
+ elif 315 < angle <= 345:
+ return 330
+ else:
+ return 0
+
+
+def angles_to_nums(angles):
+ nums = []
+ for a in angles:
+ a = round_angle(a)
+ if not a == 0:
+ a /= 30
+ nums.append(a)
+ return nums
+
+
+def is_phone_tastic(word):
+ length = len(word)
+ nums = text_to_nums(word)
+ return (nums_to_angle(nums) % length) == 0

Теодора обнови решението на 02.11.2022 12:12 (преди над 1 година)

"""
1-> pass
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,x,y,z
0-> _ interval
-1 -> new char with the same num
"""
def get_letter(num, count):
nums_chars = {-1: ('',),
1: ('',),
0: (' ',),
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', 'X', 'Y', 'Z')
}
while count >= len(nums_chars[num]):
count -= len(nums_chars[num])
return nums_chars[num][count]
def nums_to_text(nums):
text = ""
c = 0
previous = 1
for n in nums:
if n == 1: # we need to add the previous letter
text += get_letter(previous, c)
c = 0
elif n == -1:
# if c != 0:
# print(f"We are on {n} and c={c}")
text += get_letter(previous, c)
# print(text)
c = 0
else:
if previous == n:
c += 1
# print(f"We are on {n} and c={c}")
else:
text += get_letter(previous, c)
# print(text)
c = 0
previous = n
# if c != 0:
text += get_letter(previous, c)
# print(text)
return text
def text_to_nums(text):
nums = []
char_nums = {'': (1,),
' ': (0,),
'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)
}
previous = ''
for ch in text:
if char_nums[ch.upper()][-1] == char_nums[previous.upper()][0]:
nums.append(-1)
for n in char_nums[ch.upper()]:
nums.append(n)
previous = ch
return nums
def normalization(angle):
while angle > 359:
angle -= 360
while angle < 0:
angle += 360
return angle
def nums_to_angle(nums):
angle = 0
base_angle = 30
- angle = sum(nums) * base_angle
+ for n in nums:
+ if not n == -1:
+ angle+=n
+ angle *= base_angle
return normalization(angle)
def round_angle(angle):
angle = normalization(angle)
if 0 <= angle <= 15:
return 0
elif 15 < angle <= 45:
return 30
elif 45 < angle <= 75:
return 60
elif 75 < angle <= 105:
return 90
elif 105 < angle <= 135:
return 120
elif 135 < angle <= 165:
return 150
elif 165 < angle <= 195:
return 180
elif 195 < angle <= 225:
return 210
elif 225 < angle <= 255:
return 240
elif 255 < angle <= 285:
return 270
elif 285 < angle <= 315:
return 300
elif 315 < angle <= 345:
return 330
else:
return 0
def angles_to_nums(angles):
nums = []
for a in angles:
a = round_angle(a)
+ if a >= 300:
+ a = 0
if not a == 0:
a /= 30
nums.append(a)
return nums
def is_phone_tastic(word):
length = len(word)
nums = text_to_nums(word)
return (nums_to_angle(nums) % length) == 0
+
+
+# print(angles_to_nums([10, -30, 300, 200])) # [7]
+# print(nums_to_angle([2, 2, -1, 2])) # 180

Теодора обнови решението на 02.11.2022 12:13 (преди над 1 година)

"""
1-> pass
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,x,y,z
0-> _ interval
-1 -> new char with the same num
"""
def get_letter(num, count):
nums_chars = {-1: ('',),
1: ('',),
0: (' ',),
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', 'X', 'Y', 'Z')
}
while count >= len(nums_chars[num]):
count -= len(nums_chars[num])
return nums_chars[num][count]
def nums_to_text(nums):
text = ""
c = 0

Кодът е сравнително прост и мога да се ориентирам какво е c. Все пак, count е малко "по-трудоемко" за писане, но е доста по-удобно за четене в последствие.

previous = 1
for n in nums:
if n == 1: # we need to add the previous letter
text += get_letter(previous, c)
c = 0
elif n == -1:
# if c != 0:
# print(f"We are on {n} and c={c}")
text += get_letter(previous, c)
# print(text)
c = 0
else:
if previous == n:
c += 1
# print(f"We are on {n} and c={c}")
else:
text += get_letter(previous, c)
# print(text)
c = 0
previous = n
# if c != 0:
text += get_letter(previous, c)
# print(text)
return text
def text_to_nums(text):
nums = []
char_nums = {'': (1,),
' ': (0,),
'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)
}
previous = ''
for ch in text:
if char_nums[ch.upper()][-1] == char_nums[previous.upper()][0]:
nums.append(-1)
for n in char_nums[ch.upper()]:
nums.append(n)
previous = ch
return nums
def normalization(angle):
while angle > 359:
angle -= 360
while angle < 0:
angle += 360
return angle
def nums_to_angle(nums):
angle = 0
base_angle = 30
for n in nums:
if not n == -1:
- angle+=n
+ angle += n
angle *= base_angle
return normalization(angle)
def round_angle(angle):
angle = normalization(angle)
if 0 <= angle <= 15:
return 0
elif 15 < angle <= 45:
return 30
elif 45 < angle <= 75:
return 60
elif 75 < angle <= 105:
return 90
elif 105 < angle <= 135:
return 120
elif 135 < angle <= 165:
return 150
elif 165 < angle <= 195:
return 180
elif 195 < angle <= 225:
return 210
elif 225 < angle <= 255:
return 240
elif 255 < angle <= 285:
return 270
elif 285 < angle <= 315:
return 300
elif 315 < angle <= 345:
return 330
else:
return 0
def angles_to_nums(angles):
nums = []
for a in angles:
a = round_angle(a)
if a >= 300:
a = 0
if not a == 0:
a /= 30
nums.append(a)
return nums
def is_phone_tastic(word):
length = len(word)
nums = text_to_nums(word)
return (nums_to_angle(nums) % length) == 0
# print(angles_to_nums([10, -30, 300, 200])) # [7]
# print(nums_to_angle([2, 2, -1, 2])) # 180