Решение на Навигация на Piet от Георги

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

Към профила на Георги

Резултати

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

Код

def calculate_final_vector(init_vector, hexes):
"""Calculate final vector based on initial vector and list of hexes."""
# Unpack the input tuple so that its coordinates are mutable objects
x, y = init_vector
# Define light/dark saturation with their corresponding steps and
# full/empty values for color definition
saturations = (
{
'units': {
'full': 'ff',
'empty': 'c0'
},
'step': -1,
},
{
'units': {
'full': 'c0',
'empty': '00'
},
'step': +1,
}
)
# Map the hex list to lower strings and process each hex one by one
for hex in map(str.lower, hexes):
# Determine hex type
match hex:
case 'ffffff':
# White colors means no instruction - skip the current hex
continue
case '000000':
# Black colors means to halt - stop the loop
break
case _:
# Split color into RGB components to ensure it fits into
# one of the expected saturation definitions
r, g, b = hex[:2], hex[2:4], hex[4:]
# Try to find matching saturation
for satur in saturations:
if all(x in satur['units'].values() for x in (r, g, b)):
# Take leaf-level values from the saturation
# definition for better code readbility
full = satur['units']['full']
empty = satur['units']['empty']
# Determine the color and perform the action
if (r, g, b) == (full, empty, empty): # Red
x -= satur['step']
elif (r, g, b) == (full, full, empty): # Yellow
y += satur['step']
elif (r, g, b) == (empty, full, empty): # Green
x += satur['step']
elif (r, g, b) == (empty, empty, full): # Blue
y -= satur['step']
else:
raise ValueError(f'Invalid color hex: {hex}')
break
# If there was no matching saturation, meaning no break,
# that indicates invalid input
else:
raise ValueError(f'Invalid saturation for: {hex}.')
return x, y

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

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

OK

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

Георги обнови решението на 18.10.2022 23:27 (преди около 2 години)

+def calculate_final_vector(init_vector, hexes):
+ """Calculate final vector based on initial vector and list of hexes."""
+ # Unpack the input tuple so that its coordinates are mutable objects
+ x, y = init_vector
+ # Define light/dark saturation with their corresponding steps and
+ # full/empty values for color definition
+ saturations = (
+ {
+ 'units': {
+ 'full': 'ff',
+ 'empty': 'c0'
+ },
+ 'step': -1,
+ },
+ {
+ 'units': {
+ 'full': 'c0',
+ 'empty': '00'
+ },
+ 'step': +1,
+ }
+ )
+ # Map the hex list to lower strings and process each hex one by one
+ for hex in map(str.lower, hexes):
+ # Determine hex type
+ if hex == 'ffffff':
+ # White colors means no instruction - skip the current hex
+ continue
+ elif hex == '000000':
+ # Black colors means to halt - stop the loop
+ break
+ else:
+ # Split color into RGB components to ensure it fits into
+ # one of the expected saturation definitions
+ r, g, b = hex[:2], hex[2:4], hex[4:]
+ # Try to find matching saturation
+ for satur in saturations:
+ if all(x in satur['units'].values() for x in (r, g, b)):
+ # Take leaf-level values from the saturation
+ # definition for better code readbility
+ full = satur['units']['full']
+ empty = satur['units']['empty']
+ # Determine the color and perform the action
+ if (r, g, b) == (full, empty, empty): # Red
+ x -= satur['step']
+ elif (r, g, b) == (full, full, empty): # Yellow
+ y += satur['step']
+ elif (r, g, b) == (empty, full, empty): # Green
+ x += satur['step']
+ elif (r, g, b) == (empty, empty, full): # Blue
+ y -= satur['step']
+ else:
+ raise ValueError(f'Invalid color hex: {hex}')
+ break
+ # If there was no matching saturation, meaning no break,
+ # that indicates invalid input
+ else:
+ raise ValueError(f'Invalid saturation for: {hex}.')
+ return x, y

Георги обнови решението на 24.10.2022 15:27 (преди около 2 години)

