def test_reverse_range(self): solution = Solution() numbers = [1, 2, 3, 4, 5] solution.reverse_range(numbers, 1, 2) self.assertEqual(numbers, [1, 3, 2, 4, 5]) numbers = [1, 2, 3, 4, 5] solution.reverse_range(numbers, 0, 3) self.assertEqual(numbers, [4, 3, 2, 1, 5]) numbers = [1, 2, 3, 4, 5] solution.reverse_range(numbers, 0, len(numbers) - 1) self.assertEqual(numbers, [5, 4, 3, 2, 1])
def test_example_1(self): for solution in (Solution(), OfficialSolutionApproach4()): nums = [1, 2, 3, 4, 5, 6, 7] solution.rotate(nums=nums, k=3) assert nums == [ 5, 6, 7, 1, 2, 3, 4, ]
def test_rotate_swap_array(self): solution = Solution() numbers = [] solution.rotate_swap(numbers, 1) self.assertEqual(numbers, []) numbers = [2] solution.rotate_swap(numbers, 1) self.assertEqual(numbers, [2]) numbers = [1, 2] solution.rotate_swap(numbers, 0) self.assertEqual(numbers, [1, 2]) numbers = [1, 2] solution.rotate_swap(numbers, 1) self.assertEqual(numbers, [2, 1]) numbers = [1, 2] solution.rotate_swap(numbers, 2) self.assertEqual(numbers, [1, 2]) numbers = [1, 2, 3] solution.rotate_swap(numbers, 1) self.assertEqual(numbers, [3, 1, 2]) numbers = [1, 2, 3, 4, 5, 6, 7] solution.rotate_swap(numbers, 3) self.assertEqual(numbers, [5, 6, 7, 1, 2, 3, 4]) numbers = [-1, -100, 3, 99] solution.rotate_swap(numbers, 2) self.assertEqual(numbers, [3, 99, -1, -100]) numbers = [1, 2, 3] solution.rotate_swap(numbers, 6) self.assertEqual(numbers, [1, 2, 3]) numbers = [1, 2, 3] solution.rotate_swap(numbers, 1000) self.assertEqual(numbers, [3, 1, 2])
def test_example_4(self): for solution in (Solution(), OfficialSolutionApproach4()): nums = [1] solution.rotate(nums=nums, k=1) assert nums == nums
def test_example_2(self): for solution in (Solution(), OfficialSolutionApproach4()): nums = [-1, -100, 3, 99] solution.rotate(nums=nums, k=2) assert nums == [3, 99, -1, -100]