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 reto 46 en Swift #285

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Binary file added .DS_Store
Binary file not shown.
64 changes: 64 additions & 0 deletions robot.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import Foundation

func girar(direccion: String) ->String {
var d = ""

switch direccion {
case "N":
d = "O"
case "S":
d = "E"
case "E":
d = "N"
case "O":
d = "S"
default:
print("Dirección incorrecta")
}

return d
}

func avanzar(x: Int, y: Int, dato: Int, direccion: String) -> [Int] {
var xx = x
var yy = y
switch direccion {
case "N":
yy += dato
case "S":
yy -= dato
case "E":
xx += dato
case "O":
xx -= dato
default:
print("Dirección incorrecta...")
}

return [xx,yy]
}

func dondeEsRobot(arreglo:[Int]) -> [Int] {
var d = "E"
var x = 0
var y = 0
var coordenadas: [Int]
for dato in arreglo {
if dato < 0 {
d = girar(direccion: d)
d = girar(direccion: d)
d = girar(direccion: d)
} else {
d = girar(direccion: d)
}
//print(d)
coordenadas = avanzar(x: x, y: y, dato: abs(dato), direccion: d)
x = coordenadas[0]
y = coordenadas[1]
}

return [x,y]
}

print(dondeEsRobot(arreglo: [10,5,-2]))
print(dondeEsRobot(arreglo: [-2,3,-1,4]))
4 changes: 4 additions & 0 deletions robot.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
<timeline fileName='timeline.xctimeline'/>
</playground>