Решение на Навигация на Piet от Айше Джинджи

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

Към профила на Айше Джинджи

Резултати

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

Код

# A dictionary with the supported colors.
# Key: The hex code of the color
# Value: A tuple containing two items - Hue and Saturation
COLORS = {
'C0FFC0': ('Green', 'Light'),
'FFFFC0': ('Yellow', 'Light'),
'FFC0C0': ('Red', 'Light'),
'C0C0FF': ('Blue', 'Light'),
'00C000': ('Green', 'Dark'),
'C0C000': ('Yellow', 'Dark'),
'C00000': ('Red', 'Dark'),
'0000C0': ('Blue', 'Dark'),
}
# A dictionary mapping the supported saturations to their direction
# Key: Saturation
# Value: Direction corresponding to the saturation
DIRECTIONS = {
'Green': 'Right',
'Yellow': 'Up',
'Red': 'Left',
'Blue': 'Down',
}
# A dictionary mapping the supported hues to their magnitude
# Key: Hue
# Value: Magnitude corresponding to the hue
MAGNITUDE = {
'Dark': 1,
'Light': -1,
}
def calculate_final_vector(starting_point, moves):
''' calculate_final_vector(starting_point: tuple, moves: list) -> tuple
Calculates the final vector, based on a starting point (x,y)
and a list of moves (hex values)
'''
# Storing the x and y values of the vector in seperate variables,
# as the tuple is immutable
current_x, current_y = starting_point
for color in moves:
# Make sure the function is case insensitive
color = color.upper()
# Consider white and black
if color == 'FFFFFF':
continue
if color == '000000':
break
# Make sure the color is valid
if color not in COLORS:
continue
hue = COLORS[color][1]
saturation = COLORS[color][0]
magnitude = MAGNITUDE[hue]
direction = DIRECTIONS[saturation]
# Calculate new positions
if direction == 'Right':
current_x = current_x + magnitude
elif direction == 'Left':
current_x = current_x - magnitude
elif direction == 'Up':
current_y = current_y + magnitude
elif direction == 'Down':
current_y = current_y - magnitude
return current_x, current_y

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

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

OK

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

Айше обнови решението на 23.10.2022 14:29 (преди около 2 години)

+# A dictionary with the supported colors.
+# Key: The hex code of the color
+# Value: A tuple containing two items - Hue and Saturation
+COLORS = {
+ 'C0FFC0': ('Green', 'Light'),
+ 'FFFFC0': ('Yellow', 'Light'),
+ 'FFC0C0': ('Red', 'Light'),
+ 'C0C0FF': ('Blue', 'Light'),
+ '00C000': ('Green', 'Dark'),
+ 'C0C000': ('Yellow', 'Dark'),
+ 'C00000': ('Red', 'Dark'),
+ '0000C0': ('Blue', 'Dark'),
+}
+
+# A dictionary mapping the supported saturations to their direction
+# Key: Saturation
+# Value: Direction corresponding to the saturation
+DIRECTIONS = {
+ 'Green': 'Right',
+ 'Yellow': 'Up',
+ 'Red': 'Left',
+ 'Blue': 'Down',
+}
+
+# A dictionary mapping the supported hues to their magnitude
+# Key: Hue
+# Value: Magnitude corresponding to the hue
+MAGNITUDE = {
+ 'Dark': 1,
+ 'Light': -1,
+}
+
+def get_magnitude(color):
+ ''' get_magnitude(color: string) -> string\n
+ Returns the magnitude corresponding to a color
+ '''
+ saturation = COLORS[color][1]
+ return MAGNITUDE[saturation]
+
+def get_direction(color):
+ ''' get_direction(color: string) -> string\n
+ Returns the direction corresponding to a color
+ '''
+ hue = COLORS[color][0]
+ return DIRECTIONS[hue]
+
+def is_white(color):
+ ''' is_white(color: string) -> bool\n
+ Checks if the color is white
+ '''
+ return color == 'FFFFFF'
+
+def is_black(color):
+ ''' is_black(color: string) -> bool\n
+ Checks if the color is black
+ '''
+ return color == '000000'
+
+def calculate_final_vector(starting_point, moves):
+ ''' calculate_final_vector(starting_point: tuple, moves: list) -> tuple\n
+ Calculates the final vector, based on a starting point (x,y) and a list of moves (hex values)
+ '''
+ # Storing the x and y values of the vector in seperate variables, as the tuple is immutable
+ current_x = starting_point[0]
+ current_y = starting_point[1]
+
+ for color in moves:
+ # Make sure the function is case insensitive
+ color = color.upper()
+
+ # Consider empty/end moves
+ if is_white(color):
+ continue
+ if is_black(color):
+ break
+
+ # Make sure the color is valid
+ if color not in COLORS:
+ continue
+
+ magnitude = get_magnitude(color)
+ direction = get_direction(color)
+
+ # Calculate new positions
+ if direction == 'Right':
+ current_x = current_x + magnitude
+ elif direction == 'Left':
+ current_x = current_x - magnitude
+ elif direction == 'Up':
+ current_y = current_y + magnitude
+ elif direction == 'Down':
+ current_y = current_y - magnitude
+
+ return (current_x, current_y)

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

Айше обнови решението на 25.10.2022 16:38 (преди около 2 години)

