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)
}
}
filterpart, and what isincrementIndex(andAppend:)? What's the real goal here?filteronly lets me remove a value, not an index, so I didn't include it in my question.prefix&suffixget the job done for most cases, but not all cases and I'm seeking to figure out how to exclude a specific index. I don't know how many indices I'll have and there are some edge cases which throw out of bounds exception. It seems like something that's build in somewhere to the standard library, but I can't figure it out.