Doubt for-in loop in Swift

1

I'm trying to do the following exercise in Swift:
Create the function obtenerFrecuencias that allows you to calculate the frequencies of a set of answers, numbers between 0 and 9, which are stored in an array of Int.

Example:

let respuestas = [0,0,1,1,2,1,2,3,5,1,2,2,2,6]
let frec = obtenerFrecuencias(respuestas: respuestas)
print("Frecuencias: \(frec)")
Frecuencias: [2, 4, 5, 1, 0, 1, 1, 0, 0, 0]

I know the solution is the following:

   func obtenerFrecuencias(respuestas: [Int]) -> [Int] {
    var frecuencias = Array(repeating: 0, count: 10)
    for puntuacion in respuestas {
        frecuencias[puntuacion] += 1
    }
    return frecuencias
   }

But I do not understand the way in which the for-in loop works, it should act on the indexes (in this case called punctuation) of the array, so that for answers [0] would return 0, responses [1] = 0, response [2] = 1 ... but the execution of frequencies [punctuation] + = 1 does not I would get what I wanted ... It seems that it is not fixed in the index but in the associated value, I explain:
For each punctuation with value 'i' in the initial array, add 1 to the value of the index 'i' in the resulting array and so on until you complete the first array.
So if you get the desired but I do not know if there is another explanation, or if it is possible to implement it in this way ...
I've been reviewing the Swift documentation and searched the web but I can not find anything about it. I hope you explained well, thanks in advance.

    
asked by Cristian 07.06.2018 в 02:02
source

1 answer

0

The for-in cycle works as follows:

for puntuación in respuestas {
}

Gets one by one the elements of the object that iterates, in this case in the first round score would be 0, then 0, then 1 and so on, running through each element of the response array.

In the function that you share the magic makes the object frequencies when using the initializer Array (repeating: 0, count: 10) what this instruction does is build an array with length 10 whose positions will have the initial element 0:

I'll give you an example for clarity

let fiveZs = Array(repeating: "Z", count: 5)
print(fiveZs)
// Prints "["Z", "Z", "Z", "Z", "Z"]"

In your case an object would be created in the following way

var frecuencias = [0,0,0,0,0,0,0,0,0,0]

Later in the for-in cycle, what it does is get the first answer value that is 0 and tell frequencies to access that index and in that position there is a value of zero increase in 1 and so on, in something graphic it would look like this:

let respuestas = [0,0,1,1,2,1,2,3,5,1,2,2,2,6]

for puntuacion in respuestas {.....
    frecuencias[0] += 1 //posición 0 vale 1
    frecuencias[0] += 1 //posición 0 vale ahora 2
    frecuencias[1] += 1 //posición 1 vale 1
    frecuencias[1] += 1 //posición 1 vale ahora 2
    frecuencias[2] += 1 //posición 2 vale ahora 1
    frecuencias[1] += 1 //posición 1 vale ahora 3
    frecuencias[2] += 1 //posición 2 vale ahora 2
    frecuencias[3] += 1 //posición 3 vale 1
    frecuencias[5] += 1 //posición 5 vale 1
    frecuencias[1] += 1 //posición 1 vale ahora 4
    frecuencias[2] += 1 //posición 2 vale ahora 3
    frecuencias[2] += 1 //posición 2 vale ahora 4
    frecuencias[2] += 1 //posición 2 vale ahora 5
    frecuencias[6] += 1 //posición 6 vale ahora 1
}.....

Since position 4, 7, 8 and 9 of the arrangement are never accessed, they remain at 0 and therefore our output values are:

[2, 4, 5, 1, 0, 1, 1, 0, 0, 0]
    
answered by 07.06.2018 / 18:17
source