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

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

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

Резултати

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

Код

pad_cheatsheet = [" ", "", "ABC", "DEF",
"GHI", "JKL", "MNO",
"PQRS", "TUV", "WXYZ"]
dial_cheatsheet = [300, 30, 60, 90, 120,
150, 180, 210, 240, 270]
def nums_to_text(nums):
result = ''
cnt = 0
for i, num in enumerate(nums):
if num not in range(10):
continue
if num in (0, 1):
result += pad_cheatsheet[num] # ' ' or ''
# Assuming that by pressing 1 you do nothing but still 'timeout' previous input
cnt = 0
continue
if i < len(nums) - 1 and num == nums[i + 1]:
cnt += 1
else:
result += pad_cheatsheet[num][cnt % len(pad_cheatsheet[num])]
cnt = 0
return result
def text_to_nums(text):
result = []
for character in text:
if not (character.isalpha() or ' '):
continue
uppercase_character = character.upper()
for i, cs_char in enumerate(pad_cheatsheet):
if uppercase_character in cs_char:
num_to_append = i
# Assuming that multiple zeros are not separated by -1s
if result and result[-1] == num_to_append and num_to_append != 0:
result.append(-1)
result.extend([num_to_append] *
(cs_char.index(uppercase_character) + 1))
return result
def nums_to_angle(nums):
result = 0
for n in nums:
# Assuming that n can't be negative (realistically you can't dial the phone the other way :D)
if n in range(10):
result += dial_cheatsheet[n]
return result % 360
def angles_to_nums(angles):
result = []
deviation = max(dial_cheatsheet) / (2 * len(dial_cheatsheet))
for angle in angles:
angle %= 360
if not deviation < angle <= max(dial_cheatsheet) + deviation:
continue
for i, fixed_angle in enumerate(dial_cheatsheet):
if fixed_angle - deviation < angle <= fixed_angle + deviation:
result.append(i)
break
return result
def is_phone_tastic(word):
return nums_to_angle(text_to_nums(word)) % len(word) == 0 if word else False

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

.....................................
----------------------------------------------------------------------
Ran 37 tests in 0.344s

OK

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

Александър обнови решението на 28.10.2022 19:06 (преди над 1 година)

+pad_cheatsheet = [" ", "", "ABC", "DEF",
+ "GHI", "JKL", "MNO",
+ "PQRS", "TUV", "WXYZ"]
+dial_cheatsheet = [300, 30, 60, 90, 120,
+ 150, 180, 210, 240, 270]
+
+
+def nums_to_text(nums):
+
+ result = ''
+ cnt = 0
+
+ for i in range(len(nums)):
+ if nums[i] not in range(10):
+ continue
+
+ if nums[i] in range(2):
+ result += pad_cheatsheet[nums[i]] # ' ' or ''
+ # Assuming that by pressing 1 you do nothing but still 'timeout' previous input
+ cnt = 0
+ continue
+
+ if i < len(nums) - 1 and nums[i] == nums[i + 1]:
+ cnt += 1
+ else:
+ if nums[i] in (7, 9):
+ result += pad_cheatsheet[nums[i]][cnt % 4]
+ else:
+ result += pad_cheatsheet[nums[i]][cnt % 3]
+ cnt = 0
+
+ return result
+
+
+def text_to_nums(text):
+
+ result = []
+
+ for character in text:
+ if not (character.isalpha() or ' '):
+ continue
+
+ uppercase_character = character.upper()
+
+ for cs_char in pad_cheatsheet:
+ if uppercase_character in cs_char:
+ num_to_append = pad_cheatsheet.index(cs_char)
+
+ # Assuming that multiple zeros are not separated by -1s
+ if result and result[-1] == num_to_append and num_to_append != 0:
+ result.append(-1)
+
+ result.extend([num_to_append] *
+ (cs_char.index(uppercase_character) + 1))
+
+ return result
+
+
+def nums_to_angle(nums):
+
+ sum = 0
+
+ for n in nums:
+ # Assuming that n can't be negative (realistically you can't dial the phone the other way :D)
+ if n in range(10):
+ sum += dial_cheatsheet[n]
+
+ return sum % 360
+
+
+def angles_to_nums(angles):
+
+ result = []
+
+ for angle in angles:
+ angle = angle % 360
+
+ if angle <= 15 or angle > 315:
+ continue
+
+ for i in range(len(dial_cheatsheet)):
+ if angle in range(dial_cheatsheet[i] - 14, dial_cheatsheet[i] + 16):
+ result.append(i)
+ break
+
+ return result
+
+
+def is_phone_tastic(word):
+ return nums_to_angle(text_to_nums(word)) % len(word) == 0 if word else False

Александър обнови решението на 29.10.2022 19:02 (преди над 1 година)

