C# code
I want to shift my elements in an int array to the left by one.
Scenario:
[1,3,4,5] → [3,4,5,1]
[1,0,0,0] → [0,0,0,1]
etc…
Is there any built-in/algorithm which does this?
*EXTRA
If I want to show the shifted array on my UI with all the leading zeroes removed but still remember the original array so when i shift it again it will remember the amount of zeroes., how would one implement that?
I think this would be a pretty good place for an extension method if he just wants to return an array that is shifted. Should be easy enough to do with some simple for loop iteration as you said.
Also, for the UI thing, this method should return a string that follows the correct format. Be aware that this is not an extension and you will need to call this method from the containing class or a reference thereto
public static int[] Shift(this int[] myArray)
{
int[] tArray = new int[myArray.Length];
int v = myArray[0];
Array.Copy(myArray, 1, tArray, 0, myArray.Length - 1);
tArray[tArray.Length - 1] = v;
return tArray;
}
Another option is to use a Queue or LinkedList both let you easily remove an item at the front and add it to the back.
Queue<int> q = new Queue<int>( ... );
int v = q.Dequeue();
q.Enqueue(v);
// ------
LinkedList<int> l = new LinkedList<int>( ... );
int v = l.First();
l.RemoveFirst();
l.AddLast(v);
Thanks for the tip. I’m going to run some testing on each of these methods and drill down which one is actually the fastest. Could be very helpful in optimization situations!
If you are merely shifting data around like in your example, and not actually adding and removing data then I would simply shift an offset that points to the data. The data doesn’t need to change, only the way you are looking at it.
There’s more than one way to skin a cat I suppose. I personally would not shift the data in the array. I would probably just use an offset as @Hikiko66 stated; I’m just here to answer questions. If someone asks what is possible and I can show them how it can be done I will do just that. I can’t just assume that I know everything they are trying to do and there are probably situations where you may actually want to shift data; though this probably isn’t one of those cases.
My first thought was simply to set up a custom data class. Use an array or list as the underlying type, and provide two different accessors, one that returns the normal data, and one that returns the shifted data.
But there might be a reason to want a copy instead of just faking it this way.
I’ve written a few classes that test how long it takes to complete a method or section of code and log it to the debug console if anyone is interested. Apparently my laptop takes ~420ms to populate an int[5000000] array with random numbers.
I can post these if anyone is interested in using them
It occurs to me that this is thread is quickly drifting away from the OP and becoming a discussion on code optimization. I’ve done some testing and I am going to post my findings and methodology in a new thread. I’ll drop a link in here when I’ve finished. Cheers!
im spawning random integer values that the player should use to hit a certain number with.
The problem i’m addressing should be used when right clicking ANY value in the game
I think the queue option is pretty nice. How efficient is it?
Shifting of that data is VERY efficient. My findings averaged less than 1ms; however, it is not the most quickly populated. I’m working on a thread right now that gives specifics, methodology, and even the source code I used to test this. As I said, I’ll link back here once I’ve finished the thread, so be on the lookout! In the mean time, feel free to use the queue. It’s definitely a viable option.
How many numbers will there be? I’m guessing whichever of the methods above would be efficient enough, and you should choose the one that makes your code the most readable.
You can do the queue even without a temporary variable q.Enqueue(q.Dequeue())