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

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

Към профила на Кристиан Какалов

Резултати

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

Код

nums_to_letter = {
2: "A",
22: "B",
222: "C",
3: "D",
33: "E",
333: "F",
4: "G",
44: "H",
444: "I",
5: "J",
55: "K",
555: "L",
6: "M",
66: "N",
666: "O",
7: "P",
77: "Q",
777: "R",
7777: "S",
8: "T",
88: "U",
888: "V",
9: "W",
99: "X",
999: "Y",
9999: "Z",
0: " "
}
letters_to_nums = {
"A": (2, 1),
"B": (2, 2),
"C": (2, 3),
"D": (3, 1),
"E": (3, 2),
"F": (3, 3),
"G": (4, 1),
"H": (4, 2),
"I": (4, 3),
"J": (5, 1),
"K": (5, 2),
"L": (5, 3),
"M": (6, 1),
"N": (6, 2),
"O": (6, 3),
"P": (7, 1),
"Q": (7, 2),
"R": (7, 3),
"S": (7, 4),
"T": (8, 1),
"U": (8, 2),
"V": (8, 3),
"W": (9, 1),
"X": (9, 2),
"Y": (9, 3),
"Z": (9, 4),
" ": (0, 1)
}
def convert_occurances_of_digit_to_num(digit, occurances):
return int(str(digit) * occurances)
def convert_digits_to_letter(digit, occurances):
if digit > 9 or digit == 1 or digit < 0:
return ""
if occurances > 4 and digit in (7, 9):
occurances %= 4
elif occurances > 3 and digit not in (7, 9):
occurances %= 3
num = convert_occurances_of_digit_to_num(digit, occurances)
return nums_to_letter[num]
def normalize_angle(angle):
while angle < 0:
angle += 360
while angle > 359:
angle -= 360
return angle
def round_angle(angle):
upper_range_angle = int((angle + 30) / 30) * 30
lower_range_angle = upper_range_angle - 30
if angle == (upper_range_angle + lower_range_angle) / 2:
return int(angle / 30) * 30
return round(angle / 30) * 30
def nums_to_text(nums):
text = ""
last_digit = nums[0]
occurances = 0
for num in nums:
if num == last_digit:
occurances += 1
else:
letter = convert_digits_to_letter(last_digit, occurances)
text += letter
if num != -1:
last_digit = num
occurances = 1
else:
occurances = 0
text += convert_digits_to_letter(last_digit, occurances)
return text
def text_to_nums(text):
nums = []
previous_letter = ""
for letter in text:
letter_upper = letter.upper()
if (previous_letter == letter_upper):
nums.append(-1)
previous_letter = letter_upper
digit_and_occurances = letters_to_nums[letter_upper]
if digit_and_occurances is not None:
i = 0
while i < digit_and_occurances[1]:
nums.append(digit_and_occurances[0])
i += 1
return nums
def nums_to_angle(nums):
angles_sum = 0
for num in nums:
if num == 0:
angles_sum += 300
else:
angles_sum += num * 30
return normalize_angle(angles_sum)
def angles_to_nums(angles):
nums = []
for angle in angles:
angle_normalized = normalize_angle(angle)
nearest_angle = round_angle(angle_normalized)
if nearest_angle != 0 and nearest_angle != 360:
num = int(nearest_angle / 30)
nums.append(num)
return nums
def is_phone_tastic(word):
word_to_nums = text_to_nums(word)
word_angle = nums_to_angle(word_to_nums)
return word_angle % len(word) == 0

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

..F....F..E...........EEE.E...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

======================================================================
ERROR: test_empty_input (test.TestNumsToText)
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
IndexError: list index out of range

======================================================================
ERROR: test_ending_with_timeout (test.TestNumsToText)
Test with a sequence ending with a -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
ValueError: invalid literal for int() with base 10: ''

======================================================================
ERROR: test_multiple_timeouts (test.TestNumsToText)
Test with multiple '-1's next to each other.
----------------------------------------------------------------------
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
ValueError: invalid literal for int() with base 10: ''

