0

Hello am new to SO and i need some help. Is there a way i can put the value of count of a loop as an index for this:

var groups = [[String]]()

groupA.append(arr[randoms[0]])
groupA.append(arr[randoms[1]])

groupB.append(arr[randoms[2]])
groupB.append(arr[randoms[3]])

groupC.append(arr[randoms[4]])
groupC.append(arr[randoms[5]])

groupD.append(arr[randoms[6]])
groupD.append(arr[randoms[7]])

groupE.append(arr[randoms[8]])
groupE.append(arr[randoms[9]])

groups.append(groupA)
groups.append(groupB)
groups.append(groupC)
groups.append(groupD)
groups.append(groupE)

i want the index 0-10 of the random array to be from a loop or be dynamic in some way is there a way i can achieve this with swift?

  • Why do you need `randoms` at all? Just generate the random numbers. – matt Mar 06 '17 at 19:53
  • I generated random number that won't repeat the same number and stored it in an array – Hafeez Sagay Mar 06 '17 at 19:59
  • Yes, I thought maybe that was the reason. :) – matt Mar 06 '17 at 20:00
  • In that case, why not just shuffle `arr`? Now the first 10 items of the shuffled array are your items to be dealt out into groups. – matt Mar 06 '17 at 20:01
  • After shuffling all the items how can i group it then? because am using a multidimensional array here – Hafeez Sagay Mar 06 '17 at 20:05
  • I don't see any multidimensional array in your code. Basically you need to show your real code, I think. – matt Mar 06 '17 at 20:06
  • So am I right that you never really needed `groupA` and `groupB` etc in the first place? You really just want to form (or deal into) this array of arrays? Because this is actually going to be much more feasible if you don't have these extra copies of the arrays. – matt Mar 06 '17 at 20:11
  • Yes i want to pair the elements of the array – Hafeez Sagay Mar 06 '17 at 20:12
  • See my answer below. – matt Mar 06 '17 at 20:28
  • I would suggest that, at bottom, this is just a duplicate of http://stackoverflow.com/questions/26395766/swift-what-is-the-right-way-to-split-up-a-string-resulting-in-a-string-wi – matt Mar 06 '17 at 20:31

2 Answers2

1

I think you are looking for the enumerated() function...

import Foundation
let randoms = [arc4random(),arc4random(),arc4random(),arc4random(),]
randoms.enumerated().forEach {
    print($0,$1)
}

Try that out in an Xcode Playground.

harrisg
  • 613
  • 7
  • 12
0

You might be looking for this sort of thing. Let's get rid of random and just shuffle arr to generate a list of actual values in random order. (There's lots of array-shuffling code on Stack Overflow so I won't bother to tell you how to do that.) Then we can also get rid of your intermediate arrays and just deal directly into the inner arrays of the array-of-arrays groups:

var groups : [[String]] = Array(repeating: [], count: 5)
var shuffled = ["hey", "ho", "nonny", "no", "sis", "boom", "bah", "yo", "hi", "lo"]

var next = 0
for ix in stride(from:0, to:9, by:2) {
    for ixx in 0..<2 {
        groups[next].append(shuffled[ix+ixx])
    }
    next += 1
}

groups // [["hey", "ho"], ["nonny", "no"], ["sis", "boom"], ["bah", "yo"], ["hi", "lo"]]

We can actually express that more concisely using map (as explained Swift: what is the right way to split up a [String] resulting in a [[String]] with a given subarray size?):

let shuffled = ["hey", "ho", "nonny", "no", "sis", "boom", "bah", "yo", "hi", "lo"]
let chunkSize = 2
let groups = stride(from: 0, to: shuffled.count, by: chunkSize).map {
    Array(shuffled[$0..<min($0 + chunkSize, shuffled.count)])
}

groups // [["hey", "ho"], ["nonny", "no"], ["sis", "boom"], ["bah", "yo"], ["hi", "lo"]]

Even better, we can make this an extension on Array:

extension Array {
    func chunked(by chunkSize:Int) -> [[Element]] {
        let groups = stride(from: 0, to: self.count, by: chunkSize).map {
            Array(self[$0..<[$0 + chunkSize, self.count].min()!])
        }
        return groups
    }
}


let shuffled = ["hey", "ho", "nonny", "no", "sis", "boom", "bah", "yo", "hi", "lo"]
let groups = shuffled.chunked(by:2)

groups // [["hey", "ho"], ["nonny", "no"], ["sis", "boom"], ["bah", "yo"], ["hi", "lo"]]
Community
  • 1
  • 1
matt
  • 515,959
  • 87
  • 875
  • 1,141