def test_str_position(capsys): print(Position(1, 1)) captured = capsys.readouterr() assert captured.out == "Position(x=1, y=1, direction=N)\n" print(repr(Position(1, 2))) captured = capsys.readouterr() assert captured.out == "Position(x=1, y=2, direction=N)\n"
def test_direction_assignment(): origin = Position(2, 3) assert origin.direction == 0 origin.direction = 'S' assert origin.direction == 180 assert origin.direction_to_string == 'S' origin.direction = -90 assert origin.direction == -90 assert origin.direction_to_string == 'W'
def state_simulator(self, left, right): # input sequence left_ratio = left / Robot.ANALOG_MAX right_ratio = right / Robot.ANALOG_MAX for step in range(len(left)): ratio_diff = right_ratio[step] - left_ratio[step] max_dist = Robot.MAX_CM_PER_MS * self._period avg_ratio = (left_ratio + right_ratio) / 2; dist = avg_ratio * max_dist; turn_radius = left_ratio[step] * Robot.AXLE_LENGTH / ratio_diff d_theta = ratio_diff * max_dist / Robot.AXLE_LENGTH # set robot as axis of rotation to find position of center if abs(turn_radius) < Sandbox.width / 2: rel_pos = Position( -(turn_radius + Robot.AXLE_LENGTH / 2), 0) center = Robot.transform(rel_pos, self._state) # travel along circumference of circle rel_pos = Position(self._state.x - center.x, self._state.y - center.y) d_state = State(center.x, center.y, d_theta) # straight path else: # set original state as axis of rotation rel_pos = Position(0, avg_dist) d_state = self._state # update state cur_pos = Robot.transform(rel_pos, d_state) self._prev._state = self._state self._state = State(cur_pos.x, cur_pos.y, self._state.theta + d_theta, avg_dist)
import logging from data import data from robot import Position, Rover, tokenize logging.basicConfig(level=logging.DEBUG) for x, y, direction, instruction in tokenize(data): origin = Position(x, y, direction) rover = Rover(origin) rover.process(instruction) rover.position_to_string()
def test_invalid_postion_addition(): pos_1 = Position(1, 1, 'S') pos_2 = Position(1, 1, 'E') with pytest.raises(InvalidDirection): pos_1 + pos_2
def test_valid_position(): origin = Position(1, 2, 'E') assert origin.x == 1 assert origin.y == 2 assert origin.direction == 90
def test_postion_addition(): pos_1 = Position(1, 1, 'S') pos_2 = Position(1, 1, 'S') addition = pos_1 + pos_2 assert addition == Position(2, 2, 'S')
def test_invalid_int_direction(): with pytest.raises(InvalidDirection): Position(1, 3, 110)
def test_invalid_str_direction(): with pytest.raises(InvalidDirection): Position(1, 1, 'Q')
def test_position_direction(): origin = Position(1, 2) assert origin.direction == 0 assert origin.direction_to_string == "N"
def test_invalid_position(): with pytest.raises(InvalidCoordinate): Position(1.1, 2, 'W')
def _load_elem(self, char, row, col): if self._char_classes[char] == Robot: elem = Free() self.robot = Robot(Position(self, row, col, elem)) return elem return self._char_classes[char]()
def _rover(): origin = Position(2, 3, 'E') return Rover(origin)