I'd like to filter an array of numbers and use reduce on them, but I need to exclude a specific index
and I can't divide. Is it possible to do this with methods that are part of Foundation
in Swift?
I've tried breaking the array into two using prefix
& suffix
, but there are some edge cases where it blows up w/ an out of bounds exception.
while currentIndex < nums.count - 2 {
for _ in nums {
let prefix = nums.prefix(currentIndex)
let suffix = nums.suffix(from: currentIndex + 1)
if prefix.contains(0) || suffix.contains(0) {
incrementIndex(andAppend: 0)
}
let product = Array(prefix + suffix).reduce(1, *)
incrementIndex(andAppend: product)
}
}