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

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

Към профила на Роберт Борисов

Резултати

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

Код

def nums_to_text(nums):
rules = {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')}
current_num, counter, result, nums_copy = 0, 0, '', list(nums) # creating a new list, so I can append the -1 at the
# end and not modify the given list
if not nums:
return ''
current_num = nums[0]
nums_copy.append(-1) # appending -1 at the end of the list ,so I don't have to check if I've reached the end of the
# list and manually append the last letter
for num in nums_copy:
if current_num == num:
counter += 1
else:
if current_num in rules:
overlap = len(rules[current_num]) # saving the length of the current tuple in case we get out of range
result = ''.join((result, rules[current_num][(counter % overlap) - 1]))
counter = 1
current_num = num
return result
def text_to_nums(text):
rules = {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')}
nums = []
old_rule = -10
for letter in text.upper():
for rule in rules:
if letter in rules[rule]:
if rule == old_rule:
nums.append(-1)
times = rules[rule].index(letter) + 1
nums.extend([rule] * times)
old_rule = rule
return nums
def nums_to_angle(nums):
result = 0
for num in nums:
if num >= 0:
result += num
return (result % 12) * 30
def angles_to_nums(angles):
nums = []
for angle in angles:
new_angle = angle % 360 # % takes care of the negative numbers
if 15 < new_angle <= 315:
rem = (new_angle % 30) / 30
new_angle = int(new_angle / 30)
if rem > 0.5:
nums.append(new_angle + 1)
else:
nums.append(new_angle)
return nums
def is_phone_tastic(word):
nums = text_to_nums(word)
sum_angles = nums_to_angle(nums)
return not sum_angles % len(word)

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

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

First differing element 7:
10
0

- [5, 1, 2, 4, 9, 1, 8, 10, 9]
?                       -

+ [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.345s

FAILED (failures=3, errors=1)

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

Роберт обнови решението на 01.11.2022 20:04 (преди над 1 година)

+def nums_to_text(nums):
+ rules = {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')}
+ current_num, counter, result, nums1 = nums[0], 0, '', nums # creating a new list, so I can append the -1 at the
+ # end and not modify the given list
+ nums1.append(-1) # appending -1 at the end of the list ,so I don't have to check if I've reached the end of the
+ # list and manually append the last letter
+ for num in nums1:
+ if current_num == num:
+ counter += 1
+ else:
+ if current_num in rules:
+ overlap = len(rules[current_num]) # saving the length of the current tuple in case we get out of range
+ result = ''.join((result, rules[current_num][(counter % overlap) - 1]))
+ counter = 1
+ current_num = num
+ return result
+
+
+def text_to_nums(text):
+ rules = {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')}
+ nums = []
+ old_rule = -10
+ for letter in text:
+ upper_letter = letter.upper()
+ for rule in rules:
+ if upper_letter in rules[rule]:
+ if rule == old_rule:
+ nums.append(-1)
+ times = rules[rule].index(upper_letter) + 1
+ nums.extend([rule] * times)
+ old_rule = rule
+
+ return nums
+
+
+def nums_to_angle(nums):
+ return (sum(nums) % 12) * 30
+
+
+def angles_to_nums(angles):
+ nums = []
+ for angle in angles:
+ new_angle = angle % 360 # % takes care of the negative numbers
+ if 15 < new_angle <= 315:
+ rem = new_angle % 30
+ new_angle = int(new_angle / 30)
+ if rem > 0.5:
+ nums.append(new_angle + 1)
+ else:
+ nums.append(new_angle)
+
+ return nums
+
+
+def is_phone_tastic(word):
+ nums = text_to_nums(word)
+ sum_angles = nums_to_angle(nums)
+ if not (sum_angles % len(word)):
+ return True
+ return False
+

Роберт обнови решението на 01.11.2022 20:09 (преди над 1 година)

def nums_to_text(nums):
rules = {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')}
- current_num, counter, result, nums1 = nums[0], 0, '', nums # creating a new list, so I can append the -1 at the
+ current_num, counter, result, nums_copy = nums[0], 0, '', nums # creating a new list, so I can append the -1 at the
# end and not modify the given list
- nums1.append(-1) # appending -1 at the end of the list ,so I don't have to check if I've reached the end of the
+ nums_copy.append(-1) # appending -1 at the end of the list ,so I don't have to check if I've reached the end of the
# list and manually append the last letter
- for num in nums1:
+ for num in nums_copy:
if current_num == num:
counter += 1
else:
if current_num in rules:
overlap = len(rules[current_num]) # saving the length of the current tuple in case we get out of range
result = ''.join((result, rules[current_num][(counter % overlap) - 1]))
counter = 1
current_num = num
return result
def text_to_nums(text):
rules = {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')}
nums = []
old_rule = -10
for letter in text:
upper_letter = letter.upper()
for rule in rules:
if upper_letter in rules[rule]:
if rule == old_rule:
nums.append(-1)
times = rules[rule].index(upper_letter) + 1
nums.extend([rule] * times)
old_rule = rule
return nums
def nums_to_angle(nums):
return (sum(nums) % 12) * 30
def angles_to_nums(angles):
nums = []
for angle in angles:
new_angle = angle % 360 # % takes care of the negative numbers
if 15 < new_angle <= 315:
rem = new_angle % 30
new_angle = int(new_angle / 30)
if rem > 0.5:
nums.append(new_angle + 1)
else:
nums.append(new_angle)
return nums
def is_phone_tastic(word):
nums = text_to_nums(word)
sum_angles = nums_to_angle(nums)
if not (sum_angles % len(word)):
return True
- return False
-
+ return False

Роберт обнови решението на 01.11.2022 22:48 (преди над 1 година)

def nums_to_text(nums):
rules = {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')}
- current_num, counter, result, nums_copy = nums[0], 0, '', nums # creating a new list, so I can append the -1 at the
+ current_num, counter, result, nums_copy = 0, 0, '', nums # creating a new list, so I can append the -1 at the
# end and not modify the given list
+ if len(nums) == 0:
+ pass
+ else:
+ current_num = nums[0]
nums_copy.append(-1) # appending -1 at the end of the list ,so I don't have to check if I've reached the end of the
# list and manually append the last letter
for num in nums_copy:
if current_num == num:
counter += 1
else:
if current_num in rules:
overlap = len(rules[current_num]) # saving the length of the current tuple in case we get out of range
result = ''.join((result, rules[current_num][(counter % overlap) - 1]))
counter = 1
current_num = num
return result
def text_to_nums(text):
rules = {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')}
nums = []
old_rule = -10
for letter in text:
upper_letter = letter.upper()
for rule in rules:
if upper_letter in rules[rule]:
if rule == old_rule:
nums.append(-1)
times = rules[rule].index(upper_letter) + 1
nums.extend([rule] * times)
old_rule = rule
return nums
def nums_to_angle(nums):
return (sum(nums) % 12) * 30
def angles_to_nums(angles):
nums = []
for angle in angles:
new_angle = angle % 360 # % takes care of the negative numbers
if 15 < new_angle <= 315:
rem = new_angle % 30
new_angle = int(new_angle / 30)
if rem > 0.5:
nums.append(new_angle + 1)
else:
nums.append(new_angle)
return nums
def is_phone_tastic(word):
nums = text_to_nums(word)
sum_angles = nums_to_angle(nums)
if not (sum_angles % len(word)):
return True
- return False
+ return False

Роберт обнови решението на 02.11.2022 15:52 (преди над 1 година)

def nums_to_text(nums):
rules = {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')}
current_num, counter, result, nums_copy = 0, 0, '', nums # creating a new list, so I can append the -1 at the
# end and not modify the given list
if len(nums) == 0:
pass
else:
current_num = nums[0]
nums_copy.append(-1) # appending -1 at the end of the list ,so I don't have to check if I've reached the end of the
# list and manually append the last letter
for num in nums_copy:
if current_num == num:
counter += 1
else:
if current_num in rules:
overlap = len(rules[current_num]) # saving the length of the current tuple in case we get out of range
result = ''.join((result, rules[current_num][(counter % overlap) - 1]))
counter = 1
current_num = num
return result
def text_to_nums(text):
rules = {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')}
nums = []
old_rule = -10
for letter in text:
upper_letter = letter.upper()
for rule in rules:
if upper_letter in rules[rule]:
if rule == old_rule:
nums.append(-1)
times = rules[rule].index(upper_letter) + 1
nums.extend([rule] * times)
old_rule = rule
return nums
def nums_to_angle(nums):
- return (sum(nums) % 12) * 30
+ result = 0
+ for num in nums:
+ if num >= 0:
+ result += num
+ return (result % 12) * 30
def angles_to_nums(angles):
nums = []
for angle in angles:
new_angle = angle % 360 # % takes care of the negative numbers
if 15 < new_angle <= 315:
rem = new_angle % 30
new_angle = int(new_angle / 30)
if rem > 0.5:
nums.append(new_angle + 1)
else:
nums.append(new_angle)
return nums
def is_phone_tastic(word):
nums = text_to_nums(word)
sum_angles = nums_to_angle(nums)
if not (sum_angles % len(word)):
return True
return False

Роберт обнови решението на 02.11.2022 18:29 (преди над 1 година)

def nums_to_text(nums):
rules = {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')}
current_num, counter, result, nums_copy = 0, 0, '', nums # creating a new list, so I can append the -1 at the

nums_copy = nums не създава нов списък. Само кара nums_copy да сочи към същият списък!
Пак ще създадеш страничният ефект, който се опитваш да избегнеш.

>>> nums = [1, 2, 3]
>>> nums_copy = nums
>>> nums_copy.append(-1)
>>> nums
[1, 2, 3, -1]
# end and not modify the given list
if len(nums) == 0:

Ако ще е pass, тогава можеш просто да имаш

if nums:
current_num = nums[0]

План Б - ако знаеш, че при празен списък директно връщаш празен резултат, защо просто не го направиш с тази мисъл:

if not nums:
    return ''
<останалият код идва тук, защото няма да се изпълни ако излезем от функцията>
pass
else:
current_num = nums[0]
nums_copy.append(-1) # appending -1 at the end of the list ,so I don't have to check if I've reached the end of the
# list and manually append the last letter
for num in nums_copy:
if current_num == num:
counter += 1
else:
if current_num in rules:
overlap = len(rules[current_num]) # saving the length of the current tuple in case we get out of range
result = ''.join((result, rules[current_num][(counter % overlap) - 1]))
counter = 1
current_num = num
return result
def text_to_nums(text):
rules = {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')}
nums = []
old_rule = -10
for letter in text:
upper_letter = letter.upper()
for rule in rules:
if upper_letter in rules[rule]:
if rule == old_rule:
nums.append(-1)
times = rules[rule].index(upper_letter) + 1
nums.extend([rule] * times)
old_rule = rule
return nums
def nums_to_angle(nums):
result = 0
for num in nums:
if num >= 0:
result += num
return (result % 12) * 30
def angles_to_nums(angles):
nums = []
for angle in angles:
new_angle = angle % 360 # % takes care of the negative numbers
if 15 < new_angle <= 315:
- rem = new_angle % 30
+ rem = (new_angle % 30) / 30
new_angle = int(new_angle / 30)
if rem > 0.5:
nums.append(new_angle + 1)
else:
nums.append(new_angle)
return nums
def is_phone_tastic(word):
nums = text_to_nums(word)
sum_angles = nums_to_angle(nums)
if not (sum_angles % len(word)):
return True
return False

Роберт обнови решението на 03.11.2022 11:52 (преди над 1 година)

def nums_to_text(nums):
rules = {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')}
- current_num, counter, result, nums_copy = 0, 0, '', nums # creating a new list, so I can append the -1 at the
+ current_num, counter, result, nums_copy = 0, 0, '', list(nums) # creating a new list, so I can append the -1 at the
# end and not modify the given list
- if len(nums) == 0:
- pass
- else:
- current_num = nums[0]
+ if not nums:
+ return ''
+ current_num = nums[0]
nums_copy.append(-1) # appending -1 at the end of the list ,so I don't have to check if I've reached the end of the
# list and manually append the last letter
for num in nums_copy:
if current_num == num:
counter += 1
else:
if current_num in rules:
overlap = len(rules[current_num]) # saving the length of the current tuple in case we get out of range
result = ''.join((result, rules[current_num][(counter % overlap) - 1]))
counter = 1
current_num = num
return result
def text_to_nums(text):
rules = {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')}
nums = []
old_rule = -10
- for letter in text:
- upper_letter = letter.upper()
+ for letter in text.upper():
for rule in rules:
- if upper_letter in rules[rule]:
+ if letter in rules[rule]:
if rule == old_rule:
nums.append(-1)
- times = rules[rule].index(upper_letter) + 1
+ times = rules[rule].index(letter) + 1
nums.extend([rule] * times)
old_rule = rule
return nums
def nums_to_angle(nums):
result = 0
for num in nums:
if num >= 0:
result += num
return (result % 12) * 30
def angles_to_nums(angles):
nums = []
for angle in angles:
new_angle = angle % 360 # % takes care of the negative numbers
if 15 < new_angle <= 315:
rem = (new_angle % 30) / 30
new_angle = int(new_angle / 30)
if rem > 0.5:
nums.append(new_angle + 1)
else:
nums.append(new_angle)
return nums
def is_phone_tastic(word):
nums = text_to_nums(word)
sum_angles = nums_to_angle(nums)
- if not (sum_angles % len(word)):
- return True
+ return not sum_angles % len(word)
- return False