Решение на Навигация на Piet от Сузана Петкова

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

Към профила на Сузана Петкова

Резултати

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

Код

import re
def calculate_final_vector(coordinates, input_directions):
coordinates = {'x': coordinates[0], 'y': coordinates[1]}
list_of_directions = [re.search(r'([0-9A-Fa-f]+)', direction).group() for direction in input_directions]
list_of_directions = [color.upper() for color in list_of_directions]
black = '000000'
# white = 'FFFFFF'
abscissa_colors = {
'C0FFC0': -1,
'FFC0C0': 1,
'00C000': 1,
'C00000': -1
}
ordinate_colors = {
'FFFFC0': -1,
'C0C000': 1,
'C0C0FF': 1,
'0000C0': -1
}
for direction in list_of_directions:
if direction == black:
return coordinates['x'], coordinates['y']
if direction in abscissa_colors:
coordinates['x'] += abscissa_colors[direction]
elif direction in ordinate_colors:
coordinates['y'] += ordinate_colors[direction]
else:
continue
return coordinates['x'], coordinates['y']

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

........
----------------------------------------------------------------------
Ran 8 tests in 0.097s

OK

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

Сузана обнови решението на 20.10.2022 14:36 (преди над 1 година)

+import re
+
+
+def calculate_final_vector(coordinates, list_of_directions):
+ black = '000000'
+ white = 'FFFFFF'
+
+ abscissa_colors = {
+ 'C0FFC0': -1,
+ 'FFC0C0': 1,
+ '00C000': 1,
+ 'C00000': -1
+ }
+
+ ordinate_colors = {
+ 'FFFFC0': -1,
+ 'C0C000': 1,
+ 'C0C0FF': 1,
+ '0000C0': -1
+ }
+
+ directions = [color.upper() for color in list_of_directions]
+
+ for direction in directions:
+ if direction == black:
+ return coordinates
+ if direction in abscissa_colors:
+ coordinates['x'] += abscissa_colors[direction]
+ elif direction in ordinate_colors and direction != white:
+ coordinates['y'] += ordinate_colors[direction]
+ else:
+ continue
+
+ return coordinates
+
+
+start_vector = input()
+match = re.search(r'\((\d+)\, *(\d+)\)', start_vector)
+
+x = int(match.group(1))
+y = int(match.group(2))
+
+start_coordinates = {'x': x, 'y': y}
+
+input_directions = list(input().split(', '))
+list_of_input_directions = [re.search(r'([0-9A-Fa-f]+)', direction).group() for direction in input_directions]
+
+result = calculate_final_vector(start_coordinates, list_of_input_directions)
+
+print(f"({result['x']}, {result['y']})")

Сузана обнови решението на 21.10.2022 18:34 (преди над 1 година)

-import re
+def calculate_final_vector(coordinates, input_directions):
+ match = re.search(r'\((\d+)\, *(\d+)\)', str(coordinates))
-def calculate_final_vector(coordinates, list_of_directions):
+ x = int(match.group(1))
+ y = int(match.group(2))
+
+ coordinates = {'x': x, 'y': y}
+
+ list_of_directions = [re.search(r'([0-9A-Fa-f]+)', direction).group() for direction in input_directions]
+ list_of_directions = [color.upper() for color in list_of_directions]
+
black = '000000'
white = 'FFFFFF'
abscissa_colors = {
'C0FFC0': -1,
'FFC0C0': 1,
'00C000': 1,
'C00000': -1
}
-
ordinate_colors = {
'FFFFC0': -1,
'C0C000': 1,
'C0C0FF': 1,
'0000C0': -1
}
-
- directions = [color.upper() for color in list_of_directions]
-
- for direction in directions:
+ for direction in list_of_directions:
if direction == black:
return coordinates
if direction in abscissa_colors:
coordinates['x'] += abscissa_colors[direction]
elif direction in ordinate_colors and direction != white:
coordinates['y'] += ordinate_colors[direction]
else:
continue
-
- return coordinates
+ result = (coordinates['x'], coordinates['y'])
-
+ return result
-
-start_vector = input()
-match = re.search(r'\((\d+)\, *(\d+)\)', start_vector)
-
-x = int(match.group(1))
-y = int(match.group(2))
-
-start_coordinates = {'x': x, 'y': y}
-
-input_directions = list(input().split(', '))
-list_of_input_directions = [re.search(r'([0-9A-Fa-f]+)', direction).group() for direction in input_directions]
-
-result = calculate_final_vector(start_coordinates, list_of_input_directions)
-
-print(f"({result['x']}, {result['y']})")

Нямаш import re. Всички тестове ще фейлнат. :)

В този ред на мисли, защо ти е? Какво ти пречи просто да вземеш данните както са подадени?
coordinates = {'x': coordinates[0], 'y': coordinates[1]} е на практика същото, като това, което ти правиш, без да има конвертиране към стринг, мачване на regex pattern, конвертиране към инт. И всичките тези регулярни изрази те предпазват от... нищо.

Сузана обнови решението на 24.10.2022 11:48 (преди над 1 година)

+import re
+
def calculate_final_vector(coordinates, input_directions):
- match = re.search(r'\((\d+)\, *(\d+)\)', str(coordinates))
+ coordinates = {'x': coordinates[0], 'y': coordinates[1]}
- x = int(match.group(1))
- y = int(match.group(2))
-
- coordinates = {'x': x, 'y': y}
-
list_of_directions = [re.search(r'([0-9A-Fa-f]+)', direction).group() for direction in input_directions]
list_of_directions = [color.upper() for color in list_of_directions]
black = '000000'
- white = 'FFFFFF'
+ # white = 'FFFFFF'
abscissa_colors = {
'C0FFC0': -1,
'FFC0C0': 1,
'00C000': 1,
'C00000': -1
}
ordinate_colors = {
'FFFFC0': -1,
'C0C000': 1,
'C0C0FF': 1,
'0000C0': -1
}
for direction in list_of_directions:
if direction == black:
- return coordinates
+ return coordinates['x'], coordinates['y']
if direction in abscissa_colors:
coordinates['x'] += abscissa_colors[direction]
- elif direction in ordinate_colors and direction != white:
+ elif direction in ordinate_colors:
coordinates['y'] += ordinate_colors[direction]
else:
continue
- result = (coordinates['x'], coordinates['y'])
- return result
+
+ return coordinates['x'], coordinates['y']