pad_cheatsheet = [" ", "", "ABC", "DEF",
"GHI", "JKL", "MNO",
"PQRS", "TUV", "WXYZ"]
dial_cheatsheet = [300, 30, 60, 90, 120,
150, 180, 210, 240, 270]
def nums_to_text(nums):
-
result = ''
cnt = 0
for i in range(len(nums)):
if nums[i] not in range(10):
continue
if nums[i] in range(2):
result += pad_cheatsheet[nums[i]] # ' ' or ''
# Assuming that by pressing 1 you do nothing but still 'timeout' previous input
cnt = 0
continue
if i < len(nums) - 1 and nums[i] == nums[i + 1]:
cnt += 1
else:
if nums[i] in (7, 9):
result += pad_cheatsheet[nums[i]][cnt % 4]
else:
result += pad_cheatsheet[nums[i]][cnt % 3]
cnt = 0
return result
def text_to_nums(text):
-
result = []
for character in text:
if not (character.isalpha() or ' '):
continue
uppercase_character = character.upper()
for cs_char in pad_cheatsheet:
if uppercase_character in cs_char:
num_to_append = pad_cheatsheet.index(cs_char)
# Assuming that multiple zeros are not separated by -1s
if result and result[-1] == num_to_append and num_to_append != 0:
result.append(-1)
result.extend([num_to_append] *
(cs_char.index(uppercase_character) + 1))
return result
def nums_to_angle(nums):
-
sum = 0
for n in nums:
# Assuming that n can't be negative (realistically you can't dial the phone the other way :D)
if n in range(10):
sum += dial_cheatsheet[n]
return sum % 360
def angles_to_nums(angles):
-
result = []
for angle in angles:
- angle = angle % 360
+ angle %= 360
if angle <= 15 or angle > 315:
continue
for i in range(len(dial_cheatsheet)):
if angle in range(dial_cheatsheet[i] - 14, dial_cheatsheet[i] + 16):
result.append(i)
break
- return result
-
+ return result
-
-def is_phone_tastic(word):
- return nums_to_angle(text_to_nums(word)) % len(word) == 0 if word else False

Александър обнови решението на 29.10.2022 19:03 (преди над 1 година)

pad_cheatsheet = [" ", "", "ABC", "DEF",
"GHI", "JKL", "MNO",
"PQRS", "TUV", "WXYZ"]
dial_cheatsheet = [300, 30, 60, 90, 120,
150, 180, 210, 240, 270]
def nums_to_text(nums):
result = ''
cnt = 0
for i in range(len(nums)):
if nums[i] not in range(10):
continue
if nums[i] in range(2):
result += pad_cheatsheet[nums[i]] # ' ' or ''
# Assuming that by pressing 1 you do nothing but still 'timeout' previous input
cnt = 0
continue
if i < len(nums) - 1 and nums[i] == nums[i + 1]:
cnt += 1
else:
if nums[i] in (7, 9):
result += pad_cheatsheet[nums[i]][cnt % 4]
else:
result += pad_cheatsheet[nums[i]][cnt % 3]
cnt = 0
return result
def text_to_nums(text):
result = []
for character in text:
if not (character.isalpha() or ' '):
continue
uppercase_character = character.upper()
for cs_char in pad_cheatsheet:
if uppercase_character in cs_char:
num_to_append = pad_cheatsheet.index(cs_char)
# Assuming that multiple zeros are not separated by -1s
if result and result[-1] == num_to_append and num_to_append != 0:
result.append(-1)
result.extend([num_to_append] *
(cs_char.index(uppercase_character) + 1))
return result
def nums_to_angle(nums):
sum = 0
for n in nums:
# Assuming that n can't be negative (realistically you can't dial the phone the other way :D)
if n in range(10):
sum += dial_cheatsheet[n]
return sum % 360
def angles_to_nums(angles):
result = []
for angle in angles:
angle %= 360
if angle <= 15 or angle > 315:
continue
for i in range(len(dial_cheatsheet)):
if angle in range(dial_cheatsheet[i] - 14, dial_cheatsheet[i] + 16):

14 и 16 са литерали, които могат да се изгубят в кода. Ако в някакъв етап добавиш нова цифра, ъглите се променят, а тези числа ще звучат магически и няма да е много ясно защо са тук, ако въобще се сетиш да ги оправиш. Няма нужда да преправяш каквото и да било, но имай предвид за други проекти. Може би една глобална променлива, която да ги дефинира, или отделна функция, в която си написал какво точно правиш, или пък друг похват за прилагане на тази част от логиката...

result.append(i)
break
- return result
+ return result
+
+
+def is_phone_tastic(word):
+ return nums_to_angle(text_to_nums(word)) % len(word) == 0 if word else False

Александър обнови решението на 31.10.2022 12:14 (преди над 1 година)

pad_cheatsheet = [" ", "", "ABC", "DEF",
"GHI", "JKL", "MNO",
"PQRS", "TUV", "WXYZ"]
dial_cheatsheet = [300, 30, 60, 90, 120,
150, 180, 210, 240, 270]
def nums_to_text(nums):
result = ''
cnt = 0
- for i in range(len(nums)):
- if nums[i] not in range(10):
+ for i, num in enumerate(nums):
+ if num not in range(10):
continue
- if nums[i] in range(2):
- result += pad_cheatsheet[nums[i]] # ' ' or ''
+ if num in (0, 1):
+ result += pad_cheatsheet[num] # ' ' or ''
# Assuming that by pressing 1 you do nothing but still 'timeout' previous input
cnt = 0
continue
- if i < len(nums) - 1 and nums[i] == nums[i + 1]:
+ if i < len(nums) - 1 and num == nums[i + 1]:
cnt += 1
else:
- if nums[i] in (7, 9):
- result += pad_cheatsheet[nums[i]][cnt % 4]
- else:
- result += pad_cheatsheet[nums[i]][cnt % 3]
+ result += pad_cheatsheet[num][cnt % len(pad_cheatsheet[num])]
cnt = 0
return result
def text_to_nums(text):
result = []
for character in text:
if not (character.isalpha() or ' '):
continue
uppercase_character = character.upper()
- for cs_char in pad_cheatsheet:
+ for i, cs_char in enumerate(pad_cheatsheet):
if uppercase_character in cs_char:
- num_to_append = pad_cheatsheet.index(cs_char)
+ num_to_append = i
# Assuming that multiple zeros are not separated by -1s
if result and result[-1] == num_to_append and num_to_append != 0:
result.append(-1)
result.extend([num_to_append] *
(cs_char.index(uppercase_character) + 1))
return result
def nums_to_angle(nums):
- sum = 0
+ result = 0
for n in nums:
# Assuming that n can't be negative (realistically you can't dial the phone the other way :D)
if n in range(10):
- sum += dial_cheatsheet[n]
+ result += dial_cheatsheet[n]
- return sum % 360
+ return result % 360
def angles_to_nums(angles):
result = []
+ deviation = max(dial_cheatsheet) / (2 * len(dial_cheatsheet))
for angle in angles:
angle %= 360
- if angle <= 15 or angle > 315:
+ if not (deviation < angle <= max(dial_cheatsheet) + deviation):
continue
- for i in range(len(dial_cheatsheet)):
- if angle in range(dial_cheatsheet[i] - 14, dial_cheatsheet[i] + 16):
+ for i, fixed_angle in enumerate(dial_cheatsheet):
+ if fixed_angle - deviation < angle <= fixed_angle + deviation:
result.append(i)
break
return result
def is_phone_tastic(word):
return nums_to_angle(text_to_nums(word)) % len(word) == 0 if word else False

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

pad_cheatsheet = [" ", "", "ABC", "DEF",
"GHI", "JKL", "MNO",
"PQRS", "TUV", "WXYZ"]
dial_cheatsheet = [300, 30, 60, 90, 120,
150, 180, 210, 240, 270]
def nums_to_text(nums):
result = ''
cnt = 0
for i, num in enumerate(nums):
if num not in range(10):
continue
if num in (0, 1):
result += pad_cheatsheet[num] # ' ' or ''
# Assuming that by pressing 1 you do nothing but still 'timeout' previous input
cnt = 0
continue
if i < len(nums) - 1 and num == nums[i + 1]:
cnt += 1
else:
result += pad_cheatsheet[num][cnt % len(pad_cheatsheet[num])]
cnt = 0
return result
def text_to_nums(text):
result = []
for character in text:
if not (character.isalpha() or ' '):
continue
uppercase_character = character.upper()
for i, cs_char in enumerate(pad_cheatsheet):
if uppercase_character in cs_char:
num_to_append = i
# Assuming that multiple zeros are not separated by -1s
if result and result[-1] == num_to_append and num_to_append != 0:
result.append(-1)
result.extend([num_to_append] *
(cs_char.index(uppercase_character) + 1))
return result
def nums_to_angle(nums):
result = 0
for n in nums:
# Assuming that n can't be negative (realistically you can't dial the phone the other way :D)
if n in range(10):
result += dial_cheatsheet[n]
return result % 360
def angles_to_nums(angles):
result = []
deviation = max(dial_cheatsheet) / (2 * len(dial_cheatsheet))
for angle in angles:
angle %= 360
- if not (deviation < angle <= max(dial_cheatsheet) + deviation):
+ if not deviation < angle <= max(dial_cheatsheet) + deviation:
continue
for i, fixed_angle in enumerate(dial_cheatsheet):
if fixed_angle - deviation < angle <= fixed_angle + deviation:
result.append(i)
break
return result
def is_phone_tastic(word):
return nums_to_angle(text_to_nums(word)) % len(word) == 0 if word else False