I've the following five arrays
var E1 = ["A", "B", "C", "D", "E"]
var E2 = ["A", "B", "C", "D", "E"]
var E3 = ["A", "B", "C", "D", "E"]
var E4 = ["A", "B", "C", "D", "E"]
var E5 = ["A", "B", "C", "D", "E"]
Each array have the same five elements namely "A", "B", "C", "D" and "E". I want to write an algorithm to sort the elements in all the arrays such that no two arrays have the same element (let's say "A") at the same index.
A sort of the sample output that will work for me will be like:
var E1 = ["A", "B", "C", "D", "E"]
var E2 = ["B", "C", "D", "E", "A"]
var E3 = ["C", "D", "E", "A", "B"]
var E4 = ["D", "E", "A", "B", "C"]
var E5 = ["E", "A", "B", "C", "D"]
I've tried to solve this but couldn't complete. I've just written a shuffling function for sorting the elements of two arrays(E1 and E2).
var E1 = ["A", "B", "C", "D", "E"]
var E2 = ["A", "B", "C", "D", "E"]
var E3 = ["A", "B", "C", "D", "E"]
var E4 = ["A", "B", "C", "D", "E"]
var E5 = ["A", "B", "C", "D", "E"]
func shuffledArrays(var array1: [String],var array2: [String]) {
if array1[0] == array2[0] || array1[1] == array2[1] || array1[2] == array2[2] || array1[3] == array2[3] || array1[4] == array2[4] {
shuffled1 = GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(array1)
shuffled2 = GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(array2)
var array3 = shuffled1 as! [String]
var array4 = shuffled2 as! [String]
} else {
var array3 = array1
var array4 = array2
}
array1 = array3
array2 = array4
}
// Now calling the function on arrays E1 and E2
shuffledArrays(E1, array2: E2)
print(E1)
print(E2)
With this code I'm getting the following error on Xcode Playground. While sometimes the error is removed and the output is correct at lines 102 and 103 but still I'm unable to extract that output out and save it permanently into E1 and E2 respectively. Please help me with the whole algorithm in arranging the five arrays' elements.
Thanks