# A dictionary with the supported colors.
# Key: The hex code of the color
# Value: A tuple containing two items - Hue and Saturation
COLORS = {
'C0FFC0': ('Green', 'Light'),
'FFFFC0': ('Yellow', 'Light'),
'FFC0C0': ('Red', 'Light'),
'C0C0FF': ('Blue', 'Light'),
'00C000': ('Green', 'Dark'),
'C0C000': ('Yellow', 'Dark'),
'C00000': ('Red', 'Dark'),
'0000C0': ('Blue', 'Dark'),
}
# A dictionary mapping the supported saturations to their direction
# Key: Saturation
# Value: Direction corresponding to the saturation
DIRECTIONS = {
'Green': 'Right',
'Yellow': 'Up',
'Red': 'Left',
'Blue': 'Down',
}
# A dictionary mapping the supported hues to their magnitude
# Key: Hue
# Value: Magnitude corresponding to the hue
MAGNITUDE = {
'Dark': 1,
'Light': -1,
}
def get_magnitude(color):
- ''' get_magnitude(color: string) -> string\n
+ ''' get_magnitude(color: string) -> string
Returns the magnitude corresponding to a color
'''
saturation = COLORS[color][1]
return MAGNITUDE[saturation]
def get_direction(color):
- ''' get_direction(color: string) -> string\n
+ ''' get_direction(color: string) -> string
Returns the direction corresponding to a color
'''
hue = COLORS[color][0]
return DIRECTIONS[hue]
def is_white(color):
- ''' is_white(color: string) -> bool\n
+ ''' is_white(color: string) -> bool
Checks if the color is white
'''
return color == 'FFFFFF'
def is_black(color):
- ''' is_black(color: string) -> bool\n
+ ''' is_black(color: string) -> bool
Checks if the color is black
'''
return color == '000000'
def calculate_final_vector(starting_point, moves):
- ''' calculate_final_vector(starting_point: tuple, moves: list) -> tuple\n
- Calculates the final vector, based on a starting point (x,y) and a list of moves (hex values)
+ ''' calculate_final_vector(starting_point: tuple, moves: list) -> tuple
+ Calculates the final vector, based on a starting point (x,y)
+ and a list of moves (hex values)
'''
- # Storing the x and y values of the vector in seperate variables, as the tuple is immutable
- current_x = starting_point[0]
- current_y = starting_point[1]
+ # Storing the x and y values of the vector in seperate variables,
+ # as the tuple is immutable
+ current_x, current_y = starting_point
for color in moves:
# Make sure the function is case insensitive
color = color.upper()
# Consider empty/end moves
if is_white(color):
continue
if is_black(color):
break
# Make sure the color is valid
if color not in COLORS:
continue
magnitude = get_magnitude(color)
direction = get_direction(color)
# Calculate new positions
if direction == 'Right':
current_x = current_x + magnitude
elif direction == 'Left':
current_x = current_x - magnitude
elif direction == 'Up':
current_y = current_y + magnitude
elif direction == 'Down':
current_y = current_y - magnitude
- return (current_x, current_y)
+ return current_x, current_y

Айше обнови решението на 25.10.2022 16:44 (преди около 2 години)

# A dictionary with the supported colors.
# Key: The hex code of the color
# Value: A tuple containing two items - Hue and Saturation
COLORS = {
'C0FFC0': ('Green', 'Light'),
'FFFFC0': ('Yellow', 'Light'),
'FFC0C0': ('Red', 'Light'),
'C0C0FF': ('Blue', 'Light'),
'00C000': ('Green', 'Dark'),
'C0C000': ('Yellow', 'Dark'),
'C00000': ('Red', 'Dark'),
'0000C0': ('Blue', 'Dark'),
}
# A dictionary mapping the supported saturations to their direction
# Key: Saturation
# Value: Direction corresponding to the saturation
DIRECTIONS = {
'Green': 'Right',
'Yellow': 'Up',
'Red': 'Left',
'Blue': 'Down',
}
# A dictionary mapping the supported hues to their magnitude
# Key: Hue
# Value: Magnitude corresponding to the hue
MAGNITUDE = {
'Dark': 1,
'Light': -1,
}
-def get_magnitude(color):
- ''' get_magnitude(color: string) -> string
- Returns the magnitude corresponding to a color
- '''
- saturation = COLORS[color][1]
- return MAGNITUDE[saturation]
-
-def get_direction(color):
- ''' get_direction(color: string) -> string
- Returns the direction corresponding to a color
- '''
- hue = COLORS[color][0]
- return DIRECTIONS[hue]
-
-def is_white(color):
- ''' is_white(color: string) -> bool
- Checks if the color is white
- '''
- return color == 'FFFFFF'
-
-def is_black(color):
- ''' is_black(color: string) -> bool
- Checks if the color is black
- '''
- return color == '000000'
-
def calculate_final_vector(starting_point, moves):
''' calculate_final_vector(starting_point: tuple, moves: list) -> tuple
Calculates the final vector, based on a starting point (x,y)
and a list of moves (hex values)
'''
# Storing the x and y values of the vector in seperate variables,
# as the tuple is immutable
current_x, current_y = starting_point
for color in moves:
# Make sure the function is case insensitive
color = color.upper()
- # Consider empty/end moves
- if is_white(color):
+ # Consider white and black
+ if color == 'FFFFFF':
continue
- if is_black(color):
+ if color == '000000':
break
# Make sure the color is valid
if color not in COLORS:
continue
- magnitude = get_magnitude(color)
- direction = get_direction(color)
+ hue = COLORS[color][1]
+ saturation = COLORS[color][0]
+
+ magnitude = MAGNITUDE[hue]
+ direction = DIRECTIONS[saturation]
# Calculate new positions
if direction == 'Right':
current_x = current_x + magnitude
elif direction == 'Left':
current_x = current_x - magnitude
elif direction == 'Up':
current_y = current_y + magnitude
elif direction == 'Down':
current_y = current_y - magnitude
return current_x, current_y

Благодаря за обратната връзка, във втората версия оправих коментарите по решението, свързани с \n, броя символи на ред, unpack-ването и скобите, а в последната версия премахнах и функциите. Съгласна съм, че при по-прости задачки са по-скоро излишни.