What Is Rotating a List in Computer Programming?

Problem scenario
You have read about lists or arrays being rotated. What does this mean in computer programming?

Lists and arrays can be interchangeable in many contexts. Java supports arrays and Python supports lists. Both are zero-based. That is, the first item in each is indexed as 0. Lists in Python are circular: the last item in the list can be referred to as -1 and the penultimate item can be referred to as index -2.

Rotating a list is keeping the contents the same but changing the index of each item by the same amount. For example, if you rotated an index by two, the second to last item in the list would be the first item in the list.

Here is an example of a three-element list: ("red", "white", "blue")

If we rotated this list by two, the new list would be like this: ("blue", "white", "red")

Lists are often zero-based which means that the first element is indexed with a zero. Rotating a list or array has nothing to do with presenting it vertically or horizontally.

For further information, see these specific links: geeksforgeeks.com and Quora

Leave a comment

Your email address will not be published. Required fields are marked *