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

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

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

Резултати

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

Код

phone_buttons = {
-1: [""], # phone_buttons[-1][0] = ""
0: " ",
1: [""], # phone_buttons[1][0] = ""
2: "ABC",
3: "DEF",
4: "GHI",
5: "JKL",
6: "MNO",
7: "PQRS",
8: "TUV",
9: "WXYZ",
}
phone_dial = {
1: 30,
2: 60,
3: 90,
4: 120,
5: 150,
6: 180,
7: 210,
8: 240,
9: 270,
0: 300,
}
def button_iterated(clicked_button, iterations_count):
return len(phone_buttons[clicked_button]) == iterations_count + 1
def nums_to_text(nums: list):
phone_message = ""
length = len(nums)
current_clicked_button_idx = 0
next_clicked_button_idx = 0
while current_clicked_button_idx < length:
if nums[current_clicked_button_idx] == 0:
phone_message += phone_buttons[nums[current_clicked_button_idx]][0]
current_clicked_button_idx += 1
continue
button_clicked_count = 0
if (nums[current_clicked_button_idx] == -1 or nums[current_clicked_button_idx] == 1) and\
current_clicked_button_idx + 1 < length:
current_clicked_button_idx += 1
continue
if current_clicked_button_idx + 1 < length:
next_clicked_button_idx = current_clicked_button_idx + 1
while nums[current_clicked_button_idx] == nums[next_clicked_button_idx]:
if button_iterated(nums[current_clicked_button_idx], button_clicked_count):
button_clicked_count = 0
else:
button_clicked_count += 1
if next_clicked_button_idx + 1 < length:
next_clicked_button_idx += 1
else:
current_clicked_button_idx
break
phone_message += phone_buttons[nums[current_clicked_button_idx]][button_clicked_count]
if next_clicked_button_idx == length - 1 and nums[current_clicked_button_idx] == nums[
next_clicked_button_idx]:
current_clicked_button_idx = next_clicked_button_idx + 1
else:
current_clicked_button_idx = next_clicked_button_idx
else:
phone_message += phone_buttons[nums[current_clicked_button_idx]][button_clicked_count]
current_clicked_button_idx += 1
return phone_message
def text_to_nums(text: str):
text_letters = [letter for letter in text.upper()]
nums = []
previous_key = None
for letter in text_letters:
for key, value in phone_buttons.items():
if letter in value:
clicked_times = value.find(letter) + 1
if previous_key == key:
nums.append(-1)
for _ in range(0, clicked_times):
nums.append(key)
previous_key = key
return nums
def normalize_angle(angle):
if 0 <= angle < 360:
return angle
elif angle == 360:
return 0
else:
angle %= 360
return angle
def round_angle(angle):

Предполагам, че можеш да постигнеш същия резултат, като използваш остатък при деление на 360. Става въпрос както за тази, така и запредишната функция.

if angle < 30:
if angle <= 15:
return 0
else:
return 30
if angle % 30 == 0:
return angle
elif angle % 30 <= 15:
return angle - (angle % 15)
elif angle % 30 > 15:
return angle + (30 - (angle % 30))
def nums_to_angle(nums: list):
angle = 0
for num in nums:
if 0 <= num <= 9:
angle += phone_dial[num]
return normalize_angle(angle)
def angles_to_nums(angles: list):
nums = []
for angle in angles:
angle = normalize_angle(angle)
angle = round_angle(angle)
if angle != 0 and angle != 330:
nums.append(list({num for num in phone_dial if phone_dial[num] == angle}).__getitem__(0))
return nums
def is_phone_tastic(word: str):
word_length = len(word)
if word_length == 0:
return False
nums = text_to_nums(word)
angle = nums_to_angle(nums)
return angle % word_length == 0

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

.......EE............................
======================================================================
ERROR: 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
IndexError: list index out of range

======================================================================
ERROR: test_round_angle_direction (test.TestAnglesToNums)
Test with an angle requiring explicit rounding to floor.
----------------------------------------------------------------------
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

----------------------------------------------------------------------
Ran 37 tests in 0.369s

FAILED (errors=2)

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