======================================================================
ERROR: 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
ValueError: invalid literal for int() with base 10: ''

======================================================================
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: [11] != []

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

- [11]
+ []

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

First differing element 5:
11
1

First list contains 2 additional elements.
First extra element 9:
10

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

+ [5, 1, 2, 4, 9, 1, 8, 0, 9]

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

FAILED (failures=5, errors=5)

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

Кристиан обнови решението на 29.10.2022 14:40 (преди над 1 година)

+nums_to_letter = {
+ 2: "A",
+ 22: "B",
+ 222: "C",
+ 3: "D",
+ 33: "E",
+ 333: "F",
+ 4: "G",
+ 44: "H",
+ 444: "I",
+ 5: "J",
+ 55: "K",
+ 555: "L",
+ 6: "M",
+ 66: "N",
+ 666: "O",
+ 7: "P",
+ 77: "Q",
+ 777: "R",
+ 7777: "S",
+ 8: "T",
+ 88: "U",
+ 888: "V",
+ 9: "W",
+ 99: "X",
+ 999: "Y",
+ 9999: "Z",
+ 0: " "
+}
+
+letters_to_nums = {
+ "A": (2, 1),
+ "B": (2, 2),
+ "C": (2, 3),
+ "D": (3, 1),
+ "E": (3, 2),
+ "F": (3, 3),
+ "G": (4, 1),
+ "H": (4, 2),
+ "I": (4, 3),
+ "J": (5, 1),
+ "K": (5, 2),
+ "L": (5, 3),
+ "M": (6, 1),
+ "N": (6, 2),
+ "O": (6, 3),
+ "P": (7, 1),
+ "Q": (7, 2),
+ "R": (7, 3),
+ "S": (7, 4),
+ "T": (8, 1),
+ "U": (8, 2),
+ "V": (8, 3),
+ "W": (9, 1),
+ "X": (9, 2),
+ "Y": (9, 3),
+ "Z": (9, 4),
+ " ": (0, 1)
+}
+
+
+def convert_occurances_of_digit_to_num(digit, occurances):
+ i = 0
+ num = ""
+ while i < occurances:
+ num += str(digit)
+ i += 1
+ return int(num)
+
+
+def convert_digits_to_letter(digit, occurances):
+ if digit > 9 or digit == 1 or digit < 0:
+ return ""
+ if occurances > 4 and (digit == 7 or digit == 9):
+ occurances %= 4
+ elif occurances > 3 and (digit != 7 and digit != 9):
+ occurances %= 3
+ num = convert_occurances_of_digit_to_num(digit, occurances)
+ return nums_to_letter[num]
+
+
+def normalize_angle(angle):
+ if angle < 0:
+ while angle < 0:
+ angle += 360
+ else:
+ while angle > 359:
+ angle -= 360
+ return angle
+
+
+def nums_to_text(nums):
+ text = ""
+ last_digit = nums[0]
+ occurances = 0
+ for num in nums:
+ if num == last_digit:
+ occurances += 1
+ else:
+ letter = convert_digits_to_letter(last_digit, occurances)
+ text += letter
+ if num != -1:
+ last_digit = num
+ occurances = 1
+ else:
+ occurances = 0
+ text += convert_digits_to_letter(last_digit, occurances)
+ return text
+
+
+def text_to_nums(text):
+ nums = []
+ for letter in text:
+ letter_upper = letter.upper()
+ digit_and_occurances = letters_to_nums[letter_upper]
+ if digit_and_occurances is not None:
+ i = 0
+ while i < digit_and_occurances[1]:
+ nums.append(digit_and_occurances[0])
+ i += 1
+ return nums
+
+
+def nums_to_angle(nums):
+ angles_sum = 0
+ for num in nums:
+ if num == 0:
+ angles_sum += 300
+ else:
+ angles_sum += num * 30
+ return normalize_angle(angles_sum)
+
+
+def angles_to_nums(angles):
+ nums = []
+ for angle in angles:
+ angle_normalized = normalize_angle(angle)
+ if angle == 345:
+ nearest_angle = 330
+ else:
+ nearest_angle = 30 * round(angle_normalized / 30)
+ if nearest_angle != 0 and nearest_angle != 360:
+ num = int(nearest_angle / 30)
+ nums.append(num)
+ return nums
+
+
+def is_phone_tastic(word):
+ word_to_nums = text_to_nums(word)
+ word_angle = nums_to_angle(word_to_nums)
+ if word_angle % len(word) == 0:
+ return True
+ else:
+ return False

