Skip to content
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.
35 changes: 35 additions & 0 deletions contenedor.playground/Contents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Foundation

func contenedorAgua(arreglo:[Int])->Int{
var inicio = 0
var fin = arreglo.count - 1
var suma = 0
var dif: Int

while inicio != fin {
let punteroMenor = arreglo[inicio] < arreglo[fin] ? inicio : fin
if punteroMenor == fin {
fin -= 1
dif = arreglo[punteroMenor] - arreglo[fin]
while dif >= 0 {
suma += dif
fin -= 1
dif = arreglo[punteroMenor] - arreglo[fin]
}
} else {
inicio += 1
dif = arreglo[punteroMenor] - arreglo[inicio]
while dif >= 0 {
suma += dif
inicio += 1
dif = arreglo[punteroMenor] - arreglo[inicio]
}
}
}

return suma
}

print(contenedorAgua(arreglo: [4, 0, 3, 6, 1, 3]))
print(contenedorAgua(arreglo: [4,2,5,6,3,2,3]))
print(contenedorAgua(arreglo: [4,0,6,2,3]))
4 changes: 4 additions & 0 deletions contenedor.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>
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>