Алекс обнови решението на 30.10.2022 15:08 (преди над 1 година)

+phone_buttons = {
+ -1: "",
+ 0: " ",
+ 1: "",
+ 2: "ABC",
+ 3: "DEF",
+ 4: "GHI",
+ 5: "JKL",
+ 6: "MNO",
+ 7: "PQRS",
+ 8: "TUV",
+ 9: "WXYZ",
+}
+
+phone_dial = {
+ 1: 30,
+ 2: 60,
+ 3: 90,
+ 4: 120,
+ 5: 150,
+ 6: 180,
+ 7: 210,
+ 8: 240,
+ 9: 270,
+ 0: 300,
+}
+
+
+def button_iterated(clicked_button, iterations_count):
+ if len(phone_buttons[clicked_button]) == iterations_count + 1:
+ return True
+ else:
+ return False
+
+
+def nums_to_text(nums: list):
+ phone_message = ""
+ current_button_idx = 0
+ next_button_idx = 0
+
+ if len(nums) == 1:
+ return phone_buttons[nums[current_button_idx]][0]
+ if len(nums) == 0:
+ return ""
+
+ while current_button_idx < len(nums) - 1:
+ button_clicked_count = 0
+ if current_button_idx < len(nums) - 1:
+ next_button_idx = current_button_idx + 1
+
+ while nums[current_button_idx] == nums[next_button_idx]:
+ if button_iterated(nums[current_button_idx], button_clicked_count):
+ button_clicked_count = 0
+ else:
+ button_clicked_count += 1
+
+ if next_button_idx + 1 < len(nums):
+ next_button_idx += 1
+ else:
+ break
+
+ if nums[current_button_idx] == -1:
+ current_button_idx = next_button_idx
+ continue
+ phone_message += phone_buttons[nums[current_button_idx]][button_clicked_count]
+ current_button_idx = next_button_idx
+
+ return phone_message
+
+
+def text_to_nums(text: str):
+ text_letters = [letter for letter in text.upper()]
+ nums = []
+ previous_key = None
+
+ for letter in text_letters:
+ for key, value in phone_buttons.items():
+ if letter in value:
+ clicked_times = value.find(letter) + 1
+
+ if previous_key == key:
+ nums.append(-1)
+
+ for _ in range(0, clicked_times):
+ nums.append(key)
+
+ previous_key = key
+ return nums
+
+
+def normalize_angle(angle):
+ if 0 <= angle < 360:
+ return angle
+ elif angle == 360:
+ return 0
+ elif angle < 0:
+ while angle < 0:
+ angle += 360
+
+ while angle > 360:
+ angle -= 360
+
+ return angle
+
+
+def round_angle(angle):
+ if angle < 30:
+ if angle <= 15:
+ return 0
+ else:
+ return 30
+
+ if angle % 30 == 0:
+ return angle
+ elif angle % 30 <= 15:
+ return angle - (angle % 15)
+ elif angle % 30 > 15:
+ return angle + (30 - (angle % 30))
+
+
+def nums_to_angle(nums: list):
+ angle = 0
+
+ for num in nums:
+ angle += phone_dial[num]
+
+ return normalize_angle(angle)
+
+
+def angles_to_nums(angles: list):
+ nums = []
+ for angle in angles:
+ angle = normalize_angle(angle)
+ angle = round_angle(angle)
+ if angle != 0 and angle != 330:
+ for num, ang in phone_dial.items():
+ if ang == angle:
+ nums.append(num)
+
+ return nums
+
+
+def is_phone_tastic(word: str):
+ word_length = len(word)
+ nums = text_to_nums(word)
+ angle = nums_to_angle(nums)
+
+ return angle % word_length == 0

Алекс обнови решението на 30.10.2022 16:21 (преди над 1 година)

phone_buttons = {
-1: "",
0: " ",
- 1: "",
+ 1: [""],
2: "ABC",
3: "DEF",
4: "GHI",
5: "JKL",
6: "MNO",
7: "PQRS",
8: "TUV",
9: "WXYZ",
}
phone_dial = {
1: 30,
2: 60,
3: 90,
4: 120,
5: 150,
6: 180,
7: 210,
8: 240,
9: 270,
0: 300,
}
def button_iterated(clicked_button, iterations_count):
if len(phone_buttons[clicked_button]) == iterations_count + 1:

