Решение на Навигация на Piet от Антоан Ивайлов

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

Към профила на Антоан Ивайлов

Резултати

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

Код

def validate_colours(colours):
# This function will make all colours in the given list to be uppercase only
# as to allow dealing with randomized letter-casing
curr_index = 0
while curr_index < len(colours):
colours[curr_index] = colours[curr_index].upper()
curr_index += 1
def calculate_final_vector(input_vector, colours):
LIGHT_GREEN = "C0FFC0"
DARK_GREEN = "00C000"
LIGHT_YELLOW = "FFFFC0"
DARK_YELLOW = "C0C000"
LIGHT_RED = "FFC0C0"
DARK_RED = "C00000"
LIGHT_BLUE = "C0C0FF"
DARK_BLUE = "0000C0"
# Since White doesn't do anything, we will skip it as a case and as a variable
# We will assume that all colours which are not special will go to the default case together with white
BLACK = "000000"
# We can actually just unpack the tuple on 1 line, without using indexes
final_x, final_y = input_vector
validate_colours(colours)
# Negative direction - 1 = Opposite direction + 1, so we can group 5 unique cases including black
for curr_colour in colours:
if curr_colour == BLACK:
break
elif curr_colour == DARK_GREEN or curr_colour == LIGHT_RED:
final_x += 1
elif curr_colour == DARK_RED or curr_colour == LIGHT_GREEN:
final_x -= 1
elif curr_colour == DARK_YELLOW or curr_colour == LIGHT_BLUE:
final_y += 1
elif curr_colour == DARK_BLUE or curr_colour == LIGHT_YELLOW:
final_y -= 1
# Instead of creating a tuple and then passing it to return,
# we could just make the return function create the needed tuple
return final_x, final_y

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

........
----------------------------------------------------------------------
Ran 8 tests in 0.075s

OK

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

Антоан обнови решението на 22.10.2022 13:41 (преди над 1 година)

+def validate_colours(colours):
+ # This function will make all colours in the given list to be uppercase only
+ # as to allow dealing with randomized letter-casing
+ curr_index = 0
+ while curr_index < len(colours):
+ colours[curr_index] = colours[curr_index].upper()
+ curr_index += 1
+
+
+def calculate_final_vector(input_vector, colours):
+ LIGHT_GREEN = "C0FFC0"
+ DARK_GREEN = "00C000"
+ LIGHT_YELLOW = "FFFFC0"
+ DARK_YELLOW = "C0C000"
+ LIGHT_RED = "FFC0C0"
+ DARK_RED = "C00000"
+ LIGHT_BLUE = "C0C0FF"
+ DARK_BLUE = "0000C0"
+
+ # Since White doesn't do anything, we will skip it as a case and as a variable
+ # We will assume that all colours which are not special will go to the default case together with white
+ BLACK = "000000"
+
+ final_x = input_vector[0]
+ final_y = input_vector[1]
+ validate_colours(colours)
+ # Negative direction - 1 = Opposite direction + 1, so we can group 5 unique cases including black
+
+ for curr_colour in colours:
+ if curr_colour == BLACK:
+ break
+ elif curr_colour == DARK_GREEN or curr_colour == LIGHT_RED:
+ final_x += 1
+ elif curr_colour == DARK_RED or curr_colour == LIGHT_GREEN:
+ final_x -= 1
+ elif curr_colour == DARK_YELLOW or curr_colour == LIGHT_BLUE:
+ final_y += 1
+ elif curr_colour == DARK_BLUE or curr_colour == LIGHT_YELLOW:
+ final_y -= 1
+
+ output_vector = final_x, final_y
+ return output_vector

Антоан обнови решението на 24.10.2022 14:24 (преди над 1 година)

def validate_colours(colours):
# This function will make all colours in the given list to be uppercase only
# as to allow dealing with randomized letter-casing
curr_index = 0
while curr_index < len(colours):
colours[curr_index] = colours[curr_index].upper()
curr_index += 1
def calculate_final_vector(input_vector, colours):
LIGHT_GREEN = "C0FFC0"
DARK_GREEN = "00C000"
LIGHT_YELLOW = "FFFFC0"
DARK_YELLOW = "C0C000"
LIGHT_RED = "FFC0C0"
DARK_RED = "C00000"
LIGHT_BLUE = "C0C0FF"
DARK_BLUE = "0000C0"
# Since White doesn't do anything, we will skip it as a case and as a variable
# We will assume that all colours which are not special will go to the default case together with white
BLACK = "000000"
- final_x = input_vector[0]
- final_y = input_vector[1]
+ # We can actually just unpack the tuple on 1 line, without using indexes
+ final_x, final_y = input_vector
validate_colours(colours)
- # Negative direction - 1 = Opposite direction + 1, so we can group 5 unique cases including black
+ # Negative direction - 1 = Opposite direction + 1, so we can group 5 unique cases including black
for curr_colour in colours:
if curr_colour == BLACK:
break
elif curr_colour == DARK_GREEN or curr_colour == LIGHT_RED:
final_x += 1
elif curr_colour == DARK_RED or curr_colour == LIGHT_GREEN:
final_x -= 1
elif curr_colour == DARK_YELLOW or curr_colour == LIGHT_BLUE:
final_y += 1
elif curr_colour == DARK_BLUE or curr_colour == LIGHT_YELLOW:
final_y -= 1
- output_vector = final_x, final_y
- return output_vector
+ # Instead of creating a tuple and then passing it to return,
+ # we could just make the return function create the needed tuple
+ return final_x, final_y

Ок, напълно се съгласих. Наистина би било по-прегледно, ако се инициализират наведнъж променливите, вървейки по кортежа и особено това с return-a. Просто не ми беше много ясно дали би тръгнало (може би трябваше да го изтествам и по този начин преди да го бях пуснал първия път).

  • Съвет: Най-вероятно хората като мен, които са втори курс наистина не знаят какво е map, list, tuple и други структури от данни в детайли, тъй като в момента ни тече този курс по СДА. По тази тема мисля, че беше изсипана много информация на втората лекция и стана малко каша. Ще се опитам да наваксам допълнително, но имайте в предвид, че ако човек преди ФМИ не се е занимавал със състезателно програмиране, няма откъде да знае тези концепции (ВвПнР все пак пишеше, че е за от 2ри и нагоре курс - може би ще е добра идея, ако го водите догодина, да го направите от 3-ти и нагоре или пък да разместите подредбата на темите, или да обърнете повече внимание откъм идейна гледна точка на структурите от данни). Все пак много ме кефите, евала за старанието, мисля че ще се получи интересен курс.