Решение на Телефонна любов от Георги Събев

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

Към профила на Георги Събев

Резултати

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

Код

DIGIT_LETTER_MAPPINGS = {
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: [' ']
}
LETTER_DIGITS_MAPPINGS = {
'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],
' ': [0]
}
DIGIT_ANGLE_MAPPINGS = {
1: 30,
2: 60,
3: 90,
4: 120,
5: 150,
6: 180,
7: 210,
8: 240,
9: 270,
0: 300
}
ANGLE_DIGIT_MAPPINGS = {
angle: digit for digit, angle in DIGIT_ANGLE_MAPPINGS.items()
}
def count_consecutive_duplicates(arr):
grouped = []
for element in arr:
if grouped and element in grouped[-1]:
grouped[-1].append(element)
else:
grouped.append([element])
pairs = [(group[0], len(group)) for group in grouped]
return pairs
def normalize_angle(angle):
return angle % 360
def round_to_nearest_divisible_by_30(angle):
normalized = normalize_angle(angle)
smaller_divisible = normalized - (normalized % 30)
larger_divisible = normalized + (30 - (normalized % 30))
if normalized - smaller_divisible <= larger_divisible - normalized:
return smaller_divisible
return larger_divisible
def nums_to_text(nums):
text = ''
counted = count_consecutive_duplicates(nums)
for digit, count in counted:
letters = DIGIT_LETTER_MAPPINGS.get(digit, [])
if not letters:
continue
idx = (count - 1) % len(letters)
text += letters[idx]
return text
def text_to_nums(text):
nums = []
for symbol in text.upper():
digits = LETTER_DIGITS_MAPPINGS.get(symbol, [])
if nums and nums[-1] in digits:
nums.append(-1)
nums += digits
return nums
def nums_to_angle(nums):
angles = [DIGIT_ANGLE_MAPPINGS.get(num, 0) for num in nums]
angle_sum = sum(angles)
return normalize_angle(angle_sum)
def angles_to_nums(angles):
rounded = [round_to_nearest_divisible_by_30(angle) for angle in angles]
digits = [ANGLE_DIGIT_MAPPINGS[angle] for angle in rounded
if angle not in [0, 330, 360]]
return digits
def is_phone_tastic(word):
if len(word) == 0:
return False
digits = text_to_nums(word)
sum_of_angles = nums_to_angle(digits)
if sum_of_angles % len(word) == 0:
return True
return False

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

.....................................
----------------------------------------------------------------------
Ran 37 tests in 0.315s

OK

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

Георги обнови решението на 02.11.2022 23:00 (преди над 1 година)

+DIGIT_LETTER_MAPPINGS = {
+ 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: [' ']
+}
+
+LETTER_DIGITS_MAPPINGS = {
+ '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],
+ ' ': [0]
+}
+
+DIGIT_ANGLE_MAPPINGS = {
+ 1: 30,
+ 2: 60,
+ 3: 90,
+ 4: 120,
+ 5: 150,
+ 6: 180,
+ 7: 210,
+ 8: 240,
+ 9: 270,
+ 0: 300
+}
+
+ANGLE_DIGIT_MAPPINGS = {
+ angle: digit for digit, angle in DIGIT_ANGLE_MAPPINGS.items()
+}
+
+
+def count_consecutive_duplicates(arr):
+ grouped = []
+
+ for element in arr:
+ if grouped and element in grouped[-1]:
+ grouped[-1].append(element)
+ else:
+ grouped.append([element])
+
+ pairs = [(group[0], len(group)) for group in grouped]
+
+ return pairs
+
+
+def normalize_angle(angle):
+ return angle % 360
+
+
+def round_to_nearest_divisible_by_30(angle):
+ normalized = normalize_angle(angle)
+
+ smaller_divisible = normalized - (normalized % 30)
+ larger_divisible = normalized + (30 - (normalized % 30))
+
+ if normalized - smaller_divisible <= larger_divisible - normalized:
+ return smaller_divisible
+
+ return larger_divisible
+
+
+def nums_to_text(nums):
+ text = ''
+ counted = count_consecutive_duplicates(nums)
+
+ for digit, count in counted:
+ letters = DIGIT_LETTER_MAPPINGS.get(digit, [])
+
+ if not letters:
+ continue
+
+ idx = (count - 1) % len(letters)
+ text += letters[idx]
+
+ return text
+
+
+def text_to_nums(text):
+ nums = []
+
+ for symbol in text.upper():
+ digits = LETTER_DIGITS_MAPPINGS.get(symbol, [])
+
+ if nums and nums[-1] in digits:
+ nums.append(-1)
+
+ nums += digits
+
+ return nums
+
+
+def nums_to_angle(nums):
+ angles = [DIGIT_ANGLE_MAPPINGS.get(num, 0) for num in nums]
+ angle_sum = sum(angles)
+
+ return normalize_angle(angle_sum)
+
+
+def angles_to_nums(angles):
+ rounded = [round_to_nearest_divisible_by_30(angle) for angle in angles]
+
+ digits = [ANGLE_DIGIT_MAPPINGS[angle] for angle in rounded
+ if angle not in [0, 330, 360]]
+
+ return digits
+
+
+def is_phone_tastic(word):
+ digits = text_to_nums(word)
+ sum_of_angles = nums_to_angle(digits)
+
+ if sum_of_angles % len(word) == 0:
+ return True
+
+ return False

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