Можеш просто: return len(phone_buttons[clicked_button]) == iterations_count + 1 което поставя под въпрос нуждата от тази функция. Да, по този начин кодът, който я използва е по-лесен за възприемане, но пък е израз от само един ред. Въпрос на вкус :)

return True
else:
return False
def nums_to_text(nums: list):
phone_message = ""
current_button_idx = 0
next_button_idx = 0
+ intervals = 0
if len(nums) == 1:
return phone_buttons[nums[current_button_idx]][0]
if len(nums) == 0:
return ""
- while current_button_idx < len(nums) - 1:
+ while current_button_idx < len(nums):
button_clicked_count = 0
- if current_button_idx < len(nums) - 1:
- next_button_idx = current_button_idx + 1
+ next_button_idx = current_button_idx + 1
- while nums[current_button_idx] == nums[next_button_idx]:
- if button_iterated(nums[current_button_idx], button_clicked_count):
- button_clicked_count = 0
- else:
- button_clicked_count += 1
+ if next_button_idx < len(nums):
+ in_while = False
+ while nums[current_button_idx] == nums[next_button_idx]:
+ in_while = True
+ if button_iterated(nums[current_button_idx], button_clicked_count):
+ button_clicked_count = 0
+ else:
+ button_clicked_count += 1
- if next_button_idx + 1 < len(nums):
- next_button_idx += 1
- else:
- break
+ if nums[current_button_idx] == 0:
+ intervals += 1
- if nums[current_button_idx] == -1:
+ if next_button_idx + 1 < len(nums):
+ next_button_idx += 1
+ else:
+ break
+
+ if nums[current_button_idx] == -1:
+ current_button_idx = next_button_idx
+ continue
+ phone_message += phone_buttons[nums[current_button_idx]][button_clicked_count]
+ if nums[current_button_idx] == 0:
+ for _ in range(intervals):
+ phone_message += " "
+ intervals = 0
current_button_idx = next_button_idx
- continue
- phone_message += phone_buttons[nums[current_button_idx]][button_clicked_count]
- current_button_idx = next_button_idx
+ else:
+ if in_while:
+ break
+ phone_message += phone_buttons[nums[current_button_idx]][button_clicked_count]
+ current_button_idx += 1
+
return phone_message
def text_to_nums(text: str):
text_letters = [letter for letter in text.upper()]
nums = []
previous_key = None
for letter in text_letters:
for key, value in phone_buttons.items():
if letter in value:
clicked_times = value.find(letter) + 1
if previous_key == key:
nums.append(-1)
for _ in range(0, clicked_times):
nums.append(key)
previous_key = key
return nums
def normalize_angle(angle):
if 0 <= angle < 360:
return angle
elif angle == 360:
return 0
elif angle < 0:
while angle < 0:
angle += 360
while angle > 360:
angle -= 360
return angle
def round_angle(angle):

Предполагам, че можеш да постигнеш същия резултат, като използваш остатък при деление на 360. Става въпрос както за тази, така и запредишната функция.

if angle < 30:
if angle <= 15:
return 0
else:
return 30
if angle % 30 == 0:
return angle
elif angle % 30 <= 15:
return angle - (angle % 15)
elif angle % 30 > 15:
return angle + (30 - (angle % 30))
def nums_to_angle(nums: list):
angle = 0
for num in nums:
- angle += phone_dial[num]
+ if 0 <= num <= 9:
+ angle += phone_dial[num]
return normalize_angle(angle)
def angles_to_nums(angles: list):
nums = []
for angle in angles:
angle = normalize_angle(angle)
angle = round_angle(angle)
if angle != 0 and angle != 330:
for num, ang in phone_dial.items():
if ang == angle:
nums.append(num)
return nums
def is_phone_tastic(word: str):
word_length = len(word)
nums = text_to_nums(word)
angle = nums_to_angle(nums)
return angle % word_length == 0

Съветвам те да се опиташ да рефакторираш nums_to_text. Няма да лъжа - трудно ми е да проследя логиката. Оставих коментари по отделните парчета код от функцията, които видях на пръв поглед, но ми се струва прекалено усложнено.

С останалото си се справил добре.

Алекс обнови решението на 02.11.2022 22:33 (преди над 1 година)

phone_buttons = {
-1: "",
0: " ",
1: [""],
2: "ABC",
3: "DEF",
4: "GHI",
5: "JKL",
6: "MNO",
7: "PQRS",
8: "TUV",
9: "WXYZ",
}
phone_dial = {
1: 30,
2: 60,
3: 90,
4: 120,
5: 150,
6: 180,
7: 210,
8: 240,
9: 270,
0: 300,
}
def button_iterated(clicked_button, iterations_count):
- if len(phone_buttons[clicked_button]) == iterations_count + 1:
- return True
- else:
- return False
+ return len(phone_buttons[clicked_button]) == iterations_count + 1
def nums_to_text(nums: list):
phone_message = ""
current_button_idx = 0
next_button_idx = 0
intervals = 0
if len(nums) == 1:
return phone_buttons[nums[current_button_idx]][0]
if len(nums) == 0:
return ""
while current_button_idx < len(nums):
button_clicked_count = 0
next_button_idx = current_button_idx + 1
if next_button_idx < len(nums):
in_while = False
while nums[current_button_idx] == nums[next_button_idx]:
in_while = True
if button_iterated(nums[current_button_idx], button_clicked_count):
button_clicked_count = 0
else:
button_clicked_count += 1
if nums[current_button_idx] == 0:
intervals += 1
if next_button_idx + 1 < len(nums):
next_button_idx += 1
else:
break
if nums[current_button_idx] == -1:
current_button_idx = next_button_idx
continue
phone_message += phone_buttons[nums[current_button_idx]][button_clicked_count]
if nums[current_button_idx] == 0:
for _ in range(intervals):
phone_message += " "
intervals = 0
current_button_idx = next_button_idx
else:
if in_while:
break
phone_message += phone_buttons[nums[current_button_idx]][button_clicked_count]
current_button_idx += 1
return phone_message
def text_to_nums(text: str):
text_letters = [letter for letter in text.upper()]
nums = []
previous_key = None
for letter in text_letters:
for key, value in phone_buttons.items():
if letter in value:
clicked_times = value.find(letter) + 1
if previous_key == key:
nums.append(-1)
for _ in range(0, clicked_times):
nums.append(key)
previous_key = key
return nums
def normalize_angle(angle):
if 0 <= angle < 360:
return angle
elif angle == 360:
return 0
- elif angle < 0:
- while angle < 0:
- angle += 360
+ else:
+ angle %= 360
- while angle > 360:
- angle -= 360
-
return angle
def round_angle(angle):
if angle < 30:
if angle <= 15:
return 0
else:
return 30
if angle % 30 == 0:
return angle
elif angle % 30 <= 15:
return angle - (angle % 15)
elif angle % 30 > 15:
return angle + (30 - (angle % 30))
def nums_to_angle(nums: list):
angle = 0
for num in nums:
if 0 <= num <= 9:
angle += phone_dial[num]
return normalize_angle(angle)
def angles_to_nums(angles: list):
nums = []
for angle in angles:
angle = normalize_angle(angle)
angle = round_angle(angle)
if angle != 0 and angle != 330:
- for num, ang in phone_dial.items():
- if ang == angle:
- nums.append(num)
+ nums.append(list({num for num in phone_dial if phone_dial[num] == angle}).__getitem__(0))
return nums
def is_phone_tastic(word: str):
word_length = len(word)
nums = text_to_nums(word)
angle = nums_to_angle(nums)
return angle % word_length == 0
+

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

