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

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

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

Резултати

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

Код

"""Homework 02 module for fmi-intro-to-python-2022"""
# Representation of a phone keyboard
key_phone_dict = {
'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')
}
def nums_to_text(nums):
'''Translate a list of numbers from a phone keypad into text.'''
# Split the input list into sublists, containing only the consecutively
# occurring identical characters
indexes = []
for index, num in enumerate(nums):
if num != nums[index-1] or len(nums) == 1:
indexes.append(index)
indexes.append(len(nums))
separated_list = []
for i, index in enumerate(indexes):
if i != len(indexes) - 1:
sliced_list = nums[index:indexes[i+1]]
separated_list.append(sliced_list)
# Return a string with the translated digits, taken from the corresponding
# dictionary pair
output = ''
for num_element in separated_list:
letter = num_element[0]
current_iter = key_phone_dict.get(str(letter))
if letter in [-1, 1]:
continue
else:
output += current_iter[(len(num_element)%(len(current_iter)))-1]
return output
def text_to_nums(text):
'''Translate a text from a phone keypad into combination of numbers.'''
# Make the input case-insensitive
text_lst = list(text.upper())
# Search for the letter in the dictionary values and translate it using
# the corresponding key
output_list = []
for i, letter in enumerate(text_lst):
for number, letters in key_phone_dict.items():
if letter in letters:
num_times = letters.index(letter) + 1
output_list.extend(int(number) for i in range(num_times))

Не е кой знае какво, но според мен е по-четимо. Пък и дори да не се съгласиш - може да не знаеш, че можеш да умножаваш list

output_list.extend([int(number)] * num_times)

# Check for consecutive duplicating characters
if i+1 != len(text_lst) and text_lst[i+1] == letter:
output_list.append(-1)
return output_list
def nums_to_angle(nums):
'''
Take a list of numbers and return a normalized angle, relative to
the rotary dial phone idea.
'''
angle = 0
sum_of_angles = 0
for num in nums:
if num == 0:
angle = 300
else:
angle = num*30
sum_of_angles += angle
return sum_of_angles%360
def angles_to_nums(angles):
'''
Take a list of angles and return a list of numbers corresponding to them,
relative to the the rotary dial phone idea.
'''
nums = []
for angle in angles:
angle = angle%360
num = angle//30
rem = angle%30
if rem > 15:
num += 1
if num == 0 or num > 10:
continue
if num == 10:
num = 0
nums.append(num)
return nums
def is_phone_tastic(word):
'''Return true/false depending on whether the given word is phone-tastic.'''
# Text-to-nums conversion
ttn_list = text_to_nums(word)
# Normalized angle
n_angle = nums_to_angle(ttn_list)
return (n_angle%len(word) == 0)

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

..........E..........F...F..F.F..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_complex_word (test.TestNumsToText)
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: 'a' != 'ba'
- a
+ ba
? +


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

======================================================================
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: '  ' 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_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, -1, 3] != [2, -1, 2, 2, 3, -1, 3]

First differing element 1:
2
-1

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

- [2, 2, 2, 3, -1, 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, -1, 0, -1, 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.347s

FAILED (failures=6, errors=1)

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

Александра обнови решението на 03.11.2022 17:45 (преди над 1 година)

+"""Homework 02 module for fmi-intro-to-python-2022"""
+
+# Representation of a phone keyboard
+key_phone_dict = {
+ '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')
+}
+
+def nums_to_text(nums):
+ '''Translate a list of numbers from a phone keypad into text.'''
+ # Split the input list into sublists, containing only the consecutively
+ # occurring identical characters
+ indexes = []
+ for index, num in enumerate(nums):
+ if num != nums[index-1] or len(nums) == 1:
+ indexes.append(index)
+ indexes.append(len(nums))
+ separated_list = []
+ for i, index in enumerate(indexes):
+ if i != len(indexes) - 1:
+ sliced_list = nums[index:indexes[i+1]]
+ separated_list.append(sliced_list)
+ # Return a string with the translated digits, taken from the corresponding
+ # dictionary pair
+ output = ''
+ for num_element in separated_list:
+ letter = num_element[0]
+ current_iter = key_phone_dict.get(str(letter))
+ if letter in [-1, 1]:
+ continue
+ else:
+ output += current_iter[(len(num_element)%(len(current_iter)))-1]
+ return output
+
+def text_to_nums(text):
+ '''Translate a text from a phone keypad into combination of numbers.'''
+ # Make the input case-insensitive
+ text_lst = list(text.upper())
+ # Search for the letter in the dictionary values and translate it using
+ # the corresponding key
+ output_list = []
+ for i, letter in enumerate(text_lst):
+ for number, letters in key_phone_dict.items():
+ if letter in letters:
+ num_times = letters.index(letter) + 1
+ output_list.extend(int(number) for i in range(num_times))

Не е кой знае какво, но според мен е по-четимо. Пък и дори да не се съгласиш - може да не знаеш, че можеш да умножаваш list

output_list.extend([int(number)] * num_times)

+ # Check for consecutive duplicating characters
+ if i+1 != len(text_lst) and text_lst[i+1] == letter:
+ output_list.append(-1)
+ return output_list
+
+def nums_to_angle(nums):
+ '''
+ Take a list of numbers and return a normalized angle, relative to
+ the rotary dial phone idea.
+ '''
+ angle = 0
+ sum_of_angles = 0
+ for num in nums:
+ if num == 0:
+ angle = 300
+ else:
+ angle = num*30
+ sum_of_angles += angle
+ return sum_of_angles%360
+
+def angles_to_nums(angles):
+ '''
+ Take a list of angles and return a list of numbers corresponding to them,
+ relative to the the rotary dial phone idea.
+ '''
+ nums = []
+ for angle in angles:
+ angle = angle%360
+ num = angle//30
+ rem = angle%30
+ if rem > 15:
+ num += 1
+ if num == 0 or num > 10:
+ continue
+ if num == 10:
+ num = 0
+ nums.append(num)
+ return nums
+
+def is_phone_tastic(word):
+ '''Return true/false depending on whether the given word is phone-tastic.'''
+ # Text-to-nums conversion
+ ttn_list = text_to_nums(word)
+ # Normalized angle
+ n_angle = nums_to_angle(ttn_list)
+ return (n_angle%len(word) == 0)