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;
}
}
@meekg33k
Copy link

Hello @solutionCraftsman, this seems like your first time of participating in Week 6 of #AlgorithmFridays. Thanks for being a part of it.

This is a good attempt at coming up with a solution for Apex College but the one problem is that your shufflePupils method has return type void, which causes it to fail the test cases. Your solution was meant to return the shuffled list of pupils, so it should have been something like public static int[] shufflePupils(int[] pupils, int numberToMove).

Is there a chance you can make the changes so I can re-run the tests?

@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