Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solución al reto #34 #249

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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 @@ -15,3 +15,76 @@ import Foundation
* - Tienes toda la información sobre los retos semanales en https://retosdeprogramacion.com/semanales2022.
*
*/

private func arregloOrdenado(_ n: Int) -> [Int] {
return Array(Set((0...n).map { _ in .random(in: 0...n)})).sorted()
}

private func arregloDesordenado(_ n: Int) -> [Int] {
return Array(Set((0...n).map { _ in .random(in: 0...n)}))
}

private func numerosFaltantes(_ arr: [Int]) -> [Int]? {

enum caseStatus {
case arregloVacio
case arregloUnElemento
case arregloDesordenado
case arregloValido
case arregloDesconocido
}

var statusDelArreglo: caseStatus {
switch true {
case arr.isEmpty:
print("El arreglo esta vacio")
return caseStatus.arregloVacio
case arr.count == 1:
print("El arreglo solo tiene un elemento")
return caseStatus.arregloUnElemento
case arr != arr.sorted():
print("El arreglo esta desordenado")
return caseStatus.arregloDesordenado
case arr == arr.sorted():
print("El arreglo es valido")
return caseStatus.arregloValido
default:
return caseStatus.arregloDesconocido
}
}

guard statusDelArreglo == caseStatus.arregloValido else {
return nil
}

var counter = 0
var missingNumbers = [Int]()

while counter < arr.count {

guard (counter + 1) != arr.count else {
break
}

guard (arr[counter + 1] - arr[counter]) == 1 else {
let arr = Array((arr[counter] + 1)..<arr[counter + 1])
missingNumbers.append(contentsOf: arr)
counter += 1
continue
}

counter += 1

}
return missingNumbers
}

let arregloValido = arregloOrdenado(20)
let arregloAleatorio = arregloDesordenado(20)
let arregloVacio = [Int]()
let arregloDeUnSoloElemento = [1]

print(numerosFaltantes(arregloValido) ?? "Hay un problema con el arreglo introducido")
print(numerosFaltantes(arregloAleatorio) ?? "Hay un problema con el arreglo introducido")
print(numerosFaltantes(arregloVacio) ?? "Hay un problema con el arreglo introducido")
print(numerosFaltantes(arregloDeUnSoloElemento) ?? "Hay un problema con el arreglo introducido")