Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm given this line of code in particular:

C#
public void Test8(List<int> items, int places)


The tasks for this problem is to come up with solution where I am able to rotate the List to the right by the specified number of places. A List rotation can be seen as circular, which means the elements that fall of the end would wrap around to the beginning, and vice versa.

How can I do this the correct way? Is there multiple ways to do this manually without using any sort fancy code?

Example:
Initial Array or List: 20, 30, 40, 50,60, 70
Rotated to the right by 3 places: 50, 60, 70, 20, 30, 40.


What I have tried:

C#
public void Test8(List<int> items, int places)
{
    int rotate = places;
    int[] results = new int[items.Count];

    for (int i = 0; i < items.Count; i++)
    {
        results[i] = items[(i + rotate) % items.Count];
    }
}
Posted
Updated 19-Jul-16 4:13am
v2

Try this:

C#
public void Test8(List<int> items, int places)
{ 
    int nItem;

    for (int i = 0; i < places; i++)
    {
        nItem = items[items.Count - 1];     // get the last item of the list
        items.RemoveItem(items.Count - 1);  // remove this item from the end  ...
        items.InsertItem(0, nItem);         // ... and insert this (last) item in front of the list
    }
}
 
Share this answer
 
v4
Comments
Member 12642406 19-Jul-16 6:15am    
I added the code you suggested, but the inputs are inconsistent all around which makes the test to fail in all 5 runs. Look at the screenshot I took. -> http://imgur.com/3qTOxch
Member 12642406 19-Jul-16 6:17am    
Never mind, I got it too work. Was writing something wrong.
Richard Deeming 19-Jul-16 10:05am    
That will work, but it won't be very efficient. :)

Each time you InsertItem(0, ...), every other item in the list has to be moved up one place.

I'd be inclined to use CopyTo, RemoveRange and InsertRange.
You have basically 2 techniques
- Rotation with copy to a new list
- Rotation in place

Rotation with copy is the technique of solution 1.
To calc to new positionof an element, you have to apply the formula:
NewPosition= (OldPosition+ Rotation) % SizeOfList

Rotation in place
Only rotation of 1 and -1 are practical. You just have to repeat as many times as needed.
- pseudo code
C++
if (rotation== -1)
{
    tmp= list[0]
    for scan= 1; scan < list.count; scan++)
    {
        list[scan-1]= list[scan];
    }
    list[list.count-1]= tmp;
}
else
{
    tmp= list[list.count-1]
    for scan= list.count-1; scan >=0; scan--)
    {
        list[scan]= list[scan-1];
    }
    list[0]= tmp;
}
 
Share this answer
 
Comments
[no name] 19-Jul-16 7:59am    
Only a mind game:
There is a third option, "virtualRotation". I mean something like
listIndex= (userIndex + rotationValue) % list.Count;
The only problem I have, how do I Need to adapt IEnumerator that the list remains a list?
Patrice T 19-Jul-16 10:53am    
Sure
A slightly more efficient version of Solution 1:
C#
public void Test8(List<int> items, int places)
{
    // Handle cases where the number of places is greater than or equal to the number of items:
    places %= items.Count;
    if (places == 0) return;
    
    int[] range = new int[places];
    items.CopyTo(items.Count - places, range, 0, places);
    items.RemoveRange(items.Count - places, places);
    items.InsertRange(0, range);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900