import Foundation
func solution(_ a:Int, _ d:Int, _ included:[Bool]) -> Int {
var result = 0
for (idx, val) in included.enumerated() {
if included[idx] {
result += (idx * d) + a
}
}
return result
}
오늘은 enumerated()
함수를 문제를 풀다가 알게되었습니다.
enumerated()
함수는 ' 인덱스와 값의 쌍으로 시퀀스 ' 를 반환하는 함수 입니다.
for (idx, val) in "ab".enumerated() {
print(idx, ":", val)
/*
0 : a
1 : b
*/
print(type(of : idx)) // Int
}
for (idx, val) in ["1", "2", "3"].enumerated() {
print(idx, ":", val)
/*
0 : 1
1 : 2
2 : 3
*/
print(type(of : idx)) // // Int
}