Решение на Навигация на Piet от Марио Николов

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

Към профила на Марио Николов

Резултати

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

Код

def calculate_final_vector(coordinates, colors):
color_pallete = {
"C0FFC0": "Light Green",
"FFFFC0": "Light Yellow",
"FFC0C0": "Light Red",
"C0C0FF": "Light Blue",
"00C000": "Dark Green",
"C0C000": "Dark Yellow",
"C00000": "Dark Red",
"0000C0": "Dark Blue",
"FFFFFF": "White",
"000000": "Black"
}
x, y = coordinates
formatted_colors = map(lambda x: x.upper(), colors)
for color in formatted_colors:
if color_pallete[color] == "Light Green":
x -= 1
elif color_pallete[color] == "Light Yellow":
y -= 1
elif color_pallete[color] == "Light Red":
x += 1
elif color_pallete[color] == "Light Blue":
y += 1
elif color_pallete[color] == "Dark Green":
x += 1
elif color_pallete[color] == "Dark Yellow":
y += 1
elif color_pallete[color] == "Dark Red":
x -= 1
elif color_pallete[color] == "Dark Blue":
y -= 1
elif color_pallete[color] == "White":
continue
elif color_pallete[color] == "Black":
break
return x, y

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

........
----------------------------------------------------------------------
Ran 8 tests in 0.085s

OK

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

Марио обнови решението на 21.10.2022 15:09 (преди около 2 години)

+def calculate_final_vector(coordinates, colors):
+ colorPallete = {
+ "C0FFC0": "Light Green",
+ "FFFFC0": "Light Yellow",
+ "FFC0C0": "Light Red",
+ "C0C0FF": "Light Blue",
+ "00C000": "Dark Green",
+ "C0C000": "Dark Yellow",
+ "C00000": "Dark Red",
+ "0000C0": "Dark Blue",
+ "FFFFFF": "White",
+ "000000": "Black"
+ }
+
+ first, second = coordinates
+ formattedColors = map(lambda x: x.upper(), colors)
+
+ for color in formattedColors:
+ if colorPallete[color] == "Light Green":
+ first = first-1
+ elif colorPallete[color] == "Light Yellow":
+ second = second-1
+ elif colorPallete[color] == "Light Red":
+ first = first+1
+ elif colorPallete[color] == "Light Blue":
+ second = second+1
+ elif colorPallete[color] == "Dark Green":
+ first = first+1
+ elif colorPallete[color] == "Dark Yellow":
+ second = second+1
+ elif colorPallete[color] == "Dark Red":
+ first = first-1
+ elif colorPallete[color] == "Dark Blue":
+ second = second-1
+ elif colorPallete[color] == "White":
+ continue
+ elif colorPallete[color] == "Black":
+ break
+
+ return (first, second)

Марио обнови решението на 21.10.2022 15:29 (преди около 2 години)

def calculate_final_vector(coordinates, colors):
colorPallete = {
"C0FFC0": "Light Green",
"FFFFC0": "Light Yellow",
"FFC0C0": "Light Red",
"C0C0FF": "Light Blue",
"00C000": "Dark Green",
"C0C000": "Dark Yellow",
"C00000": "Dark Red",
"0000C0": "Dark Blue",
"FFFFFF": "White",
"000000": "Black"
}
first, second = coordinates
formattedColors = map(lambda x: x.upper(), colors)
for color in formattedColors:
if colorPallete[color] == "Light Green":
first = first-1
elif colorPallete[color] == "Light Yellow":
second = second-1
elif colorPallete[color] == "Light Red":
first = first+1
elif colorPallete[color] == "Light Blue":
second = second+1
elif colorPallete[color] == "Dark Green":
first = first+1
elif colorPallete[color] == "Dark Yellow":
second = second+1
elif colorPallete[color] == "Dark Red":
first = first-1
elif colorPallete[color] == "Dark Blue":
second = second-1
elif colorPallete[color] == "White":
continue
elif colorPallete[color] == "Black":
break
- return (first, second)
+ return (first, second)

Съветвам те да прочетеш как се именоват променливи в Python и да преименуваш camelCase променливите си.
В същият документ ще намериш и това, че е хубаво да оставяш интервали около операторите (в повечето случаи) - first = first + 1.

Също така, tuple unpacking-а няма нужда от скоби (както и return-а).

Марио обнови решението на 25.10.2022 14:52 (преди около 2 години)

def calculate_final_vector(coordinates, colors):
- colorPallete = {
+ color_pallete = {
"C0FFC0": "Light Green",
"FFFFC0": "Light Yellow",
"FFC0C0": "Light Red",
"C0C0FF": "Light Blue",
"00C000": "Dark Green",
"C0C000": "Dark Yellow",
"C00000": "Dark Red",
"0000C0": "Dark Blue",
"FFFFFF": "White",
"000000": "Black"
}
- first, second = coordinates
- formattedColors = map(lambda x: x.upper(), colors)
+ x, y = coordinates
+ formatted_colors = map(lambda x: x.upper(), colors)
- for color in formattedColors:
- if colorPallete[color] == "Light Green":
- first = first-1
- elif colorPallete[color] == "Light Yellow":
- second = second-1
- elif colorPallete[color] == "Light Red":
- first = first+1
- elif colorPallete[color] == "Light Blue":
- second = second+1
- elif colorPallete[color] == "Dark Green":
- first = first+1
- elif colorPallete[color] == "Dark Yellow":
- second = second+1
- elif colorPallete[color] == "Dark Red":
- first = first-1
- elif colorPallete[color] == "Dark Blue":
- second = second-1
- elif colorPallete[color] == "White":
+ for color in formatted_colors:
+ if color_pallete[color] == "Light Green":
+ x -= 1
+ elif color_pallete[color] == "Light Yellow":
+ y -= 1
+ elif color_pallete[color] == "Light Red":
+ x += 1
+ elif color_pallete[color] == "Light Blue":
+ y += 1
+ elif color_pallete[color] == "Dark Green":
+ x += 1
+ elif color_pallete[color] == "Dark Yellow":
+ y += 1
+ elif color_pallete[color] == "Dark Red":
+ x -= 1
+ elif color_pallete[color] == "Dark Blue":
+ y -= 1
+ elif color_pallete[color] == "White":
continue
- elif colorPallete[color] == "Black":
+ elif color_pallete[color] == "Black":
break
- return (first, second)
+ return x, y