Search code examples
arraysswiftprobability

In swift, how to generate a multidimensional array?


I have an array :

let myArray : [Int] = [1, 1, 1]

3 numbers are an example. In the reality, it will be different each time. And I have variables of minimum and maximum

let valueMin = 1
let valueMax = 500

I need to generate a FinalArray that will contain all the possible values ​​between min and max of each number of my initial array.

Here is an example:

for the beginning we have the first possibility and it's possibility001: [1, 1, 1]

I add this value to my FinalArray

var finalArray : [[Int]] = []

finalArray.append(possibility001)

The second value: possibility002: [1, 1, 2]

finalArray.append(possibility002)

possibility003: [1, 1, 3]

at one point, we will have possibility 14.944.020: [230, 182, 357]

etc until possibilityFinal: [500, 500, 500]

finalArray.append(possibilityFinal)

How could I make multi dimensional loop to create it?


Solution

  • You can probably do that using a recursive logic in a function:

    func possibleSums(min: Int, max: Int, length: Int) -> [Int] {
        guard length > 0 else { return [] }
    
        var sums = [Int]()
        for i in min...max {
            if length == 1 {
                sums.append(i)
            } else {
                let subSums = possibleSums(min: min, max: max, length: length - 1).map { $0 + i }
                sums.append(contentsOf: subSums)
            }
        }
        return sums
    }
    

    This will return an array with all possible sums including duplicate ones. But it will need to run like forever in your case:

    let sums = possibleSums(min: 1, max: 500, length: myArray.count)
    

    If you want to ditch the duplicate sum values, you can do something like this:

    func possibleSums(min: Int, max: Int, length: Int) -> [Int] {
        guard length > 0 else { return [] }
    
        var sums = [Int]()
        for i in min...max {
            if length == 1 {
                sums.append(i)
            } else {
                let subSums = possibleSums(min: i, max: max, length: length - 1).map { $0 + i }
                sums.append(contentsOf: subSums)
            }
        }
        return sums
    }
    

    This will also take a long time to finish.

    Update: To create a function that returns all the possible arrays without summing the numbers in each one, you can do something like this:

    func possibleArrays(min: Int, max: Int, length: Int) -> [[Int]] {
        guard length > 0 else { return [] }
    
        var arrays = [[Int]]()
        for i in min...max {
            if length == 1 {
                arrays.append([i])
            } else {
                let subArrays: [[Int]] = possibleArrays(min: min, max: max, length: length - 1).map {
                    var array = $0
                    array.insert(i, at: 0)
                    return array
                }
                arrays.append(contentsOf: subArrays)
            }
        }
        return arrays
    }