phone_buttons = {
- -1: "",
+ -1: [""], # phone_buttons[-1][0] = ""
0: " ",
- 1: [""],
+ 1: [""], # phone_buttons[-1][0] = ""
2: "ABC",
3: "DEF",
4: "GHI",
5: "JKL",
6: "MNO",
7: "PQRS",
8: "TUV",
9: "WXYZ",
}
phone_dial = {
1: 30,
2: 60,
3: 90,
4: 120,
5: 150,
6: 180,
7: 210,
8: 240,
9: 270,
0: 300,
}
def button_iterated(clicked_button, iterations_count):
return len(phone_buttons[clicked_button]) == iterations_count + 1
def nums_to_text(nums: list):
phone_message = ""
- current_button_idx = 0
- next_button_idx = 0
- intervals = 0
+ length = len(nums)
+ current_clicked_button_idx = 0
+ next_clicked_button_idx = 0
- if len(nums) == 1:
- return phone_buttons[nums[current_button_idx]][0]
- if len(nums) == 0:
- return ""
+ while current_clicked_button_idx < length:
+ if nums[current_clicked_button_idx] == 0:
+ phone_message += phone_buttons[nums[current_clicked_button_idx]][0]
+ current_clicked_button_idx += 1
+ continue
- while current_button_idx < len(nums):
button_clicked_count = 0
- next_button_idx = current_button_idx + 1
- if next_button_idx < len(nums):
- in_while = False
- while nums[current_button_idx] == nums[next_button_idx]:
- in_while = True
- if button_iterated(nums[current_button_idx], button_clicked_count):
+ if (nums[current_clicked_button_idx] == -1 or nums[current_clicked_button_idx] == 1) and\
+ current_clicked_button_idx + 1 < length:
+ current_clicked_button_idx += 1
+ continue
+
+ if current_clicked_button_idx + 1 < length:
+ next_clicked_button_idx = current_clicked_button_idx + 1
+
+ while nums[current_clicked_button_idx] == nums[next_clicked_button_idx]:
+ if button_iterated(nums[current_clicked_button_idx], button_clicked_count):
button_clicked_count = 0
else:
button_clicked_count += 1
- if nums[current_button_idx] == 0:
- intervals += 1
-
- if next_button_idx + 1 < len(nums):
- next_button_idx += 1
+ if next_clicked_button_idx + 1 < length:
+ next_clicked_button_idx += 1
else:
+ current_clicked_button_idx
break
- if nums[current_button_idx] == -1:
- current_button_idx = next_button_idx
- continue
- phone_message += phone_buttons[nums[current_button_idx]][button_clicked_count]
- if nums[current_button_idx] == 0:
- for _ in range(intervals):
- phone_message += " "
- intervals = 0
- current_button_idx = next_button_idx
+ phone_message += phone_buttons[nums[current_clicked_button_idx]][button_clicked_count]
+ if next_clicked_button_idx == length - 1 and nums[current_clicked_button_idx] == nums[
+ next_clicked_button_idx]:
+ current_clicked_button_idx = next_clicked_button_idx + 1
+ else:
+ current_clicked_button_idx = next_clicked_button_idx
else:
- if in_while:
- break
+ phone_message += phone_buttons[nums[current_clicked_button_idx]][button_clicked_count]
+ current_clicked_button_idx += 1
- phone_message += phone_buttons[nums[current_button_idx]][button_clicked_count]
- current_button_idx += 1
-
return phone_message
def text_to_nums(text: str):
text_letters = [letter for letter in text.upper()]
nums = []
previous_key = None
for letter in text_letters:
for key, value in phone_buttons.items():
if letter in value:
clicked_times = value.find(letter) + 1
if previous_key == key:
nums.append(-1)
for _ in range(0, clicked_times):
nums.append(key)
previous_key = key
return nums
def normalize_angle(angle):
if 0 <= angle < 360:
return angle
elif angle == 360:
return 0
else:
angle %= 360
return angle
def round_angle(angle):
if angle < 30:
if angle <= 15:
return 0
else:
return 30
if angle % 30 == 0:
return angle
elif angle % 30 <= 15:
return angle - (angle % 15)
elif angle % 30 > 15:
return angle + (30 - (angle % 30))
def nums_to_angle(nums: list):
angle = 0
for num in nums:
if 0 <= num <= 9:
angle += phone_dial[num]
return normalize_angle(angle)
def angles_to_nums(angles: list):
nums = []
for angle in angles:
angle = normalize_angle(angle)
angle = round_angle(angle)
if angle != 0 and angle != 330:
nums.append(list({num for num in phone_dial if phone_dial[num] == angle}).__getitem__(0))
return nums
def is_phone_tastic(word: str):
word_length = len(word)
+ if word_length == 0:
+ return False
+
nums = text_to_nums(word)
angle = nums_to_angle(nums)
- return angle % word_length == 0
-
+ return angle % word_length == 0

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

