def test_invalid_range(self):
        input, expected, start, end = [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], 0, 10
        result = reverse_by_index(input, start, end)
        self.assertEqual(expected, result)

        input, expected, start, end = [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], -2, 2
        result = reverse_by_index(input, start, end)
        self.assertEqual(expected, result)

        input, expected, start, end = [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], 5, 0
        result = reverse_by_index(input, start, end)
        self.assertEqual(expected, result)
def rotate_using_reversing_array(elements, rotate_by):
    if not valid_inputs(elements, rotate_by):
        return elements
    size = len(elements)
    rotate_by = rotate_by % size
    reverse_by_index(elements, 0, rotate_by - 1)
    reverse_by_index(elements, rotate_by, size - 1)
    reverse_by_index(elements, 0, size - 1)
    return elements
示例#3
0
def right_rotation_using_reverse(elements, rotate_by):
    if not valid_inputs(elements, rotate_by):
        return elements
    size = len(elements)
    rotate_by = rotate_by % size

    reverse_by_index(elements, 0, size - rotate_by - 1)
    reverse_by_index(elements, size - rotate_by, size - 1)
    reverse_by_index(elements, 0, size - 1)
    print(elements)
    return elements
 def test_zero_range(self):
     input, expected, start, end = [1, 2, 3, 4, 5, 6], [1, 2, 3, 4, 5, 6], 0, 0
     result = reverse_by_index(input, start, end)
     self.assertEqual(expected, result)
 def test_valid_range(self):
     input, expected, start, end = [1, 2, 3, 4, 5, 6], [4,3,2,1,5,6], 0, 3
     result = reverse_by_index(input, start, end)
     self.assertEqual(expected, result)