Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Example:
if \$N=2\$, given the input array \${[1, 2, 3, 4, 5, 6]}\$ the function should return \${[5, 6, 1, 2, 3, 4]}\$

def copyDigit(list,index,item):

        """
                Copy the item to the indexed location in the given list
        """
        list[index] = item
        return list

def rotateList(noListToRorate,timesRotate):

        """
                Rotate List to right    
        """
        print "Function received {0},{1}".format(noListToRorate,timesRotate)
        for rotate in range(N):
            lastItemInList = noListToRorate[-1]
            for i in range(len(noListToRorate)-2,-1,-1):
                itemToShift = noListToRorate[i]
                noListToRorate = copyDigit(noListToRorate,i+1,itemToShift)
            noListToRorate[0] = lastItemInList
        print "Rotate once: {0}".format(noListToRorate)
        return noListToRorate


if __name__ == '__main__':

        """
           This program will rorate right the given list N no of times  
        """

        arrayList = [1,2,3,4,5,6]
        N = 2
        print "Rotate an array: " ,(arrayList), "No of times: ", N
        finalList = rotateList(arrayList,N)
        print "Rotated List: ", finalList
share|improve this question

2 Answers 2

The obvious solution

Not sure if you know the obvious solution and just want to re-invent the wheel or if you are just not aware of this but you could just do :

>>> l = [1, 2, 3, 4, 5, 6]
>>> n = 2
>>> l[-n:] + l[:-n]
[5, 6, 1, 2, 3, 4]

The actual review : details

You have splitted your code into multiple functions, documented them, written the usual if __name-- == 'main': test : this is already a pretty good beginning.

Also, pep8 only complies about some whitespaces not following the Python Style Guide (this is worth fixing it anyway).

However, here a few things that do not look good in your code and could easily be fixed :

  • your code can be python 3 compliant by adding parenthesis in your calls to print.
  • you have the same typo rorate in multiple places
  • list is a bad name for a list as you can easily mix-up with the type
  • your function and variable names does not follow the convention.

After fixing all this, you get something like :

def copy_digit(lst, index, item):
    """
            Copy the item to the indexed location in the given list
    """
    lst[index] = item
    return lst


def rotate_list(lst, nb_rotate):
    """
            Rotate List to right
    """
    print("Function received {0},{1}".format(lst, nb_rotate))
    for rotate in range(N):
        last_item = lst[-1]
        for i in range(len(lst) - 2, -1, -1):
            item_to_shift = lst[i]
            lst = copy_digit(lst, i + 1, item_to_shift)
        lst[0] = last_item
    print("Rotate once: {0}".format(lst))
    return lst


if __name__ == '__main__':
    """
       This program will rotate right the given list N no of times
    """

    array = [1, 2, 3, 4, 5, 6]
    N = 2
    print("Rotate an array: ", array, "No of times: ", N)
    final_list = rotate_list(array, N)
    print("Rotated List: ", final_list)

A subtle bug

What if I was to call :

final_list = rotate_list(array, 2)

? Surprisingly enough, I'd get an error. I'll let you have a look at this because it is not very complicated.

Clearer interface for your functions

Your function copy_digit updates the list and returns it. There is no real need for this as the caller would already have the initial list. Then you can just have :

def copy_digit(lst, index, item):
    """
            Copy the item to the indexed location in the given list
    """
    lst[index] = item

and

...
copy_digit(lst, i + 1, item_to_shift)
...

Of course, the need for a function seems a bit doubtful here. Let's inline this :

...
lst[i + 1] = item_to_shift
...

Then, it seems like the item_to_shift variable is not really required anymore :

for i in range(len(lst) - 2, -1, -1):
    lst[i + 1] = lst[i]

And more generally

In Programming Pearls (sorry I couldn't find the relevant full text), Jon Bentley describes different solutions to the problem. Some behind quite original and actually very easy to implement on top of being efficient. I'll let you google "programming pearls rotate" if you want to read more about this. It is defintly worth a read (and the whole book is too).

share|improve this answer
    
Thanks a ton for a detailed review. I am learning on so not that great in naming the variables and functions according to the standards. Your link to PEP8 is definitely useful. Yes, I am aware of the slicing concept to solve this, which is one liner but just wanted to write another program. Could I install PEP8 in windows too ? –  python_lover 20 hours ago
    
Subtle error do you mean you mean the handling of conditions N=0 and N > array length, which is missing. –  python_lover 20 hours ago
    
I meant : removing the declaration of n and passing the value directly to the function. –  Josay 19 hours ago
    
I am not getting an error in my existing code even when passing 2 as the argument to the function. ?? –  python_lover 9 hours ago
    
Unable to install pip in my windows with python3.0, any help? I have chocolatey but neither able to install pip nor easy.install. Any help?? –  python_lover 9 hours ago

Along the lines of Josay, could also do this:

def rotate(lst, n):
    if n == 0 or n > len(lst):
        return lst

    n = -abs(n) # Get the negative of the absolute of n
    lst_n = lst[n:] # Get the last n elements of lst
    for i in range(n, 0): 
        del lst[i] # Remove the last n elements of lst
    return lst_n + lst # Return the merged list
share|improve this answer
    
That's short. Great peace of code. Thanks for sharing this. –  python_lover 20 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.