Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,41 @@ import Foundation
* https://retosdeprogramacion.com/semanales2022.
*
*/

func countBoomerang(_ data: [Int]) -> Int {
let magicNumber = 3
var countTotal = 0
let lengthData = data.count

if lengthData >= magicNumber {
for idx in data.indices {
if idx+magicNumber <= lengthData {
let candidate: [Int] = Array(data[idx..<idx+magicNumber])
if let firstValue = candidate.first,
let lastValue = candidate.last {
let midValue = candidate[1]
if firstValue == lastValue && firstValue != midValue {
print("Aquí un bumerán: \(candidate)")
countTotal += 1
}
}
} else {
break
}
}
}

return countTotal
}

let candidate1 = [2, 1, 2, 3, 3, 4, 2, 4]
print("En total hay \(countBoomerang(candidate1)) bumerán/es")

let candidate2 = [2, 3]
print("En total hay \(countBoomerang(candidate2)) bumerán/es")

let candidate3 = [2, 3, 2, 3, 5, 1, 5]
print("En total hay \(countBoomerang(candidate3)) bumerán/es")

let candidate4 = [2, 7, 2, 3, -5, 1, -5]
print("En total hay \(countBoomerang(candidate4)) bumerán/es")