[ create a new paste ] login | about

Link: http://codepad.org/nKk5LOLl    [ raw code | fork ]

Python, pasted on Oct 17:
def reverse_array(A, b, e):
    #reverse the array A inplace from b to e inclusive
    while b < e:
        A[b],A[e] = A[e],A[b];
        b += 1;
        e -= 1;
    
def rotate_array(A, n):
    m = len(A);
    rot_right = False;
    if n < 0:
        rot_right = True;

    n = abs(n) % m;
    if rot_right:
        reverse_array(A, m-n, m-1  );
        reverse_array(A, 0  , m-n-1);
    else:
        reverse_array(A, 0, n-1);
        reverse_array(A, n, m-1);

    reverse_array(A, 0, m-1);

 


Output:
No errors or program output.


Create a new paste based on this one


Comments: