- 2.5k
- 1
- 18
- 30
DIRECTION_VECTORS = {
'n': [0, 1],
's': [0, -1],
'e': [1, 0],
'w': [-1, 0]
}
def unit_vector_from_cardinal(cardinal: str) -> numpy.array:
if cardinal in DIRECTION_VECTORS.keys():
return numpy.array(DIRECTION_VECTORS[cardinal])
else:
raise ValueError
You could also use a dict that contains tuples instead of lists, and get the same result, as suggested in comments by Graipher , like so:
DIRECTION_VECTORS = {
'n': (0, 1),
's': (0, -1),
'e': (1, 0),
'w': (-1, 0)
}
DIRECTION_VECTORS = {
'n': [0, 1],
's': [0, -1],
'e': [1, 0],
'w': [-1, 0]
}
def unit_vector_from_cardinal(cardinal: str) -> numpy.array:
if cardinal in DIRECTION_VECTORS.keys():
return numpy.array(DIRECTION_VECTORS[cardinal])
else:
raise ValueError
DIRECTION_VECTORS = {
'n': [0, 1],
's': [0, -1],
'e': [1, 0],
'w': [-1, 0]
}
def unit_vector_from_cardinal(cardinal: str) -> numpy.array:
if cardinal in DIRECTION_VECTORS:
return numpy.array(DIRECTION_VECTORS[cardinal])
else:
raise ValueError
You could also use a dict that contains tuples instead of lists, and get the same result, as suggested in comments by Graipher , like so:
DIRECTION_VECTORS = {
'n': (0, 1),
's': (0, -1),
'e': (1, 0),
'w': (-1, 0)
}
get_initial_coordinates_list
may be a static method
A static method only relies on the arguments passed to it. Your get_initial_coordinates_list
function does not directly access any of the properties or variables assigned directly to the class itself (such as body_coordinates_list
), and only uses the arguments and non-class-specific methods/functions, so we can actually make it a static method, and not have to provide a self
argument (which means it doesn't need to have access to everything else in the class). At least, in its current form, it can be made into a static method:
@staticmethod
def get_initial_coordinates_list(x_pos: int, y_pos: int, facing: str) -> list:
...
Optional: Consider a better ValueError message
Optional: Consider a better ValueError message
get_initial_coordinates_list
may be a static method
A static method only relies on the arguments passed to it. Your get_initial_coordinates_list
function does not directly access any of the properties or variables assigned directly to the class itself (such as body_coordinates_list
), and only uses the arguments and non-class-specific methods/functions, so we can actually make it a static method, and not have to provide a self
argument (which means it doesn't need to have access to everything else in the class). At least, in its current form, it can be made into a static method:
@staticmethod
def get_initial_coordinates_list(x_pos: int, y_pos: int, facing: str) -> list:
...
Optional: Consider a better ValueError message
- 2.5k
- 1
- 18
- 30