From 225d79930b49784cc7355636a1620b644dfe8fc0 Mon Sep 17 00:00:00 2001 From: Camilo Plaza Date: Sat, 12 Nov 2022 19:48:45 +0100 Subject: [PATCH 1/2] Reto #45 --- .../Contents.swift | 50 +++++++++++++++++++ .../contents.xcplayground | 1 - 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/WeeklyChallenge2022.playground/Pages/Challenge45.xcplaygroundpage/Contents.swift b/WeeklyChallenge2022.playground/Pages/Challenge45.xcplaygroundpage/Contents.swift index f340f95..a9cca75 100644 --- a/WeeklyChallenge2022.playground/Pages/Challenge45.xcplaygroundpage/Contents.swift +++ b/WeeklyChallenge2022.playground/Pages/Challenge45.xcplaygroundpage/Contents.swift @@ -29,3 +29,53 @@ 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") 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 From 3b5fbe99a1cd615f37a5388181f11bb50d823f93 Mon Sep 17 00:00:00 2001 From: Camilo Plaza Date: Mon, 14 Nov 2022 08:33:15 +0100 Subject: [PATCH 2/2] ADD empty Array Test --- .../Pages/Challenge45.xcplaygroundpage/Contents.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/WeeklyChallenge2022.playground/Pages/Challenge45.xcplaygroundpage/Contents.swift b/WeeklyChallenge2022.playground/Pages/Challenge45.xcplaygroundpage/Contents.swift index a9cca75..2b149c1 100644 --- a/WeeklyChallenge2022.playground/Pages/Challenge45.xcplaygroundpage/Contents.swift +++ b/WeeklyChallenge2022.playground/Pages/Challenge45.xcplaygroundpage/Contents.swift @@ -79,3 +79,6 @@ 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")