Решение на Навигация на Piet от Назифе Алиджик

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

Към профила на Назифе Алиджик

Резултати

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

Код

def calculate_final_vector(starting, hex_codes):
x_point, y_point = starting
for hex_code in hex_codes:
# white
if hex_code.upper() == 'FFFFFF':
continue
# black
elif hex_code.upper() == '000000':
break
# dark colors
elif hex_code.upper() == '00C000':
x_point += 1
elif hex_code.upper() == 'C00000':
x_point -= 1
elif hex_code.upper() == 'C0C000':
y_point += 1
elif hex_code.upper() == '0000C0':
y_point -= 1
# light colors
elif hex_code.upper() == 'C0FFC0':
x_point -= 1
elif hex_code.upper() == 'FFFFC0':
y_point -= 1
elif hex_code.upper() == 'FFC0C0':
x_point += 1
elif hex_code.upper() == 'C0C0FF':
y_point += 1
return x_point, y_point

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

........
----------------------------------------------------------------------
Ran 8 tests in 0.087s

OK

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

Назифе обнови решението на 23.10.2022 21:19 (преди около 2 години)

+def calculate_final_vector(starting, hex_codes):
+
+ starting_point = list(starting)

Използвай unpacking. x_point, y_point = starting прави същото, което ти постигаш с три реда.

(Edit): Сега виждам, че използваш starting_point като склад, в който слагаш всички стъпки, така че коментарът ми по-горе не е много ясен. Разяснявам. Няма нужда да пазиш всички стъпки в list, защото те интересуват само последните координати. Т.е. unpack-ването по-горе е достатъчна отправна точка. След това само променяш стойността на x_point и y_point и когато списъкът се изчерпа, връщаш техните стойности.

+ x_point = starting_point[0]
+ y_point = starting_point[1]
+
+ for hex_code in hex_codes:
+
+ # white
+ if hex_code.upper() == 'FFFFFF':
+ continue
+
+ # black
+ elif hex_code.upper() == '000000':
+ break
+
+ # dark colors
+ elif hex_code.upper() == '00C000':
+ x_point = starting_point[-2] + 1
+ y_point = starting_point[-1]
+
+ elif hex_code.upper() == 'C00000':
+ x_point = starting_point[-2] - 1
+ y_point = starting_point[-1]
+
+ elif hex_code.upper() == 'C0C000':
+ y_point = starting_point[-1] + 1
+ x_point = starting_point[-2]
+
+ elif hex_code.upper() == '0000C0':
+ y_point = starting_point[-1] - 1
+ x_point = starting_point[-2]
+
+ # light colors
+ elif hex_code.upper() == 'C0FFC0':
+ x_point = starting_point[-2] - 1
+ y_point = starting_point[-1]
+
+ elif hex_code.upper() == 'FFFFC0':
+ y_point = starting_point[-1] - 1
+ x_point = starting_point[-2]
+
+ elif hex_code.upper() == 'FFC0C0':
+ x_point = starting_point[-2] + 1
+ y_point = starting_point[-1]
+
+ elif hex_code.upper() == 'C0C0FF':
+ y_point = starting_point[-1] + 1
+ x_point = starting_point[-2]
+
+ starting_point.append(x_point)
+ starting_point.append(y_point)
+
+ point_x = starting_point[-2]
+ point_y = starting_point[-1]
+
+ final_answer = (point_x, point_y)

Да, но в случая, както и в подадения пример в задачата, резултата на функцията се изписва като tuple тоест (0,2), но return point_x, point_y връща 0 2 и затова бях написала така, но мисля, че няма значение много. Оправих го и много много благогаря!

+
+ print(final_answer)
+ return (final_answer)
+

Назифе обнови решението на 25.10.2022 11:24 (преди около 2 години)

-def calculate_final_vector(starting, hex_codes):
+def calculate_final_vector(starting,hex_codes):
- starting_point = list(starting)
- x_point = starting_point[0]
- y_point = starting_point[1]
+ x_point, y_point = starting
for hex_code in hex_codes:
- # white
- if hex_code.upper() == 'FFFFFF':
- continue
+ # white
+ if hex_code.upper() == 'FFFFFF':
+ continue
- # black
- elif hex_code.upper() == '000000':
- break
+ # black
+ elif hex_code.upper() == '000000':
+ break
- # dark colors
- elif hex_code.upper() == '00C000':
- x_point = starting_point[-2] + 1
- y_point = starting_point[-1]
+ # dark colors
+ elif hex_code.upper() == '00C000':
+ x_point += 1
- elif hex_code.upper() == 'C00000':
- x_point = starting_point[-2] - 1
- y_point = starting_point[-1]
+ elif hex_code.upper() == 'C00000':
+ x_point -= 1
- elif hex_code.upper() == 'C0C000':
- y_point = starting_point[-1] + 1
- x_point = starting_point[-2]
+ elif hex_code.upper() == 'C0C000':
+ y_point += 1
+
+ elif hex_code.upper() == '0000C0':
+ y_point -= 1
+
+ # light colors
+ elif hex_code.upper() == 'C0FFC0':
+ x_point -= 1
- elif hex_code.upper() == '0000C0':
- y_point = starting_point[-1] - 1
+ elif hex_code.upper() == 'FFFFC0':
- x_point = starting_point[-2]
+ y_point -= 1
-
+
- # light colors
+ elif hex_code.upper() == 'FFC0C0':
- elif hex_code.upper() == 'C0FFC0':
+ x_point += 1
- x_point = starting_point[-2] - 1
+
- y_point = starting_point[-1]
+ elif hex_code.upper() == 'C0C0FF':
-
+ y_point += 1
- elif hex_code.upper() == 'FFFFC0':
+
- y_point = starting_point[-1] - 1
+ return x_point,y_point
- x_point = starting_point[-2]
-
- elif hex_code.upper() == 'FFC0C0':
- x_point = starting_point[-2] + 1
- y_point = starting_point[-1]
-
- elif hex_code.upper() == 'C0C0FF':
- y_point = starting_point[-1] + 1
- x_point = starting_point[-2]
-
- starting_point.append(x_point)
- starting_point.append(y_point)
-
- point_x = starting_point[-2]
- point_y = starting_point[-1]
-
- final_answer = (point_x, point_y)
-
- print(final_answer)
- return (final_answer)
-

Назифе обнови решението на 25.10.2022 11:37 (преди около 2 години)

-def calculate_final_vector(starting,hex_codes):
+def calculate_final_vector(starting, hex_codes):
x_point, y_point = starting
for hex_code in hex_codes:
# white
if hex_code.upper() == 'FFFFFF':
continue
# black
elif hex_code.upper() == '000000':
break
# dark colors
elif hex_code.upper() == '00C000':
x_point += 1
elif hex_code.upper() == 'C00000':
x_point -= 1
elif hex_code.upper() == 'C0C000':
y_point += 1
elif hex_code.upper() == '0000C0':
y_point -= 1
# light colors
elif hex_code.upper() == 'C0FFC0':
x_point -= 1
elif hex_code.upper() == 'FFFFC0':
y_point -= 1
elif hex_code.upper() == 'FFC0C0':
x_point += 1
elif hex_code.upper() == 'C0C0FF':
y_point += 1
- return x_point,y_point
+ return x_point, y_point