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

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

Към профила на Виктор Христов

Резултати

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

Код

def calculate_final_vector(coordinates, colors):
for index in range(len(colors)):
colors[index] = colors[index].upper()
for color in colors:
#for testing purposes
#temp = coordinates
match color:
case 'FFFFFF':
#White - does nothing
continue
case '000000':
#Black - return coordinates
return coordinates
case 'C0FFC0':
#Light green - move to right with -1
coordinates = (coordinates[0] - 1, coordinates[1])
case '00C000':
#Dark green - move to right with 1
coordinates = (coordinates[0] + 1, coordinates[1])
case 'FFFFC0':
#Light yellow - move to up with -1
coordinates = (coordinates[0], coordinates[1] - 1)
case 'C0C000':
#Dark yellow - move to up with 1
coordinates = (coordinates[0], coordinates[1] + 1)
case 'FFC0C0':
#Light red - move to left with -1
coordinates = (coordinates[0] + 1, coordinates[1])
case 'C00000':
#Dark red - move to left with 1
coordinates = (coordinates[0] - 1, coordinates[1])
case 'C0C0FF':
#Light blue - move to down with -1
coordinates = (coordinates[0], coordinates[1] + 1)
case '0000C0':
#Dark blue - move to down with 1
coordinates = (coordinates[0], coordinates[1] - 1)
case _:
return -1

Няма да тестваме с невалидни данни, и това не е забележка, която изисква да си променяш кода, но имай предвид, че в Python -1 няма специално значение, както в някои други езици. :)

#for testing purposes
#print('From ', temp,' after ', color, ' result is ', coordinates)
return coordinates

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

........
----------------------------------------------------------------------
Ran 8 tests in 0.095s

OK

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

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

+def calculate_final_vector(coordinates, colors):
+ for index in range(len(colors)):
+ colors[index] = colors[index].upper()
+
+ for color in colors:
+ #for testing purposes
+ #temp = coordinates
+
+ if color == 'FFFFFF':
+ #White - does nothing
+ continue
+ elif color == '000000':
+ #Black - return coordinates
+ return coordinates
+ elif color == 'C0FFC0':
+ #Light green - move to right with -1
+ coordinates = (coordinates[0] - 1, coordinates[1])
+ elif color == '00C000':
+ #Dark green - move to right with 1
+ coordinates = (coordinates[0] + 1, coordinates[1])
+ elif color == 'FFFFC0':
+ #Light yellow - move to up with -1
+ coordinates = (coordinates[0], coordinates[1] - 1)
+ elif color == 'C0C000':
+ #Dark yellow - move to up with 1
+ coordinates = (coordinates[0], coordinates[1] + 1)
+ elif color == 'FFC0C0':
+ #Light red - move to left with -1
+ coordinates = (coordinates[0] + 1, coordinates[1])
+ elif color == 'C00000':
+ #Dark red - move to left with 1
+ coordinates = (coordinates[0] - 1, coordinates[1])
+ elif color == 'C0C0FF':
+ #Light blue - move to down with -1
+ coordinates = (coordinates[0], coordinates[1] + 1)
+ elif color == '0000C0':
+ #Dark blue - move to down with 1
+ coordinates = (coordinates[0], coordinates[1] - 1)
+ else:
+ return -1
+
+ #for testing purposes
+ #print('From ', temp,' after ', color, ' result is ', coordinates)
+
+ return coordinates

Оригинално бях използвал match case, но в условията за предаване пишеше да използваме Python 3.5.1, a match case е feature на Python 3.10, така че го направих така. Искам да питам дали може да използваме Python 3.10 за следващите домашни.

Nvm. Току що видях поста и понеже не мога да изтрия коментара просто ще добавя новото решение за Python 3.10.

Btw не ми позволява да си кача кода и ми казва, че има грешки. Тествах си кода и дори run-нах unitest-а, и беше OK, затова ще си paste-на кода тук в коментара.

Виктор обнови решението на 20.10.2022 19:27 (преди около 2 години)

def calculate_final_vector(coordinates, colors):
for index in range(len(colors)):
colors[index] = colors[index].upper()
for color in colors:
#for testing purposes
#temp = coordinates
- if color == 'FFFFFF':
- #White - does nothing
- continue
- elif color == '000000':
- #Black - return coordinates
- return coordinates
- elif color == 'C0FFC0':
- #Light green - move to right with -1
- coordinates = (coordinates[0] - 1, coordinates[1])
- elif color == '00C000':
- #Dark green - move to right with 1
- coordinates = (coordinates[0] + 1, coordinates[1])
- elif color == 'FFFFC0':
- #Light yellow - move to up with -1
- coordinates = (coordinates[0], coordinates[1] - 1)
- elif color == 'C0C000':
- #Dark yellow - move to up with 1
- coordinates = (coordinates[0], coordinates[1] + 1)
- elif color == 'FFC0C0':
- #Light red - move to left with -1
- coordinates = (coordinates[0] + 1, coordinates[1])
- elif color == 'C00000':
- #Dark red - move to left with 1
- coordinates = (coordinates[0] - 1, coordinates[1])
- elif color == 'C0C0FF':
- #Light blue - move to down with -1
- coordinates = (coordinates[0], coordinates[1] + 1)
- elif color == '0000C0':
- #Dark blue - move to down with 1
- coordinates = (coordinates[0], coordinates[1] - 1)
- else:
- return -1
+ match color:
+ case 'FFFFFF':
+ #White - does nothing
+ continue
+ case '000000':
+ #Black - return coordinates
+ return coordinates
+ case 'C0FFC0':
+ #Light green - move to right with -1
+ coordinates = (coordinates[0] - 1, coordinates[1])
+ case '00C000':
+ #Dark green - move to right with 1
+ coordinates = (coordinates[0] + 1, coordinates[1])
+ case 'FFFFC0':
+ #Light yellow - move to up with -1
+ coordinates = (coordinates[0], coordinates[1] - 1)
+ case 'C0C000':
+ #Dark yellow - move to up with 1
+ coordinates = (coordinates[0], coordinates[1] + 1)
+ case 'FFC0C0':
+ #Light red - move to left with -1
+ coordinates = (coordinates[0] + 1, coordinates[1])
+ case 'C00000':
+ #Dark red - move to left with 1
+ coordinates = (coordinates[0] - 1, coordinates[1])
+ case 'C0C0FF':
+ #Light blue - move to down with -1
+ coordinates = (coordinates[0], coordinates[1] + 1)
+ case '0000C0':
+ #Dark blue - move to down with 1
+ coordinates = (coordinates[0], coordinates[1] - 1)
+ case _:
+ return -1

Няма да тестваме с невалидни данни, и това не е забележка, която изисква да си променяш кода, но имай предвид, че в Python -1 няма специално значение, както в някои други езици. :)

#for testing purposes
#print('From ', temp,' after ', color, ' result is ', coordinates)
return coordinates