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?

10
  • Why do you need randoms at all? Just generate the random numbers. Commented Mar 6, 2017 at 19:53
  • I generated random number that won't repeat the same number and stored it in an array Commented Mar 6, 2017 at 19:59
  • Yes, I thought maybe that was the reason. :) Commented Mar 6, 2017 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. Commented Mar 6, 2017 at 20:01
  • After shuffling all the items how can i group it then? because am using a multidimensional array here Commented Mar 6, 2017 at 20:05

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This did not work and how am i supposed to use it with my code?
Don't forget to import Foundation
Hafeez Sagay, I'm just trying to show you how to incorporate the index of an item in an array into a loop.
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"]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.