We need an algorithm to shuffle elements from 5 arrays, each of which has the same 5 elements that two arrays do not have the same element with the same index

This image I have 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 has the same five elements, namely "A", "B", "C", "D" and "E". I want to write an algorithm for sorting elements in all arrays, so that no two arrays have the same element (let them say "A") with the same index.

The kind of test output that will work for me will look like this:

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 tried to solve this, but could not complete. I just wrote a shuffle function to sort elements from 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)

Xcode Playground. , 102 103, E1 E2 . , .

+4
5

E1,..., E5 ( ), E2 E5 ( , - E1).

, E2 E5 .

import GameplayKit

func shiftByOne (arr: [String]) -> [String] {
    var shiftedArr = arr
    shiftedArr.insert(shiftedArr.popLast()!, atIndex: 0)
    return shiftedArr
}

var E1 = ["A", "B", "C", "D", "E"]
E1 = GKRandomSource.sharedRandom().arrayByShufflingObjectsInArray(E1) as! [String]
var E2 = shiftByOne(E1)
var E3 = shiftByOne(E2)
var E4 = shiftByOne(E3)
var E5 = shiftByOne(E4)

/** Result without initial shuffle:
E1 = ["A", "B", "C", "D", "E"]
E2 = ["B", "C", "D", "E", "A"]
E3 = ["C", "D", "E", "A", "B"]
E4 = ["D", "E", "A", "B", "C"]
E5 = ["E", "A", "B", "C", "D"] */

E1 (, ) , E2 E5 , w.r.t. , .

R Menke , E1, ( ). shuffler, , Swifty . :

+7

, :

  • ( , )
  • ( )
  • ( )

.

, .

["A", "B", "C", "D", "E"]
["B", "C", "D", "E", "A"]
["C", "D", "E", "A", "B"]
["D", "E", "A", "B", "C"]
["E", "A", "B", "C", "D"]

.

["C", "D", "E", "A", "B"]
["B", "C", "D", "E", "A"]
["A", "B", "C", "D", "E"]
["D", "E", "A", "B", "C"]
["E", "A", "B", "C", "D"]

.

["C", "E", "A", "D", "B"]
["B", "D", "E", "C", "A"]
["A", "C", "D", "B", "E"]
["D", "A", "B", "E", "C"]
["E", "B", "C", "A", "D"]

, , .

Ruby. .

# Initialize the first row of the matrix
matrix = []
matrix[0] = ('A'..'E').to_a

size = matrix[0].size

# Initialize the rotated array
for i in 1..size-1
  matrix[i] = matrix[i-1].rotate
end

puts "Original matrix"
for x in matrix
  puts x.inspect
end

# Shuffle the indexes of the rows and columns
rows = (0..size-1).to_a.shuffle
cols = (0..size-1).to_a.shuffle

# Shuffle the rows
for i in 0..size-1
  row1 = i
  row2 = rows[i]
  tmp = matrix[row1]
  matrix[row1] = matrix[row2]
  matrix[row2] = tmp
end

# Shuffle the columns.
for i in 0..size-1
  col1 = i
  col2 = cols[i]

  for j in 0..size-1
    tmp = matrix[j][col1]
    matrix[j][col1] = matrix[j][col2]
    matrix[j][col2] = tmp
  end
end

puts "Shuffled matrix"
for x in matrix
  puts x.inspect
end

@Schwern: .

+3

. "" ( - arc4random_uniform)

import Foundation // arc4random_uniform

let arr = Array(1...5)
var result: [[Int]] = []
repeat {
    let r = arr.sort { (a, b) -> Bool in
        arc4random_uniform(2) == 0
    }
    if !result.contains({ (s) -> Bool in
        s == r
    }) {
        result.append(r)
    }
} while result.count < 6

print(result)
// [[2, 3, 1, 4, 5], [4, 2, 1, 3, 5], [2, 1, 3, 4, 5], [2, 3, 1, 5, 4], [2, 4, 5, 1, 3], [2, 1, 4, 3, 5]]
0

iPhone, . , .

, (, "A" ), .

var dict = [String : [Int]]()

5 . , - . , , , dict.

, .

0

:

  • , /.
  • ( )

, .


Array , Array Element Equatable.

extension Array where Element : _ArrayType, Element.Element : Equatable {

Self Array<Element> , Array<Array<Element.Element>>. . .

    func entropy() -> [[Element.Element]]? { // returns nil when it is impossible

.

        var matrix = self.matrix() // see gist for Matrix type

Array. elements row.

        func swap(row r:Int,column c:Int) {

            let nextColumn : Int = {
                if (c + 1) < matrix[r].count {
                    return c + 1
                } else {
                    return 0
                }
            }()

            let element = matrix[r][c]
            let neighbour = matrix[r][nextColumn]
            matrix[r][c] = neighbour
            matrix[r][nextColumn] = element

        }

:

β†’ β†’ β†’ .

. , , .

        while matrix.columnsContainDuplicates() {
            for c in 0..<matrix.columns.count {
                let column = matrix.columns[c]
                if let dupeIndex = column.indexOfDuplicate() {
                    swap(row: dupeIndex, column: c)
                }
            }
        }

        return matrix.rows
    }
}

, , . , .

extension Array where Element : _ArrayType, Element.Element : Equatable {

    func entropy() -> [[Element.Element]]? {

        var matrix = self.matrix() // this comes from an extension to Array: if nested, can be converted to a matrix

        // is there a possible configuration where no column has duplicates?
        func solvable() -> Bool {
            // this just checks if element x does not appear more than there are possible indexes.
            var uniqueElements : [Element.Element] = []
            var occurences : [Int] = []

            for row in matrix.rows {
                var rowCopy = row
                while let pop = rowCopy.popLast() {
                    if let index = uniqueElements.indexOf(pop) {
                        occurences[index] += 1
                        occurences[index]
                    } else {
                        uniqueElements.append(pop)
                        occurences.append(1)
                    }
                }
            }

            var highest = 0
            for times in occurences {
                if highest < times { highest = times }
            }

            if highest > matrix.columns.count {
                highest
                return false
            }

            return true
        }

        func swap(row r:Int,column c:Int) {

            let nextColumn : Int = {
                if (c + 1) < matrix[r].count {
                    return c + 1
                } else {
                    return 0
                }
            }()

            let element = matrix[r][c]
            let neighbour = matrix[r][nextColumn]
            matrix[r][c] = neighbour
            matrix[r][nextColumn] = element

        }

        guard solvable() else {
            return nil
        }

        while matrix.columnsContainDuplicates() {
            for c in 0..<matrix.columns.count {
                let column = matrix.columns[c]
                if let dupeIndex = column.indexOfDuplicate() {
                    swap(row: dupeIndex, column: c)
                }
            }
        }

        return matrix.rows
    }
}

:

let test = [[4,3,2,1],[5,2,3,4],[1,6,3,4],[1,2,3,4]] // 8 loops
test.entropy() // [[3, 4, 1, 2], [4, 3, 2, 5], [6, 1, 4, 3], [1, 2, 3, 4]]

let test2 = [[1,2,3,4],[1,2,3,4],[1,2,3,4],[1,2,3,4]] // 8 loops
test2.entropy() // [[3, 4, 1, 2], [4, 3, 2, 1], [2, 1, 4, 3], [1, 2, 3, 4]]
0
source

Source: https://habr.com/ru/post/1621727/


All Articles