Skip to content

Instantly share code, notes, and snippets.

@otunbaCrafts
Last active May 17, 2021 08:26
Show Gist options
  • Select an option

  • Save otunbaCrafts/f4413654ec019c5014f6c903f77b2bf5 to your computer and use it in GitHub Desktop.

Select an option

Save otunbaCrafts/f4413654ec019c5014f6c903f77b2bf5 to your computer and use it in GitHub Desktop.
Some help for Apex College. A logic implemented in Java to shuffle pupils on an assembly line by moving a number of pupils either to the front of the line or to the end of the line.
public class ShuffleClass {
public static int[] shufflePupils(int[] pupils, int numberToMove)
{
if(pupils == null) {
throw new IllegalArgumentException("List of pupils is null");
}
if(Math.abs(numberToMove) > pupils.length) {
numberToMove %= pupils.length;
}
if(pupils.length == 0 || numberToMove == 0) {
return pupils;
}
if(Math.abs(numberToMove) == pupils.length) {
return pupils;
}
int temp;
int nextPupilToMove;
int nextPupilToShift;
int indexOfNextToMove;
int indexOfLastPupil = pupils.length - 1;
if(numberToMove > 0)
{
int indexOfFirstToMove = ( indexOfLastPupil ) - ( numberToMove - 1 );
for (int move = 0; move < numberToMove; move++) {
indexOfNextToMove = indexOfFirstToMove + move;
nextPupilToMove = pupils[indexOfNextToMove];
temp = pupils[move];
for (int index = move; index < indexOfNextToMove; index++) {
nextPupilToShift = temp;
temp = pupils[index + 1];
pupils[index + 1] = nextPupilToShift;
}
pupils[move] = nextPupilToMove;
}
}
else
{
indexOfNextToMove = 0;
numberToMove = Math.abs(numberToMove);
for (int move = 0; move < numberToMove; move++) {
nextPupilToMove = pupils[indexOfNextToMove];
for (int index = 1; index <= indexOfLastPupil; index++) {
pupils[index - 1] = pupils[index];
}
pupils[indexOfLastPupil] = nextPupilToMove;
}
}
return pupils;
}
}
@otunbaCrafts
Copy link
Author

Hi @meekg33k, yes indeed it is. Thank you for the kind words. I have refactored as directed.
Thank you for the opportunity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment