Решение на Навигация на Piet от Весела Петрова

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

Към профила на Весела Петрова

Резултати

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

Код

def calculate_final_vector(coords_vector, color_list):
final_coords_vector = coords_vector
for color in color_list:
match color.upper():
case 'C0FFC0': #light green
final_coords_vector = (final_coords_vector[0] - 1, final_coords_vector[1])
case 'FFFFC0': #light yellow
final_coords_vector = (final_coords_vector[0], final_coords_vector[1] - 1)
case 'FFC0C0': #light red
final_coords_vector = (final_coords_vector[0] + 1, final_coords_vector[1])
case 'C0C0FF': #light blue
final_coords_vector = (final_coords_vector[0], final_coords_vector[1] + 1)
case '00C000': #dark green
final_coords_vector = (final_coords_vector[0] + 1, final_coords_vector[1])
case 'C0C000': #dark yellow
final_coords_vector = (final_coords_vector[0], final_coords_vector[1] + 1)
case 'C00000': #dark red
final_coords_vector = (final_coords_vector[0] - 1, final_coords_vector[1])
case '0000C0': #dark blue
final_coords_vector = (final_coords_vector[0], final_coords_vector[1] - 1)
case 'FFFFFF': #white
continue
case '000000': #black
break
return final_coords_vector

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

........
----------------------------------------------------------------------
Ran 8 tests in 0.088s

OK

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

Весела обнови решението на 25.10.2022 02:37 (преди около 2 години)

+def calculate_final_vector(coordsVector, colorList) :
+ finalCoordsVector = coordsVector
+
+ for color in colorList:
+ match color.upper():
+ case 'C0FFC0': #light green
+ finalCoordsVector = (finalCoordsVector[0] - 1, finalCoordsVector[1])
+ case 'FFFFC0': #light yellow
+ finalCoordsVector = (finalCoordsVector[0], finalCoordsVector[1] - 1)
+ case 'FFC0C0': #light red
+ finalCoordsVector = (finalCoordsVector[0] + 1, finalCoordsVector[1])
+ case 'C0C0FF': #light blue
+ finalCoordsVector = (finalCoordsVector[0], finalCoordsVector[1] + 1)
+ case '00C000': #dark green
+ finalCoordsVector = (finalCoordsVector[0] + 1, finalCoordsVector[1])
+ case 'C0C000': #dark yellow
+ finalCoordsVector = (finalCoordsVector[0], finalCoordsVector[1] + 1)
+ case 'C00000': #dark red
+ finalCoordsVector = (finalCoordsVector[0] - 1, finalCoordsVector[1])
+ case '0000C0': #dark blue
+ finalCoordsVector = (finalCoordsVector[0], finalCoordsVector[1] - 1)
+ case 'FFFFFF': #white
+ continue
+ case '000000': #black
+ return finalCoordsVector

Можеш директно да break-неш и ще се изпълни следващия ред. Така избягваш дублиране на код, а и като цяло е добра практика да се стремиш да имаш само един return, поне според някои линтери.

+ return finalCoordsVector

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

-def calculate_final_vector(coordsVector, colorList) :
- finalCoordsVector = coordsVector
+def calculate_final_vector(coords_vector, color_list):
+ final_coords_vector = coords_vector
- for color in colorList:
+ for color in color_list:
match color.upper():
case 'C0FFC0': #light green
- finalCoordsVector = (finalCoordsVector[0] - 1, finalCoordsVector[1])
+ final_coords_vector = (final_coords_vector[0] - 1, final_coords_vector[1])
case 'FFFFC0': #light yellow
- finalCoordsVector = (finalCoordsVector[0], finalCoordsVector[1] - 1)
+ final_coords_vector = (final_coords_vector[0], final_coords_vector[1] - 1)
case 'FFC0C0': #light red
- finalCoordsVector = (finalCoordsVector[0] + 1, finalCoordsVector[1])
+ final_coords_vector = (final_coords_vector[0] + 1, final_coords_vector[1])
case 'C0C0FF': #light blue
- finalCoordsVector = (finalCoordsVector[0], finalCoordsVector[1] + 1)
+ final_coords_vector = (final_coords_vector[0], final_coords_vector[1] + 1)
case '00C000': #dark green
- finalCoordsVector = (finalCoordsVector[0] + 1, finalCoordsVector[1])
+ final_coords_vector = (final_coords_vector[0] + 1, final_coords_vector[1])
case 'C0C000': #dark yellow
- finalCoordsVector = (finalCoordsVector[0], finalCoordsVector[1] + 1)
+ final_coords_vector = (final_coords_vector[0], final_coords_vector[1] + 1)
case 'C00000': #dark red
- finalCoordsVector = (finalCoordsVector[0] - 1, finalCoordsVector[1])
+ final_coords_vector = (final_coords_vector[0] - 1, final_coords_vector[1])
case '0000C0': #dark blue
- finalCoordsVector = (finalCoordsVector[0], finalCoordsVector[1] - 1)
+ final_coords_vector = (final_coords_vector[0], final_coords_vector[1] - 1)
case 'FFFFFF': #white
continue
case '000000': #black
- return finalCoordsVector
- return finalCoordsVector
+ break
+ return final_coords_vector