Кристиан обнови решението на 30.10.2022 12:36 (преди над 1 година)

nums_to_letter = {
2: "A",
22: "B",
222: "C",
3: "D",
33: "E",
333: "F",
4: "G",
44: "H",
444: "I",
5: "J",
55: "K",
555: "L",
6: "M",
66: "N",
666: "O",
7: "P",
77: "Q",
777: "R",
7777: "S",
8: "T",
88: "U",
888: "V",
9: "W",
99: "X",
999: "Y",
9999: "Z",
0: " "
}
letters_to_nums = {
"A": (2, 1),
"B": (2, 2),
"C": (2, 3),
"D": (3, 1),
"E": (3, 2),
"F": (3, 3),
"G": (4, 1),
"H": (4, 2),
"I": (4, 3),
"J": (5, 1),
"K": (5, 2),
"L": (5, 3),
"M": (6, 1),
"N": (6, 2),
"O": (6, 3),
"P": (7, 1),
"Q": (7, 2),
"R": (7, 3),
"S": (7, 4),
"T": (8, 1),
"U": (8, 2),
"V": (8, 3),
"W": (9, 1),
"X": (9, 2),
"Y": (9, 3),
"Z": (9, 4),
" ": (0, 1)
}
def convert_occurances_of_digit_to_num(digit, occurances):
- i = 0
- num = ""
- while i < occurances:
- num += str(digit)
- i += 1
- return int(num)
+ return int(str(digit) * occurances)
def convert_digits_to_letter(digit, occurances):
if digit > 9 or digit == 1 or digit < 0:
return ""
- if occurances > 4 and (digit == 7 or digit == 9):
+ if occurances > 4 and digit in (7, 9):
occurances %= 4
- elif occurances > 3 and (digit != 7 and digit != 9):
+ elif occurances > 3 and digit not in (7, 9):
occurances %= 3
num = convert_occurances_of_digit_to_num(digit, occurances)
return nums_to_letter[num]
def normalize_angle(angle):
- if angle < 0:
- while angle < 0:
- angle += 360
- else:
- while angle > 359:
- angle -= 360
+ while angle < 0:
+ angle += 360
+ while angle > 359:
+ angle -= 360
return angle
+def round_angle(angle):
+ upper_range_angle = int((angle + 30) / 30) * 30
+ lower_range_angle = upper_range_angle - 30
+ if angle == (upper_range_angle + lower_range_angle) / 2:
+ return int(angle / 30) * 30
+ return round(angle / 30) * 30
+
+
def nums_to_text(nums):
text = ""
last_digit = nums[0]
occurances = 0
for num in nums:
if num == last_digit:
occurances += 1
else:
letter = convert_digits_to_letter(last_digit, occurances)
text += letter
if num != -1:
last_digit = num
occurances = 1
else:
occurances = 0
text += convert_digits_to_letter(last_digit, occurances)
return text
def text_to_nums(text):
nums = []
for letter in text:
letter_upper = letter.upper()
digit_and_occurances = letters_to_nums[letter_upper]
if digit_and_occurances is not None:
i = 0
while i < digit_and_occurances[1]:
nums.append(digit_and_occurances[0])
i += 1
return nums
def nums_to_angle(nums):
angles_sum = 0
for num in nums:
if num == 0:
angles_sum += 300
else:
angles_sum += num * 30
return normalize_angle(angles_sum)
def angles_to_nums(angles):
nums = []
for angle in angles:
angle_normalized = normalize_angle(angle)
- if angle == 345:
- nearest_angle = 330
- else:
- nearest_angle = 30 * round(angle_normalized / 30)
+ nearest_angle = round_angle(angle_normalized)
if nearest_angle != 0 and nearest_angle != 360:
num = int(nearest_angle / 30)
nums.append(num)
return nums
def is_phone_tastic(word):
word_to_nums = text_to_nums(word)
word_angle = nums_to_angle(word_to_nums)
- if word_angle % len(word) == 0:
- return True
+ return word_angle % len(word) == 0
- else:
- return False

Кристиан обнови решението на 30.10.2022 12:46 (преди над 1 година)

nums_to_letter = {
2: "A",
22: "B",
222: "C",
3: "D",
33: "E",
333: "F",
4: "G",
44: "H",
444: "I",
5: "J",
55: "K",
555: "L",
6: "M",
66: "N",
666: "O",
7: "P",
77: "Q",
777: "R",
7777: "S",
8: "T",
88: "U",
888: "V",
9: "W",
99: "X",
999: "Y",
9999: "Z",
0: " "
}
letters_to_nums = {
"A": (2, 1),
"B": (2, 2),
"C": (2, 3),
"D": (3, 1),
"E": (3, 2),
"F": (3, 3),
"G": (4, 1),
"H": (4, 2),
"I": (4, 3),
"J": (5, 1),
"K": (5, 2),
"L": (5, 3),
"M": (6, 1),
"N": (6, 2),
"O": (6, 3),
"P": (7, 1),
"Q": (7, 2),
"R": (7, 3),
"S": (7, 4),
"T": (8, 1),
"U": (8, 2),
"V": (8, 3),
"W": (9, 1),
"X": (9, 2),
"Y": (9, 3),
"Z": (9, 4),
" ": (0, 1)
}
def convert_occurances_of_digit_to_num(digit, occurances):
return int(str(digit) * occurances)
def convert_digits_to_letter(digit, occurances):
if digit > 9 or digit == 1 or digit < 0:
return ""
if occurances > 4 and digit in (7, 9):
occurances %= 4
elif occurances > 3 and digit not in (7, 9):
occurances %= 3
num = convert_occurances_of_digit_to_num(digit, occurances)
return nums_to_letter[num]
def normalize_angle(angle):
while angle < 0:
angle += 360
while angle > 359:
angle -= 360
return angle
def round_angle(angle):
upper_range_angle = int((angle + 30) / 30) * 30
lower_range_angle = upper_range_angle - 30
if angle == (upper_range_angle + lower_range_angle) / 2:
return int(angle / 30) * 30
return round(angle / 30) * 30
def nums_to_text(nums):
text = ""
last_digit = nums[0]
occurances = 0
for num in nums:
if num == last_digit:
occurances += 1
else:
letter = convert_digits_to_letter(last_digit, occurances)
text += letter
if num != -1:
last_digit = num
occurances = 1
else:
occurances = 0
text += convert_digits_to_letter(last_digit, occurances)
return text
def text_to_nums(text):
nums = []
+ previous_letter = ""
for letter in text:
letter_upper = letter.upper()
+ if (previous_letter == letter_upper):
+ nums.append(-1)
+ previous_letter = letter_upper
digit_and_occurances = letters_to_nums[letter_upper]
if digit_and_occurances is not None:
i = 0
while i < digit_and_occurances[1]:
nums.append(digit_and_occurances[0])
i += 1
return nums
def nums_to_angle(nums):
angles_sum = 0
for num in nums:
if num == 0:
angles_sum += 300
else:
angles_sum += num * 30
return normalize_angle(angles_sum)
def angles_to_nums(angles):
nums = []
for angle in angles:
angle_normalized = normalize_angle(angle)
nearest_angle = round_angle(angle_normalized)
if nearest_angle != 0 and nearest_angle != 360:
num = int(nearest_angle / 30)
nums.append(num)
return nums
def is_phone_tastic(word):
word_to_nums = text_to_nums(word)
word_angle = nums_to_angle(word_to_nums)
- return word_angle % len(word) == 0
+ return word_angle % len(word) == 0