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

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

Към профила на Рая Симеонова

Резултати

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

Код

ANGLE_DIFF = 30
MIN_ANGLE_TURNABOUT = 360
def nums_to_angle(nums):
sum_angles = 0
for num in nums:
if num == 0:
angle = 300
else:
angle = num * ANGLE_DIFF
sum_angles += angle
while sum_angles >= MIN_ANGLE_TURNABOUT:
sum_angles -= MIN_ANGLE_TURNABOUT
return sum_angles
def angles_to_nums(angles):
nums = []
for angle in angles:
while angle < 0:
angle += MIN_ANGLE_TURNABOUT
while angle >= MIN_ANGLE_TURNABOUT:
angle -= MIN_ANGLE_TURNABOUT
digit = round(angle / ANGLE_DIFF)
if digit == 10:
nums.append(0)
elif digit > 0 and digit < 10:
nums.append(digit)
return nums
def nums_to_text(nums):
num_to_symbols = {
2: 'ABC',
3: 'DEF',
4: 'GHI',
5: 'JKL',
6: 'MNO',
7: 'PQRS',
8: 'TUV',
9: 'WXYZ',
0: ' ',
}
text = ''
num_occurances = 0
current_num = 0
is_first_iteration = True
nums.append(1) # adding 1 as buffer so that the last num could be processed in the loop
for num in nums:
if is_first_iteration:
current_num = num
is_first_iteration = False
continue
if current_num == num:
num_occurances += 1
continue
if current_num in num_to_symbols:
symbols_from_num = num_to_symbols[current_num]
symbols_from_num_len = len(symbols_from_num)
while num_occurances >= symbols_from_num_len:
num_occurances -= symbols_from_num_len
text += symbols_from_num[num_occurances]
num_occurances = 0
current_num = num
nums.pop() # removing the buffer
return text
def text_to_nums(text):
# for eeach symbol the tuple coressponding is (times needed to hit the digit, digit to hit)
symbol_to_num = {
'A': (1, 2),
'B': (2, 2),
'C': (3, 2),
'D': (1, 3),
'E': (2, 3),
'F': (3, 3),
'G': (1, 4),
'H': (2, 4),
'I': (3, 4),
'J': (1, 5),
'K': (2, 5),
'L': (3, 5),
'M': (1, 6),
'N': (2, 6),
'O': (3, 6),
'P': (1, 7),
'Q': (2, 7),
'R': (3, 7),
'S': (4, 7),
'T': (1, 8),
'U': (2, 8),
'V': (3, 8),
'W': (1, 9),
'X': (2, 9),
'Y': (3, 9),
'Z': (4, 9),
' ': (1, 0),
}
nums = []
last_num = -1
for char in text:
times, num = symbol_to_num[char.upper()]
if last_num == num:
nums.append(-1)
nums.extend([num] * times)
last_num = num
return nums
def is_phone_tastic(word):
nums = text_to_nums(word)
angle = nums_to_angle(nums)
return angle % 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.496s

FAILED (failures=2, errors=1)

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

Рая обнови решението на 02.11.2022 00:54 (преди над 1 година)

+def nums_to_angle(nums):
+ sum_angles = 0
+ angle_diff = 30

Такива променливи, които ще ползваш на няколко места (nums_to_angle, angle_to_nums) е нелошо да бъдат дефинирани като константи (спазвайки конвенцията за именуване на константи) извън тялото на функцията.

+ min_angle_turnabout = 360
+
+ for num in nums:
+ if num == 0:
+ angle = 300
+ else:
+ angle = num * angle_diff
+
+ sum_angles += angle
+
+ while sum_angles >= min_angle_turnabout:
+ sum_angles -= min_angle_turnabout
+
+ return sum_angles
+
+def angles_to_nums(angles):
+ angle_diff = 30
+ min_angle_turnabout = 360
+ nums = []
+ for angle in angles:
+ while angle < 0:
+ angle += min_angle_turnabout
+ while angle >= min_angle_turnabout:
+ angle -= min_angle_turnabout
+
+ digit = round(angle / angle_diff)
+
+ if digit == 10:
+ nums.append(0)
+ elif digit > 0 and digit < 10:
+ nums.append(digit)
+
+ return nums
+
+def nums_to_text(nums):
+ num_to_symbols = {
+ 2: 'ABC',
+ 3: 'DEF',
+ 4: 'GHI',
+ 5: 'JKL',
+ 6: 'MNO',
+ 7: 'PQRS',
+ 8: 'TUV',
+ 9: 'WXYZ',
+ 0: ' ',
+ }
+ text = ''
+ num_occurances = 0
+ current_num = 0
+ is_first_iteration = True
+
+ buffer = 1
+ nums.append(buffer) # adding buffer so that the last num could be processed in the loop
+
+ for num in nums:
+ if is_first_iteration:
+ current_num = num
+ is_first_iteration = False
+ continue
+
+ if current_num == num:
+ num_occurances += 1
+ continue
+
+ if current_num in num_to_symbols:
+ symbols_from_num = num_to_symbols[current_num]
+ symbols_from_num_len = len(symbols_from_num)
+ while num_occurances >= symbols_from_num_len:
+ num_occurances -= symbols_from_num_len
+
+ text += symbols_from_num[num_occurances]
+
+ num_occurances = 0
+ current_num = num
+
+ nums.pop() # removing the buffer
+ return text
+
+
+def text_to_nums(text):
+ # for eeach symbol the tuple coressponding is (times needed to hit the digit, digit to hit)
+ symbol_to_num = {
+ 'A': (1, 2),
+ 'B': (2, 2),
+ 'C': (3, 2),
+ 'D': (1, 3),
+ 'E': (2, 3),
+ 'F': (3, 3),
+ 'G': (1, 4),
+ 'H': (2, 4),
+ 'I': (3, 4),
+ 'J': (1, 5),
+ 'K': (2, 5),
+ 'L': (3, 5),
+ 'M': (1, 6),
+ 'N': (2, 6),
+ 'O': (3, 6),
+ 'P': (1, 7),
+ 'Q': (2, 7),
+ 'R': (3, 7),
+ 'S': (4, 7),
+ 'T': (1, 8),
+ 'U': (2, 8),
+ 'V': (3, 8),
+ 'W': (1, 9),
+ 'X': (2, 9),
+ 'Y': (3, 9),
+ 'Z': (4, 9),
+ ' ': (1, 0),
+ }
+ nums = []
+ last_num = -1
+ for char in text:
+ times, num = symbol_to_num[char.upper()]
+ if last_num == num:
+ nums.append(-1)
+
+ for _ in range(times):
+ nums.append(num)
+
+ last_num = num
+
+ return nums
+
+
+def is_phone_tastic(word):
+ nums = text_to_nums(word)
+ angle = nums_to_angle(nums)
+
+ return angle % len(word) == 0

