-
Notifications
You must be signed in to change notification settings - Fork 182
/
Copy pathContents.swift
72 lines (62 loc) · 2.99 KB
/
Contents.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import Foundation
/*
* Reto #17
* LA CARRERA DE OBSTÁCULOS
* Fecha publicación enunciado: 25/04/22
* Fecha publicación resolución: 02/05/22
* Dificultad: MEDIA
*
* Enunciado: Crea una función que evalúe si un/a atleta ha superado correctamente una
* carrera de obstáculos.
* - La función recibirá dos parámetros:
* - Un array que sólo puede contener String con las palabras "run" o "jump"
* - Un String que represente la pista y sólo puede contener "_" (suelo) o "|" (valla)
* - La función imprimirá cómo ha finalizado la carrera:
* - Si el/a atleta hace "run" en "_" (suelo) y "jump" en "|" (valla) será correcto y no
* variará el símbolo de esa parte de la pista.
* - Si hace "jump" en "_" (suelo), se variará la pista por "x".
* - Si hace "run" en "|" (valla), se variará la pista por "/".
* - La función retornará un Boolean que indique si ha superado la carrera.
* Para ello tiene que realizar la opción correcta en cada tramo de la pista.
*
* Información adicional:
* - Usa el canal de nuestro discord (https://mouredev.com/discord) "🔁reto-semanal" para preguntas, dudas o prestar ayuda a la comunidad.
* - Puedes hacer un Fork del repo y una Pull Request al repo original para que veamos tu solución aportada.
* - Revisaré el ejercicio en directo desde Twitch el lunes siguiente al de su publicación.
* - Subiré una posible solución al ejercicio el lunes siguiente al de su publicación.
*
*/
enum AthleteState: String {
case run = "_"
case jump = "|"
}
func checkRace(athlete: [AthleteState], track: String) -> Bool {
let totalActions = athlete.count > track.count ? athlete.count : track.count
let minActions = athlete.count > track.count ? track.count : athlete.count
let trackSegments = Array(track)
var athleteTrack = ""
for index in (0..<totalActions) {
if index >= minActions {
athleteTrack += "?"
} else {
let segment = trackSegments[index]
let state = athlete[index]
switch state {
case .run:
athleteTrack += segment.description == state.rawValue ? state.rawValue : "/"
case .jump:
athleteTrack += segment.description == state.rawValue ? state.rawValue : "x"
}
}
}
print(athleteTrack)
return track == athleteTrack
}
print(checkRace(athlete: [.run, .jump, .run, .jump, .run], track: "_|_|_"))
print(checkRace(athlete: [.run, .run, .run, .jump, .run], track: "_|_|_"))
print(checkRace(athlete: [.run, .run, .jump, .jump, .run], track: "_|_|_"))
print(checkRace(athlete: [.run, .run, .jump, .jump, .run], track: "_|_|_|_"))
print(checkRace(athlete: [.run, .jump, .run, .jump], track: "_|_|_"))
print(checkRace(athlete: [.run, .jump, .run, .jump, .run, .jump, .run], track: "_|_|_"))
print(checkRace(athlete: [.jump, .jump, .jump, .jump, .jump], track: "|||||"))
print(checkRace(athlete: [.jump, .jump, .jump, .jump, .jump], track: "||?||"))