将列表在 n 大小的容器内随机排列。

huangapple go评论94阅读模式
英文:

Shuffle list within n size of bins in list

问题

我有一个Java列表,示例:

  1. a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22]

我想要对它进行洗牌,但是要在n大小的桶内进行。所谓的桶,我是指我想要先洗牌前5个元素,然后是接下来的5个元素,依此类推...
因此,其中一种期望的结果将是:

  1. sorted = [3,5,4,2,1, 8,9,7,10,6, 14,11,12,15,13, 19,20,17,18,16, 22,21]

如何高效地实现这个目标?

英文:

I have a java list, example:

  1. a = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22]

I want to shuffle it but within n size bins within. From bin, I meant, I want to shuffle first 5 elements, then next 5 and so on..
So, one of the the expected result will be:

  1. sorted = [3,5,4,2,1, 8,9,7,10,6, 14,11,12,15,13, 19,20,17,18,16, 22,21]

How can I do it efficiently

答案1

得分: 4

将数组放入列表中并对子列表进行洗牌:

  1. Integer[] array = new Integer[] {
  2. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  3. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  4. 21, 22
  5. };
  6. List<Integer> list = Arrays.asList(array);
  7. int binSize = 5;
  8. for (int i = 0, n = list.size(); i < n; i += binSize) {
  9. int j = Math.min(i + binSize, n);
  10. Collections.shuffle(list.subList(i, j));
  11. }
  12. array = list.toArray(new Integer[] {});
  13. System.out.println(Arrays.toString(array));
  1. [2, 1, 3, 4, 5, 6, 8, 10, 7, 9, 13, 11, 15, 14, 12, 19, 16, 17, 20, 18, 22, 21]
英文:

Put the array into a list and shuffle the sublists:

  1. Integer[] array = new Integer [] {
  2. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
  3. 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  4. 21, 22
  5. };
  6. List&lt;Integer&gt; list = Arrays.asList(array);
  7. int binSize = 5;
  8. for (int i = 0, n = list.size(); i &lt; n; i += binSize) {
  9. int j = Math.min(i + binSize, n);
  10. Collections.shuffle(list.subList(i, j));
  11. }
  12. array = list.toArray(new Integer[] { });
  13. System.out.println(Arrays.toString(array));
  1. [2, 1, 3, 4, 5, 6, 8, 10, 7, 9, 13, 11, 15, 14, 12, 19, 16, 17, 20, 18, 22, 21]

答案2

得分: 0

Just shuffle each subList defined by the start and end of the bins. Since the subList provides a view of the list, they are shuffled in place so there is no need to create a new collection.

  1. List<Integer> vals = new ArrayList<>(
  2. List.of(1,2,3,4,5,10,20,30,40,50,100,200,300,400,500,1000,2000));
  3. shuffleBins(vals);
  4. System.out.println(vals);

Prints

  1. [4, 5, 2, 3, 1, 10, 40, 20, 50, 30, 400, 200, 300, 100, 500, 2000, 1000]

The method

  1. public static void shuffleBins(int binSize,
  2. List<Integer> items) {
  3. int start = 0;
  4. for (int i = 0; i < items.size()/5; i++) {
  5. Collections.shuffle(items.subList(start, start+binSize ));
  6. start += binSize;
  7. }
  8. // finish up when items.size() % binSize != 0
  9. Collections.shuffle(items.subList(start, items.size() ));
  10. }

If you want to make it more versatile you can provide a set of indices indicating the bin starts.

  1. public static void shuffleBins(List<Integer> binStarts,
  2. List<Integer> items) {
  3. int start = 0;
  4. for (int i = 0; i < binStarts.size(); i++) {
  5. Collections
  6. .shuffle(items.subList(start, binStarts.get(i)));
  7. start = binStarts.get(i) + 1;
  8. }
  9. Collections.shuffle(items.subList(start, items.size()));
  10. }
英文:

Just shuffle each subList defined by the start and end of the bins. Since the subList provides a view of the list, they are shuffled in place so there is no need to create a new collection.

  1. List&lt;Integer&gt; vals = new ArrayList&lt;&gt;
  2. (List.of(1,2,3,4,5,10,20,30,40,50,100,200,300,400,500,1000,2000));
  3. shuffleBins(vals);
  4. System.out.println(vals);

Prints

  1. [4, 5, 2, 3, 1, 10, 40, 20, 50, 30, 400, 200, 300, 100, 500, 2000, 1000]

The method

  1. public static void shuffleBins(int binSize,
  2. List&lt;Integer&gt; items) {
  3. int start = 0;
  4. for (int i = 0; i &lt; items.size()/5; i++) {
  5. Collections.shuffle(items.subList(start, start+binSize ));
  6. start += binSize;
  7. }
  8. // finish up when items.size() % binSize != 0
  9. Collections.shuffle(items.subList(start, items.size() ));
  10. }

If you want to make it more versatile you can provide a set of indices indicating the bin starts.

  1. public static void shuffleBins(List&lt;Integer&gt; binStarts,
  2. List&lt;Integer&gt; items) {
  3. int start = 0;
  4. for (int i = 0; i &lt; binStarts.size(); i++) {
  5. Collections
  6. .shuffle(items.subList(start, binStarts.get(i)));
  7. start = binStarts.get(i) + 1;
  8. }
  9. Collections.shuffle(items.subList(start, items.size()));
  10. }
  11. </details>

huangapple
  • 本文由 发表于 2020年8月3日 23:09:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63232029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定