Created
May 15, 2021 11:37
-
-
Save Ezeji/0ddd36d25d25e13dab75d8c794b2afc7 to your computer and use it in GitHub Desktop.
This algorithm 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
private static List<int> Result { get; set; } = new List<int>(); | |
static void Main(string[] args) | |
{ | |
int[] nums = { 4, 1, 3 }; | |
int val = -1; | |
ShufflePupils(nums, val); | |
PrintArray(Result); | |
} | |
public static void SplitFirstSetFromArrayIntoNewArray(int[] nums, int val) | |
{ | |
for (int i = 0; i < nums.Length; i++) | |
{ | |
if (i >= Math.Abs(val)) | |
{ | |
Result.Add(nums[i]); | |
} | |
} | |
} | |
public static void SplitSecondSetFromArrayIntoNewArray(int[] nums, int val) | |
{ | |
for (int i = 0; i < nums.Length; i++) | |
{ | |
if (i < Math.Abs(val)) | |
{ | |
Result.Add(nums[i]); | |
} | |
} | |
} | |
public static void ShufflePupils(int[] nums, int val) | |
{ | |
if (nums == null) { } | |
else if (nums != null && val == 0) | |
{ | |
Result.AddRange(nums); | |
} | |
else | |
{ | |
SplitFirstSetFromArrayIntoNewArray(nums, val); | |
SplitSecondSetFromArrayIntoNewArray(nums, val); | |
} | |
} | |
public static void PrintArray(List<int> result) | |
{ | |
for (int i = 0; i < result.Count; i++) | |
{ | |
Console.Write(result[i] + " "); | |
} | |
Console.WriteLine(""); | |
} | |
} |
Thank you so much @meekg33k for your detailed feedback, it means a lot to me and I appreciate.
All your points raised are noted with thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello @Ezeji, thank you for participating in Week 6 of #AlgorithmFridays.
This is a good attempt at coming up with a solution for Apex College, and your solution passes most of the test cases. There is one test case that it didn't seem to account for:
When
val
is greater than the size of thenums
array; in such cases, your solution returns thenums
. For example,ShufflePupils({2, 3}, 3); // yours would return {2, 3} instead of {3, 2}
. The expectation is that whenval
is greater than thenums
, you should think of it as shuffling the entire pupils list for as many times as is possible untilval
becomes less than the size ofnums
. In mathematical terms, that would beval = val % nums.length
.Also your solution should return the modified list not print it out. That would mean
ShufflePupils
should return anint[]
so the function signature should bepublic static int[] ShufflePupils(int[] nums, int val)
I understand part of this solution was made with some assumptions of how certain things should be implemented. I think it's always best to check in with your interviewer before making assumptions on how edge cases should be implemented. I wrote about that in this article, you might find it helpful.
Apart from that, this was a decent attempt. Please let me know your thoughts.