phone_buttons = {
-1: [""], # phone_buttons[-1][0] = ""
0: " ",
- 1: [""], # phone_buttons[-1][0] = ""
+ 1: [""], # phone_buttons[1][0] = ""
2: "ABC",
3: "DEF",
4: "GHI",
5: "JKL",
6: "MNO",
7: "PQRS",
8: "TUV",
9: "WXYZ",
}
phone_dial = {
1: 30,
2: 60,
3: 90,
4: 120,
5: 150,
6: 180,
7: 210,
8: 240,
9: 270,
0: 300,
}
def button_iterated(clicked_button, iterations_count):
return len(phone_buttons[clicked_button]) == iterations_count + 1
def nums_to_text(nums: list):
phone_message = ""
length = len(nums)
current_clicked_button_idx = 0
next_clicked_button_idx = 0
while current_clicked_button_idx < length:
if nums[current_clicked_button_idx] == 0:
phone_message += phone_buttons[nums[current_clicked_button_idx]][0]
current_clicked_button_idx += 1
continue
button_clicked_count = 0
if (nums[current_clicked_button_idx] == -1 or nums[current_clicked_button_idx] == 1) and\
current_clicked_button_idx + 1 < length:
current_clicked_button_idx += 1
continue
if current_clicked_button_idx + 1 < length:
next_clicked_button_idx = current_clicked_button_idx + 1
while nums[current_clicked_button_idx] == nums[next_clicked_button_idx]:
if button_iterated(nums[current_clicked_button_idx], button_clicked_count):
button_clicked_count = 0
else:
button_clicked_count += 1
if next_clicked_button_idx + 1 < length:
next_clicked_button_idx += 1
else:
current_clicked_button_idx
break
phone_message += phone_buttons[nums[current_clicked_button_idx]][button_clicked_count]
if next_clicked_button_idx == length - 1 and nums[current_clicked_button_idx] == nums[
next_clicked_button_idx]:
current_clicked_button_idx = next_clicked_button_idx + 1
else:
current_clicked_button_idx = next_clicked_button_idx
else:
phone_message += phone_buttons[nums[current_clicked_button_idx]][button_clicked_count]
current_clicked_button_idx += 1
return phone_message
def text_to_nums(text: str):
text_letters = [letter for letter in text.upper()]
nums = []
previous_key = None
for letter in text_letters:
for key, value in phone_buttons.items():
if letter in value:
clicked_times = value.find(letter) + 1
if previous_key == key:
nums.append(-1)
for _ in range(0, clicked_times):
nums.append(key)
previous_key = key
return nums
def normalize_angle(angle):
if 0 <= angle < 360:
return angle
elif angle == 360:
return 0
else:
angle %= 360
return angle
def round_angle(angle):
if angle < 30:
if angle <= 15:
return 0
else:
return 30
if angle % 30 == 0:
return angle
elif angle % 30 <= 15:
return angle - (angle % 15)
elif angle % 30 > 15:
return angle + (30 - (angle % 30))
def nums_to_angle(nums: list):
angle = 0
for num in nums:
if 0 <= num <= 9:
angle += phone_dial[num]
return normalize_angle(angle)
def angles_to_nums(angles: list):
nums = []
for angle in angles:
angle = normalize_angle(angle)
angle = round_angle(angle)
if angle != 0 and angle != 330:
nums.append(list({num for num in phone_dial if phone_dial[num] == angle}).__getitem__(0))
return nums
def is_phone_tastic(word: str):
word_length = len(word)
if word_length == 0:
return False
nums = text_to_nums(word)
angle = nums_to_angle(nums)
return angle % word_length == 0