需要一种算法来对5个数组的元素进行混洗,每个数组具有相同的5个元素,这样就不会有两个数组在相同的索引处具有相同的元素 [英] Need an algorithm for to shuffle elements of 5 arrays, each with the same 5 elements, such that no two arrays have the same element at the same index

查看:102
本文介绍了需要一种算法来对5个数组的元素进行混洗,每个数组具有相同的5个元素,这样就不会有两个数组在相同的索引处具有相同的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下五个数组

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"]

每个数组具有相同的五个元素,即 A, B, C, D和 E。我想编写一种算法来对所有数组中的元素进行排序,以使没有两个数组在相同的索引处具有相同的元素(比如说 A)。

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"]

我尝试解决此问题,但无法完成。我刚刚编写了一个改组函数,用于对两个数组(E1和E2)的元素进行排序。

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)

使用此代码,我在Xcode Playground上遇到以下错误。尽管有时错误会被消除,并且在第102行和第103行的输出是正确的,但是我仍然无法提取该输出并将其分别永久保存到E1和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.

谢谢

推荐答案

因为您知道数组 E1 ,..., E5 来保存相同的条目(相同订单),则无需显式保存数组 E2 E5 (因为您知道这些数组的值等于 E1 )。

Since you know arrays E1, ..., E5 to hold identical entries (in identical order), you needn't explicitly hold the arrays E2 through E5 (since you know these are value-equal to E1).

因此,您只需定义一个移位函数并创建 E2 通过 E5 重复移动前一个数组。

Hence, you could simply define a shift function and create E2 through E5 by repeated shifting of previous array.

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 都是构造的, wrt

This method starts with E1 (possibly shuffling only this array), and guarantees that E2 through E5 are constructed to all differ, w.r.t. order, from each other.

如下面的R Menke所述,如果对数组 E1 进行混洗,则行为相同将保持(但是改组初始数组)。在这里,我使用了与您的示例相同的混洗器,但是对于更多的 Swifty 方法,请参见例如:

As noted by R Menke below, if you shuffle array E1, the same behaviour will hold (however with shuffled initial array). Here I've used the same shuffler as in your example, but for a more Swifty approach, see e.g.:

  • How do I shuffle an array in Swift?

这篇关于需要一种算法来对5个数组的元素进行混洗,每个数组具有相同的5个元素,这样就不会有两个数组在相同的索引处具有相同的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