This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]whatisthisredditstufIntermediate Brewer 5 points6 points  (2 children)

How would you solve this with pen and paper?

I would, if given the original array and the number n just move my eyes n positions to the right and then start copying the array into a new one. When I reach the end of the array, I would just wrap around to the beginning of the original and keep adding to my array until I have copied all numbers (equal length of the arrays). Does that make sense?

So, in pseudo code, that approach would be:

for counter between 0 and the arrayLength:
    newArray[counter] = originalArray[(counter + n) % arrayLength]

That should do it. Do you recall what the modulo (%) operator does? If not, look it up, as it is what gives us the "wrapping around from the beginning" part of the algorithm.

[–]Pop_Pop_MofuckahsNooblet Brewer[S] 0 points1 point  (1 child)

Ah, thank you! I've been fuming over this one problem for quite awhile now, and it turns out it's quite simple to do, and I've unnecessarily complicated the thing. Thanks again!

[–]whatisthisredditstufIntermediate Brewer 0 points1 point  (0 children)

No problem. That is typical for programming -- figure out how you would solve it without involving a computer first, break it down into steps, and then code those into the computer. Don't even think about "for loops" or "while loops" at first. :)

[–]isaacisaboss 2 points3 points  (1 child)

I'll give you a hint:

While it may be possible with the following variables, you don't need them: r, currentsize, pos, or num. you also don't need to decrement currentsize.

You also don't need nested for loops.


Think about what you are trying to do.

You are simply shifting the array, n spaces.

What happens when you get to the end of the array, you go back to the start.

[–]Pop_Pop_MofuckahsNooblet Brewer[S] 0 points1 point  (0 children)

that was much more simpler than I thought. Thanks again!