diff --git a/WeeklyChallenge2022.playground/Pages/Challenge45.xcplaygroundpage/Contents.swift b/WeeklyChallenge2022.playground/Pages/Challenge45.xcplaygroundpage/Contents.swift index f340f95..2b149c1 100644 --- a/WeeklyChallenge2022.playground/Pages/Challenge45.xcplaygroundpage/Contents.swift +++ b/WeeklyChallenge2022.playground/Pages/Challenge45.xcplaygroundpage/Contents.swift @@ -29,3 +29,56 @@ import Foundation * https://retosdeprogramacion.com/semanales2022. * */ + +internal func validArray(_ data: [Int]) -> Bool { + let lengthData = data.count + if lengthData < 3 { return false } + if data.contains(where: { $0 < 0 }) { return false } + if data.filter({ $0 == 0 }).count == lengthData { return false } + return true +} + +func countGotas(_ data: [Int]) -> Int { + guard validArray(data) else { + return 0 + } + + var countTotal = 0 + let lengthData = data.count + (1.. 0 { + countTotal += valueToSum + } + } + + return countTotal +} + +let candidate1 = [2, 1, 2, 3, 0, 4, 0, 4] +print("En total hay \(countGotas(candidate1)) gotas de agua") + +let candidate2 = [2, 3] +print("En total hay \(countGotas(candidate2)) gotas de agua") + +let candidate3 = [2, 3, 2, 3, 5, 1, 5] +print("En total hay \(countGotas(candidate3)) gotas de agua") + +let candidate4 = [2, 7, 2, 3, 0, 1] +print("En total hay \(countGotas(candidate4)) gotas de agua") + +let candidate5 = [2, 0, 2, 3, 0, 0, 0, 4] +print("En total hay \(countGotas(candidate5)) gotas de agua") + +let candidate6 = [2, 0, -2, 3, 0, 0, -1, 4] +print("En total hay \(countGotas(candidate6)) gotas de agua") + +let candidate7 = [0, 0, 0, 0, 0] +print("En total hay \(countGotas(candidate7)) gotas de agua") + +let candidate8: [Int] = [] +print("En total hay \(countGotas(candidate8)) gotas de agua") diff --git a/WeeklyChallenge2022.playground/contents.xcplayground b/WeeklyChallenge2022.playground/contents.xcplayground index 63f417d..0b390e1 100644 --- a/WeeklyChallenge2022.playground/contents.xcplayground +++ b/WeeklyChallenge2022.playground/contents.xcplayground @@ -47,6 +47,5 @@ - \ No newline at end of file