Логиката в ядрото на nums_to_text ми е малко Fortran-ска (да се чете "като на C"), но не мога да отрека, че ще работи.
За празните редове ще си поговорим днес на лекцията.

Останалото е добре. :)

Рая обнови решението на 03.11.2022 12:56 (преди над 1 година)

+ANGLE_DIFF = 30
+MIN_ANGLE_TURNABOUT = 360
+
def nums_to_angle(nums):
sum_angles = 0
- angle_diff = 30
- min_angle_turnabout = 360
for num in nums:
if num == 0:
angle = 300
else:
- angle = num * angle_diff
+ angle = num * ANGLE_DIFF
sum_angles += angle
- while sum_angles >= min_angle_turnabout:
- sum_angles -= min_angle_turnabout
+ while sum_angles >= MIN_ANGLE_TURNABOUT:
+ sum_angles -= MIN_ANGLE_TURNABOUT
return sum_angles
def angles_to_nums(angles):
- angle_diff = 30
- min_angle_turnabout = 360
nums = []
for angle in angles:
while angle < 0:
- angle += min_angle_turnabout
- while angle >= min_angle_turnabout:
- angle -= min_angle_turnabout
+ angle += MIN_ANGLE_TURNABOUT
+ while angle >= MIN_ANGLE_TURNABOUT:
+ angle -= MIN_ANGLE_TURNABOUT
- digit = round(angle / angle_diff)
+ digit = round(angle / ANGLE_DIFF)
if digit == 10:
nums.append(0)
elif digit > 0 and digit < 10:
nums.append(digit)
return nums
def nums_to_text(nums):
num_to_symbols = {
2: 'ABC',
3: 'DEF',
4: 'GHI',
5: 'JKL',
6: 'MNO',
7: 'PQRS',
8: 'TUV',
9: 'WXYZ',
0: ' ',
}
text = ''
num_occurances = 0
current_num = 0
is_first_iteration = True
- buffer = 1
- nums.append(buffer) # adding buffer so that the last num could be processed in the loop
+ nums.append(1) # adding 1 as buffer so that the last num could be processed in the loop
for num in nums:
if is_first_iteration:
current_num = num
is_first_iteration = False
continue
if current_num == num:
num_occurances += 1
continue
if current_num in num_to_symbols:
symbols_from_num = num_to_symbols[current_num]
symbols_from_num_len = len(symbols_from_num)
while num_occurances >= symbols_from_num_len:
num_occurances -= symbols_from_num_len
text += symbols_from_num[num_occurances]
num_occurances = 0
current_num = num
nums.pop() # removing the buffer
return text
def text_to_nums(text):
# for eeach symbol the tuple coressponding is (times needed to hit the digit, digit to hit)
symbol_to_num = {
'A': (1, 2),
'B': (2, 2),
'C': (3, 2),
'D': (1, 3),
'E': (2, 3),
'F': (3, 3),
'G': (1, 4),
'H': (2, 4),
'I': (3, 4),
'J': (1, 5),
'K': (2, 5),
'L': (3, 5),
'M': (1, 6),
'N': (2, 6),
'O': (3, 6),
'P': (1, 7),
'Q': (2, 7),
'R': (3, 7),
'S': (4, 7),
'T': (1, 8),
'U': (2, 8),
'V': (3, 8),
'W': (1, 9),
'X': (2, 9),
'Y': (3, 9),
'Z': (4, 9),
' ': (1, 0),
}
nums = []
last_num = -1
for char in text:
times, num = symbol_to_num[char.upper()]
if last_num == num:
nums.append(-1)
- for _ in range(times):
- nums.append(num)
+ nums.extend([num] * times)
last_num = num
return nums
def is_phone_tastic(word):
nums = text_to_nums(word)
angle = nums_to_angle(nums)
return angle % len(word) == 0