def calculate_final_vector(init_vector, hexes):
"""Calculate final vector based on initial vector and list of hexes."""
# Unpack the input tuple so that its coordinates are mutable objects
x, y = init_vector
# Define light/dark saturation with their corresponding steps and
# full/empty values for color definition
saturations = (
{
'units': {
'full': 'ff',
'empty': 'c0'
},
'step': -1,
},
{
'units': {
'full': 'c0',
'empty': '00'
},
'step': +1,
}
)
# Map the hex list to lower strings and process each hex one by one
for hex in map(str.lower, hexes):
# Determine hex type
- if hex == 'ffffff':
- # White colors means no instruction - skip the current hex
- continue
- elif hex == '000000':
- # Black colors means to halt - stop the loop
- break
- else:
- # Split color into RGB components to ensure it fits into
- # one of the expected saturation definitions
- r, g, b = hex[:2], hex[2:4], hex[4:]
- # Try to find matching saturation
- for satur in saturations:
- if all(x in satur['units'].values() for x in (r, g, b)):
- # Take leaf-level values from the saturation
- # definition for better code readbility
- full = satur['units']['full']
- empty = satur['units']['empty']
- # Determine the color and perform the action
- if (r, g, b) == (full, empty, empty): # Red
- x -= satur['step']
- elif (r, g, b) == (full, full, empty): # Yellow
- y += satur['step']
- elif (r, g, b) == (empty, full, empty): # Green
- x += satur['step']
- elif (r, g, b) == (empty, empty, full): # Blue
- y -= satur['step']
- else:
- raise ValueError(f'Invalid color hex: {hex}')
- break
- # If there was no matching saturation, meaning no break,
- # that indicates invalid input
- else:
- raise ValueError(f'Invalid saturation for: {hex}.')
+ match hex:
+ case 'ffffff':
+ # White colors means no instruction - skip the current hex
+ continue
+ case '000000':
+ # Black colors means to halt - stop the loop
+ break
+ case _:
+ # Split color into RGB components to ensure it fits into
+ # one of the expected saturation definitions
+ r, g, b = hex[:2], hex[2:4], hex[4:]
+ # Try to find matching saturation
+ for satur in saturations:
+ if all(x in satur['units'].values() for x in (r, g, b)):
+ # Take leaf-level values from the saturation
+ # definition for better code readbility
+ full = satur['units']['full']
+ empty = satur['units']['empty']
+ # Determine the color and perform the action
+ if (r, g, b) == (full, empty, empty): # Red
+ x -= satur['step']
+ elif (r, g, b) == (full, full, empty): # Yellow
+ y += satur['step']
+ elif (r, g, b) == (empty, full, empty): # Green
+ x += satur['step']
+ elif (r, g, b) == (empty, empty, full): # Blue
+ y -= satur['step']
+ else:
+ raise ValueError(f'Invalid color hex: {hex}')
+ break
+ # If there was no matching saturation, meaning no break,
+ # that indicates invalid input
+ else:
+ raise ValueError(f'Invalid saturation for: {hex}.')
return x, y
+
+
+print(calculate_final_vector((0, 0), ['ffc0c0', 'c0ffc0', 'C0C000', 'c0ffc0', 'ffffff', '000000', 'ffc0c0', 'c0ffc0']))

Георги обнови решението на 24.10.2022 15:27 (преди около 2 години)

def calculate_final_vector(init_vector, hexes):
"""Calculate final vector based on initial vector and list of hexes."""
# Unpack the input tuple so that its coordinates are mutable objects
x, y = init_vector
# Define light/dark saturation with their corresponding steps and
# full/empty values for color definition
saturations = (
{
'units': {
'full': 'ff',
'empty': 'c0'
},
'step': -1,
},
{
'units': {
'full': 'c0',
'empty': '00'
},
'step': +1,
}
)
# Map the hex list to lower strings and process each hex one by one
for hex in map(str.lower, hexes):
# Determine hex type
match hex:
case 'ffffff':
# White colors means no instruction - skip the current hex
continue
case '000000':
# Black colors means to halt - stop the loop
break
case _:
# Split color into RGB components to ensure it fits into
# one of the expected saturation definitions
r, g, b = hex[:2], hex[2:4], hex[4:]
# Try to find matching saturation
for satur in saturations:
if all(x in satur['units'].values() for x in (r, g, b)):
# Take leaf-level values from the saturation
# definition for better code readbility
full = satur['units']['full']
empty = satur['units']['empty']
# Determine the color and perform the action
if (r, g, b) == (full, empty, empty): # Red
x -= satur['step']
elif (r, g, b) == (full, full, empty): # Yellow
y += satur['step']
elif (r, g, b) == (empty, full, empty): # Green
x += satur['step']
elif (r, g, b) == (empty, empty, full): # Blue
y -= satur['step']
else:
raise ValueError(f'Invalid color hex: {hex}')
break
# If there was no matching saturation, meaning no break,
# that indicates invalid input
else:
raise ValueError(f'Invalid saturation for: {hex}.')
return x, y
-
-
-print(calculate_final_vector((0, 0), ['ffc0c0', 'c0ffc0', 'C0C000', 'c0ffc0', 'ffffff', '000000', 'ffc0c0', 'c0ffc0']))