Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """Simulation."""
- from typing import Tuple
- def simulate(world_map: list, flight_plan: list) -> list:
- """
- Simulate a flying space ship fighting space pirates.
- :param world_map: A list of strings indicating rows that make up the space map.
- The space map is always rectangular and the minimum given size is 1x1.
- Space pirate free zone is indicated by the symbol ('-'), low presence by ('w') and high presence by ('W').
- The ship position is indicated by the symbol ('X'). There is always one ship on the space map.
- Asteroid fields are indicated by the symbol ('#').
- :param flight_plan: A list of moves.
- The moves are abbreviated N - north, E - east, S - south, W - west.
- Ignore moves that would put the ship out of bounds or crash it into an asteroid field.
- :return: A list of strings indicating rows that make up the space map. Same format as the given wmap.
- Pirates under Shippy's starting position are always eliminated ('-').
- If Shippy fights pirates in high presence area, it first turns into low presence ('w')
- and then from low presence into no presence area ('-').
- """
- row_num = 0
- world_map_dict = {}
- coast_dict = {}
- coast_dict["S"] = (1, 0)
- coast_dict["N"] = (-1, 0)
- coast_dict["E"] = (0, 1)
- coast_dict["W"] = (0, -1)
- s_ship_loc = ()
- for row in world_map:
- map_width = len(row)
- map_height = len(world_map)
- elem_num = 0
- for element in row:
- world_map_dict[(row_num, elem_num)] = element
- if element == "X":
- s_ship_loc = (row_num, elem_num)
- elem_num += 1
- row_num += 1
- move_num = 0
- for move in flight_plan:
- unableToMove = False
- loc_after_move = (s_ship_loc[0] + coast_dict[move][0], s_ship_loc[1] + coast_dict[move][1])
- # Kas tahab kaardi pikkusest välja minna?
- if loc_after_move[0] > map_height or loc_after_move[0] < 0:
- unableToMove = True
- # Kas tahab kaardi laiusest välja minna?
- if loc_after_move[1] > map_width or loc_after_move[1] < 0:
- unableToMove = True
- # Kas tahab asteroidile otsa sõita?
- if world_map_dict[loc_after_move] == "#":
- unableToMove = True
- # Out of bounds
- if world_map_dict[loc_after_move] not in world_map_dict.values():
- unableToMove = True
- move_num += 1
- if not unableToMove:
- if move_num == 1:
- world_map_dict[s_ship_loc] = "-"
- world_map_dict[loc_after_move] = "-"
- s_ship_loc = loc_after_move
- elif move_num > 1:
- s_ship_loc = loc_after_move
- if world_map_dict[loc_after_move] == "W":
- world_map_dict[loc_after_move] = "w"
- else:
- world_map_dict[loc_after_move] = "-"
- if unableToMove:
- world_map_dict[s_ship_loc] = "X"
- else:
- world_map_dict[loc_after_move] = "X"
- world_map_values = list(world_map_dict.values())
- c = 0
- final_list = []
- for x in world_map:
- c += map_width
- a = "".join(world_map_values[c-map_width:c])
- final_list.append(a)
- return final_list
- def list_to_dictionary_converter(world_map: list) -> Tuple[dict, int, int]:
- """
- Convert a list to dictionary using coordinates as keys.
- :param world_map: list of strings.
- :return: dictionary of the space, shippy y position, shippy x position
- Map tile under Shippy's location is marked as "-" or no presence area.
- Dictionaries key is a Tuple which has Y-position as the first value and X-position as
- the second value. If there is no Shippy (Marked as X in the list) in the list, the
- coordinates are marked as 0 and 0.
- """
- row = 0
- n_dict = {}
- s_loc = (0, 0)
- for x in world_map:
- ix = 0
- for char in x:
- n_dict[row, ix] = char
- if char == "X":
- s_loc = (row, ix)
- n_dict[row, ix] = "-"
- ix += 1
- row += 1
- dict_tuple = (n_dict, s_loc[0], s_loc[1])
- return dict_tuple
- def dictionary_to_list_converter(space_map: dict, width: int, height: int) -> list:
- """
- Convert dictionary of coordinates to list of strings.
- :param space_map: Dictionary of the space
- :param width: Width of the world.
- :param height: Height of the world.
- :return: List of strings
- PS: You should add Shippy back the the dictionary before you call this method.
- """
- pass
- if __name__ == '__main__':
- space_list1 = [
- "#www-",
- "wXw#-",
- ]
- flight_plan1 = ["N", "E", "E", "S", "E"]
- print("\n".join(simulate(space_list1, flight_plan1)))
- print(list_to_dictionary_converter(flight_plan1))
- # #---X
- # w-w#-
- assert simulate(space_list1, flight_plan1) == ["#---X", "w-w#-"]
- print()
- space_list2 = [
- "WWWW",
- "-wwW",
- "X-#W",
- ]
- flight_plan2 = ["N", "N", "E", "E", "S", "W", "W", "S", "E", "E"]
- print("\n".join(simulate(space_list2, flight_plan2)))
- # wwwW
- # ---W
- # -X#W
- assert simulate(space_list2, flight_plan2) == ["wwwW", "---W", "-X#W"]
- assert list_to_dictionary_converter(["-"]) == ({(0, 0): "-"}, 0, 0)
- assert list_to_dictionary_converter(['W#', '-X']) == ({(0, 0): 'W', (0, 1): '#', (1, 0): '-', (1, 1): '-'}, 1, 1)
- assert list_to_dictionary_converter(
- world_map=space_list1
- ) == ({(0, 0): '#', (0, 1): 'w', (0, 2): 'w', (0, 3): 'w', (0, 4): '-', (1, 0): 'w', (1, 1): '-', (1, 2): 'w',
- (1, 3): '#', (1, 4): '-'}, 1, 1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement