« index coverage.py v6.0b1, - created at 2021-08-27 11:47 +0200 + created at 2021-08-27 15:04 +0200
diff --git a/.coverage b/.coverage index 6281315..465fc78 100644 Binary files a/.coverage and b/.coverage differ diff --git a/htmlcov/d_a44f0ac069e85531_test_rover_py.html b/htmlcov/d_a44f0ac069e85531_test_rover_py.html index d1e8c71..75e72f2 100644 --- a/htmlcov/d_a44f0ac069e85531_test_rover_py.html +++ b/htmlcov/d_a44f0ac069e85531_test_rover_py.html @@ -134,7 +134,7 @@
« index coverage.py v6.0b1, - created at 2021-08-27 11:47 +0200 + created at 2021-08-27 15:04 +0200
1import dataclasses
- +1from dataclasses import dataclass, field
+2from typing import Tuple
-4@dataclasses.dataclass
-5class Rover:
-6 x: int
-7 y: int
-8 direction: str
-9 obstacles: list[list[int]] = dataclasses.field(default_factory=list)
- -11 valid_commands = ("F", "B", "R", "L")
+ +5@dataclass(frozen=True)
+6class Coordinates:
+7 x: int
+8 y: int
+9 left: str
+10 right: str
+ -13 direction_map = {
-14 "NORTH": (0, 1, "WEST", "EAST"),
-15 "EAST": (1, 0, "NORTH", "SOUTH"),
-16 "SOUTH": (0, -1, "EAST", "WEST"),
-17 "WEST": (-1, 0, "SOUTH", "NORTH"),
-18 }
+13@dataclass
+14class Rover:
+15 x: int
+16 y: int
+17 direction: str
+18 obstacles: list[list[int]] = field(default_factory=list)
-20 def is_valid_command(self, command):
-21 for ch in command:
-22 if ch not in self.valid_commands:
-23 return False
- -25 return True
- -27 def is_obstacle(self, x: int, y: int):
-28 return [x, y] in self.obstacles
- -30 def move(self, command: str):
-31 if not self.is_valid_command(command):
-32 raise ValueError("invalid command. The rover does not move")
+20 valid_commands = ("F", "B", "R", "L")
+ +22 direction_map = {
+23 "NORTH": Coordinates(0, 1, "WEST", "EAST"),
+24 "EAST": Coordinates(1, 0, "NORTH", "SOUTH"),
+25 "SOUTH": Coordinates(0, -1, "EAST", "WEST"),
+26 "WEST": Coordinates(-1, 0, "SOUTH", "NORTH"),
+27 }
+ +29 def is_valid_command(self, command: str) -> bool:
+30 for ch in command:
+31 if ch not in self.valid_commands:
+32 return False
-34 for ch in command:
-35 x, y, direction = self._compute_new_coordinates(ch)
-36 if obstacle := self.is_obstacle(x, y):
-37 return self.new_coordinates_output(obstacle)
+34 return True
+ +36 def is_obstacle(self, x: int, y: int) -> bool:
+37 return [x, y] in self.obstacles
-39 self.apply_new_coordinates(x, y, direction)
- -41 return self.new_coordinates_output()
+39 def move(self, command: str):
+40 if not self.is_valid_command(command):
+41 raise ValueError("invalid command. The rover does not move")
-43 def new_coordinates_output(self, obstacle: bool = False):
-44 if obstacle:
-45 return self.x, self.y, self.direction, "STOPPED"
-46 return self.x, self.y, self.direction
+43 for ch in command:
+44 x, y, direction = self._compute_new_coordinates(ch)
+45 if obstacle := self.is_obstacle(x, y):
+46 return self._new_coordinates_output(obstacle)
-48 def _compute_new_coordinates(self, command):
-49 x = self.x
-50 y = self.y
-51 direction = self.direction
- -53 if command == "B":
-54 x -= self.direction_map[self.direction][0]
-55 y -= self.direction_map[self.direction][1]
-56 if command == "F":
-57 x += self.direction_map[self.direction][0]
-58 y += self.direction_map[self.direction][1]
-59 if command == "R":
-60 direction = self.direction_map[self.direction][3]
-61 if command == "L":
-62 direction = self.direction_map[self.direction][2]
- -64 return x, y, direction
- -66 def apply_new_coordinates(self, x, y, direction):
-67 self.x = x
-68 self.y = y
-69 self.direction = direction
+48 self._apply_new_coordinates(x, y, direction)
+ +50 return self._new_coordinates_output()
+ +52 def _new_coordinates_output(self, obstacle: bool = False):
+53 if obstacle:
+54 return self.x, self.y, self.direction, "STOPPED"
+55 return self.x, self.y, self.direction
+ +57 def _compute_new_coordinates(self, command) -> Tuple[int, int, str]:
+58 x = self.x
+59 y = self.y
+60 direction = self.direction
+ +62 if command == "B":
+63 x -= self.direction_map[self.direction].x
+64 y -= self.direction_map[self.direction].y
+65 if command == "F":
+66 x += self.direction_map[self.direction].x
+67 y += self.direction_map[self.direction].y
+68 if command == "R":
+69 direction = self.direction_map[self.direction].right
+70 if command == "L":
+71 direction = self.direction_map[self.direction].left
+ +73 return x, y, direction
+ +75 def _apply_new_coordinates(self, x, y, direction) -> None:
+76 self.x = x
+77 self.y = y
+78 self.direction = direction
coverage.py v6.0b1, - created at 2021-08-27 11:47 +0200 + created at 2021-08-27 15:09 +0200