DIGIT_LETTER_MAPPINGS = {
+ +
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: [' ']
}
LETTER_DIGITS_MAPPINGS = {
'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],
' ': [0]
}
DIGIT_ANGLE_MAPPINGS = {
1: 30,
2: 60,
3: 90,
4: 120,
5: 150,
6: 180,
7: 210,
8: 240,
9: 270,
0: 300
}
ANGLE_DIGIT_MAPPINGS = {
angle: digit for digit, angle in DIGIT_ANGLE_MAPPINGS.items()
}
def count_consecutive_duplicates(arr):
grouped = []
for element in arr:
if grouped and element in grouped[-1]:
grouped[-1].append(element)
else:
grouped.append([element])
pairs = [(group[0], len(group)) for group in grouped]
return pairs
def normalize_angle(angle):
return angle % 360
def round_to_nearest_divisible_by_30(angle):
normalized = normalize_angle(angle)
smaller_divisible = normalized - (normalized % 30)
larger_divisible = normalized + (30 - (normalized % 30))
if normalized - smaller_divisible <= larger_divisible - normalized:
return smaller_divisible
return larger_divisible
def nums_to_text(nums):
text = ''
counted = count_consecutive_duplicates(nums)
for digit, count in counted:
letters = DIGIT_LETTER_MAPPINGS.get(digit, [])
if not letters:
continue
idx = (count - 1) % len(letters)
text += letters[idx]
return text
def text_to_nums(text):
nums = []
for symbol in text.upper():
digits = LETTER_DIGITS_MAPPINGS.get(symbol, [])
if nums and nums[-1] in digits:
nums.append(-1)
nums += digits
return nums
def nums_to_angle(nums):
angles = [DIGIT_ANGLE_MAPPINGS.get(num, 0) for num in nums]
angle_sum = sum(angles)
return normalize_angle(angle_sum)
def angles_to_nums(angles):
rounded = [round_to_nearest_divisible_by_30(angle) for angle in angles]
digits = [ANGLE_DIGIT_MAPPINGS[angle] for angle in rounded
if angle not in [0, 330, 360]]
return digits
def is_phone_tastic(word):
+ if len(word) == 0:
+ return False
+
digits = text_to_nums(word)
sum_of_angles = nums_to_angle(digits)
if sum_of_angles % len(word) == 0:
return True
return False

Георги обнови решението на 03.11.2022 00:12 (преди над 1 година)

DIGIT_LETTER_MAPPINGS = {
- +
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: [' ']
}
LETTER_DIGITS_MAPPINGS = {
'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],
' ': [0]
}
DIGIT_ANGLE_MAPPINGS = {
1: 30,
2: 60,
3: 90,
4: 120,
5: 150,
6: 180,
7: 210,
8: 240,
9: 270,
0: 300
}
ANGLE_DIGIT_MAPPINGS = {
angle: digit for digit, angle in DIGIT_ANGLE_MAPPINGS.items()
}
def count_consecutive_duplicates(arr):
grouped = []
for element in arr:
if grouped and element in grouped[-1]:
grouped[-1].append(element)
else:
grouped.append([element])
pairs = [(group[0], len(group)) for group in grouped]
return pairs
def normalize_angle(angle):
return angle % 360
def round_to_nearest_divisible_by_30(angle):
normalized = normalize_angle(angle)
smaller_divisible = normalized - (normalized % 30)
larger_divisible = normalized + (30 - (normalized % 30))
if normalized - smaller_divisible <= larger_divisible - normalized:
return smaller_divisible
return larger_divisible
def nums_to_text(nums):
text = ''
counted = count_consecutive_duplicates(nums)
for digit, count in counted:
letters = DIGIT_LETTER_MAPPINGS.get(digit, [])
if not letters:
continue
idx = (count - 1) % len(letters)
text += letters[idx]
return text
def text_to_nums(text):
nums = []
for symbol in text.upper():
digits = LETTER_DIGITS_MAPPINGS.get(symbol, [])
if nums and nums[-1] in digits:
nums.append(-1)
nums += digits
return nums
def nums_to_angle(nums):
angles = [DIGIT_ANGLE_MAPPINGS.get(num, 0) for num in nums]
angle_sum = sum(angles)
return normalize_angle(angle_sum)
def angles_to_nums(angles):
rounded = [round_to_nearest_divisible_by_30(angle) for angle in angles]
digits = [ANGLE_DIGIT_MAPPINGS[angle] for angle in rounded
if angle not in [0, 330, 360]]
return digits
def is_phone_tastic(word):
if len(word) == 0:
return False
digits = text_to_nums(word)
sum_of_angles = nums_to_angle(digits)
if sum_of_angles % len(word) == 0:
return True
return False

Слагам и тестовете, с които тествах (тези, които са от други колеги във форума, съм отбелязал с "Test from forum N"): https://pastebin.com/mi85yePt

На тестовете за 0/' ' съм сложил assertTrue(... in [v1, v2]), тъй като, доколкото разбрах от форума, считаме, че и двата варианта за 0 са валидни.