No tienes acceso a esta clase

隆Contin煤a aprendiendo! 脷nete y comienza a potenciar tu carrera

Compra acceso a todo Platzi por 1 a帽o

Antes: $249

Currency
$209/a帽o

Paga en 4 cuotas sin intereses

Paga en 4 cuotas sin intereses
Suscr铆bete

Termina en:

14D
18H
23M
51S

Trailing Closures

10/16
Recursos

Aportes 42

Preguntas 1

Ordenar por:

驴Quieres ver m谩s aportes, preguntas y respuestas de la comunidad?

o inicia sesi贸n.

// Reto

let numbers = [-9, 34 , -23, -2127 ,46, -6]

let numbersStrings = numbers.map{ (number) -> String in
       
    var number = number
    var output = ""
    let minus = "menos"
    let negative = number
    
    number = number < 0 ? number * -1 : number

    repeat{
        output = digitNames[number%10]! + output
        number /= 10
    }while number > 0
    output = negative < 0 ? minus + output : output
    return output
}

print(numbersStrings)

[鈥渕enosNueve鈥, 鈥淭resCuatro鈥, 鈥渕enosDosTres鈥, 鈥渕enosDosUnoDosSiete鈥, 鈥淐uatroSeis鈥, 鈥渕enosSeis鈥漖

Los clousures son m谩s o menos como funciones an贸nimas que se escriben un tanto diferente

let digitNames = [0:"Cero", 1:"Uno", 2:"Dos", 3:"Tres", 4:"Cuatro", 5:"Cinco", 6:"Seis", 7:"Siete", 8:"Ocho", 9:"Nueve"]
let numbers = [-16,58,-510,2127,-94]

let numberStrings = numbers.map { (number) -> String in
    let signal = (number < 0) ? "Menos" : ""
    var number = (number > 0) ? number : number*(-1)
    var output = ""
    repeat{
        output = digitNames[number%10]! + output
        number /= 10
    } while number > 0
    return signal+output
}
numberStrings
let digitNames = [0:"Cero", 1:"Uno", 2:"Dos", 3:"Tres", 4:"Cuatro", 5:"Cinco", 6:"Seis", 7:"Siete", 8:"Ocho", 9:"Nueve"]
let numbers = [16, -58, 510, 2127]

let numberStrings = numbers.map { (num) -> String in
    var number = num < 0 ? num * -1 : num
    var output = ""
    repeat{
        output = digitNames[number%10]! + output
        number /= 10
    }while number > 0
    
    return num < 0 ? "negativo" + output : output
}

Reto usando optional binding en el output

let numbers = [-97, -1, 16, 58, -510, 2127]

let numberStrings = numbers.map {(number) -> String in
    let initNumber = number
    var number = number < 0 ? number * -1 : number
    var output = ""

    repeat{
        if let modulOfTen = digitNames[number%10] {
            output = modulOfTen + " " + output
            number /= 10
        }
    }while number > 0
    output = initNumber < 0 ? "Menos " + output : output
    return output
}

En Platzi 馃挌lo mejor es la comunidad, viendo esta clase y los aportes de los otros estudiantes me ayudo muchisimo a resolver el reto, gracias a todos!

```js var numbers = [0: "cero", 1: "uno", 2: "dos", 3: "tres", 4: "cuatro", 5: "cinco", 6: "seis", 7: "siete", 8: "ocho", 9: "nueve"] var cifras = [23,56,128,2546,-10, -1234] var cifrasNumeros = cifras.map{ (numero) -> String in var numero = numero var output = "" var signo = "" if numero < 0{ numero *= -1 signo = "menos " } repeat{ output = numbers[numero%10]! + output numero /= 10 }while numero > 0 return signo + output } print(cifrasNumeros) ```

Reto:

let numberStrings = numbers.map { (number) -> String in
    var number = number;
    let isNegative = String(number).hasPrefix("-");
    if isNegative {
        number *= -1;
    }
    print(isNegative);
    var output = String();
    repeat {
        // Number % 10 => always returns the last digit in any given number.
        output = digitNames[number%10]! + output; // <-- The ! at the end is a force unwrapping.
        number /= 10;
    } while number > 0;
    if isNegative {
        output = "minus " + output;
    }
    return output;
}
let dicNumeros = [0:"Cero", 1:"Uno", 2:"Dos", 3:"Tres", 4:"Cuatro", 5:"Cinco", 6:"Seis", 7: "Siete", 8: "Ocho", 9:"Nueve"]

let numeros = [17, -1, 1997, -15, 1, -1998]
var texto: [String] = []
numeros.map{numero -> Void in
    var output = ""
    var numero = numero
    var numeroNegativo = false
    if (numero < 0 ) {
        numero *= -1
        numeroNegativo = true
    }
    repeat {
        output = dicNumeros[numero%10]! + output
        numero /= 10
    } while numero > 0
    
    if numeroNegativo { output = "-" + output }
    texto.append(output)
}
let numbers2 = [16,-58,510,2127]
let numbersStrings2 = numbers2.map{ (number) -> String in
    var number = number
    var output = ""
    
    func numberString (_ number : inout  Int, _ output : inout String) -> String{
        repeat{
            output = digiNames[number%10]!+" "+output
            number /= 10
        }while number > 0
        return output
    }
    
    if number < 0 {
        number = -number
        return "menos "+numberString(&number,&output)
        
    }else{
        return numberString(&number,&output)
    }
}

// Ejercicios

let digitNames = [
    0: "Cero", 1: "Uno", 2: "Dos", 3: "Tres", 4: "Cuatro",
    5: "Cinco", 6: "Seis", 7: "Siete", 8: "Ocho", 9: "Nueve"
]

let numbers = [-27, 0, -123, 456]

let numbersString = numbers.map { (number) -> String in
    
    var number = number
    var isNegative = false
    var output = ""
    
    if number < 0 {
        isNegative = true
        number *= -1
    }
    
    repeat {
        output = digitNames[number % 10]! + output
        number /= 10
    } while number > 0
    
    if isNegative {
        output = "-" + output
    }
    
    return output
}

Encontr茅 una forma de hacerlo usando la propiedad magnitude del number:

let digitNames = [0: "Cero", 1: "Uno", 2: "Dos", 3: "Tres", 4: "Cuatro", 5: "Cinco", 6: "Seis", 7: "Siete", 8: "Ocho", 9: "Nueve"]

let numbers = [-16, 58, -510, 2127]

let numberStrings = numbers.map { number -> String in
    let isNegative = number < 0
    var number = number.magnitude
    var output = ""
    repeat {
        output = digitNames[Int(number)%10]! + " " + output
        number /= 10
    } while number > 0
    if isNegative {
        output = "Menos " + output
    }
    return output
}

print(numberStrings)

Aqu铆 mi aporte: agregue un variable que me guarda el n煤mero original y dependiendo de ello realizo las respectivas validaciones.

let numbers = [-16, 58, -510, 2127]

let numberString = numbers.map{(number) -> String in
    var initNumber = number
    var number = number
    var output = ""
    
    number = initNumber < 0 ? initNumber * -1 : initNumber
    repeat {
        output = digitNames[number%10]! + output
        number /= 10
    } while number > 0
    output = initNumber < 0 ? "Menos" + output : output
    return output
}

Reto

let numbersString = numbers.map { (num) -> String in
    var number = num
    var output = ""
    if(num < 0) {
        number *= -1
        output = "negativo"
    }
    repeat {
        output = digitNames[number % 10]! + " \(output)"
        number /= 10
    } while number > 0
    return output
}

print("numero a string es \(numbersString)")

Resultado
numero a string es ["Uno seis ", "cinco ocho ", "cinco Uno Cero ", "dos Uno dos siete ", 鈥渄os tres negativo鈥漖

Muy genial, veo que se usa mucho en swift.

let notas = [10,12,15,16,20]
var ordenedNumbers = notas.filter { s1 in
s1 >= 12
}

let numberStrings = numbers.map { number -> String in
    var num = abs(number)
    var output = ""
    repeat {
        output = digitNames[num%10]! + output
        num /= 10
    }while num > 0
    
    return number >= 0 ? output : "Minus" + output
}

Soluci贸n al reto

let digitNames = [0: "Zero", 1: "One", 2: "Two", 3: "Three", 4: "Four", 5: "Five", 6: "Six", 7: "Seven", 8: "Height", 9: "Nine"]

let numbers = [10, -20, 15, -5]

let numberString = numbers.map{(number) -> String in
    var number = number
    var aux = number
    var output = ""
    number = number < 0 ? (number * -1) : number
    
    repeat{
        output = digitNames[number%10]! + output
        number /= 10
    } while number > 0

    return aux < 0 ? "-\(output)" : output
}
let digitNames = [0:"cero", 1:"Uno",2:"Dos",3:"tres",4:"cuatro",5:"cinco",
                  6:"seis",7:"siete",8:"ocho",9:"nueve"]
let numbers = [16,58,510,-2127]

let numberStrings = numbers.map {(number) -> String  in
    
    var num = number < 0 ? number * -1 : number
    var output = ""
 
    repeat {
        output =  digitNames [num%10]! + output
          num/=10
    } while num > 0
    
   return number < 0 ? "menos" + output  : output
}

Esto se parece mucho a los trailing lambdas que se emplean en Kotlin cumpliendo la misma funci贸n de los trailing closures de Swift

import UIKit

let digitNames = [ 0: "Zero", 1: "One", 2: "Two", 3: "Three", 4: "Four", 5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine" ]
let numbers = [1, -17, 46, -510, 2227]

let numberStrings = numbers.map { (numberParam) -> String in
    var number = (numberParam < 0 ? numberParam * -1 : numberParam)
    var output = ""
    let signNegative = "-"
    
    repeat {
        output = "\(digitNames[number%10]!) \(output)"
        number /= 10
    } while(number > 0)
    
    output = (numberParam < 0 ? "\(signNegative) \(output)" : output)
    return output
}
print("numberStrings: \(numberStrings)")

let numbers = [-9, 34 , -23, -2127 ,46, -6]
let numberStrings = numbers.map{(number)-> String in
    var number = number
    var output = ""
    var isNegative = false
    repeat{
        var key = number % 10
       
        if(number < 0)
        {
           
           key = key * -1
           isNegative = true
        }
        output =  digitNames[key]! + output
        number /= 10
        
    }while(number != 0)
    if(isNegative)
    {
        output = "menos" + output
        isNegative = false
    }
    return output
}

Soy p茅simo para las matem谩ticas pero qued贸 m谩s que claro como funcionan, genial!

Reto

import Foundation

let digitNames = [0:"cero", 1:"uno",2:"dos",3:"tres",4:"cuatro",5:"cinco",6:"seis",
                  7:"siete", 8:"ocho",9:"nueve"]

let numbers = [-200, 34 , -2, -100 , 46, -6 , 120 , -20, 0]

let numbersStrings = numbers.map{ (number) -> String in
       
    var number = number
    var output = ""
    let minus = "menos"
    var flag = false

    if number < 0 {
      number *= -1
      flag = true
    }
    
    repeat{
        output = digitNames[number%10]! + " " + output
        number /= 10
    }while number > 0

    if(flag){output = minus + " " + output}

    return output 
}

Resolv铆 el reto de esta forma 馃槃

let numbersToString = numbers.map{ (number) -> String in
    var absoluteNumber = number < 0 ? number * -1 : number
    var output = ""
    
    repeat{
        output = digitNames[absoluteNumber%10]! + output
        absoluteNumber /= 10
    }while absoluteNumber > 0
    
    if number < 0 {
            output = "(Negativo)" + output
            absoluteNumber = -absoluteNumber
        }
    
    return output
}

RETO

let digitNames = [0: "Cero", 1: "Uno", 2: "Dos", 3: "Tres", 4: "Cuatro", 5: "Cinco", 6: "Seis", 7: "Siete", 8: "Ocho", 9: "Nueve"]
let numbers = [-45, -9, 16, 58, 510, 2127]
let numberStrings = numbers.map { (number) -> String in
    var number = number
    var output = ""
    let isNegative = number < 0
    number = abs(number)
    repeat {
        output = digitNames[number % 10]! + output
        number /= 10
    } while (number > 0)
    return isNegative ? "menos\(output)" : output
}

No se si esta bien, en lo que veo funciona se me ocurrio esta forma que no es la que dice el profe en el video pero creo que esta bien.

let digitNames = [0:"Cero", 1:"Uno", 2:"Dos", 3:"Tres", 4:"Cuatro",
                  5:"Cinco", 6:"Seis", 7:"Siete", 8:"Ocho", 9:"Nueve", -1 :"menos 1", -2 :"menos 2", -3 :"menos 3", -4 :"menos 4", -5 :"menos 5", -6 :"menos -6", -7 :"menos 7", -8 :"menos 8", -9 :"menos 9"]
let numbers = [-16, 58, 510, 2127]

let numberStrings = numbers.map { (number) -> String in
    
    var number = number
    var output = ""
    repeat{
        output = digitNames[number%10]! + output
        number /= 10
    }while number > 0
    return output
}
let numbers = [-16, 58, -510, 2127]
let numsToStrings = numbers.map { (num) -> String in
    var num = num
    var str = ""
    var positive = true
    if num < 0 {
        positive = false
        num = -num
    }
    
    repeat{
        str = digitNames[num%10]! + str
        num /= 10
    }while num > 0
    
    return positive ? str : "menos " + str
}

print(numsToStrings)

Reto:

let digitNames = [
    0:"Cero", 1:"Uno", 2:"Dos", 3:"Tres", 4:"Cuatro"
    , 5:"Cinco", 6:"Seis", 7:"Siete", 8:"Ocho", 9:"Nueve"]
let numbers = [16, 58, 503, 10567, -23, 0]
let numbersStrings = numbers.map { (number) -> String in
    let minus = number < 0 ? "Menos" : ""
    var number = number < 0 ? number * -1 : number
    var output = ""
    repeat {
        output = digitNames[number%10]! + output
        number /= 10
    } while number > 0
    output = minus + output
    return output
}
numbersStrings

let numberStrings = numbers.map { (number) -> String in
    
    var number = number
        
    var output = ""
    
    var sign = ""
    
    if(number < 0){
        number = abs(number)
        sign = "Menos"
    }

    repeat{
        output = digitNames[number%10]! + output
        number /= 10
        
        if(number <= 0){
            output = sign + output
        }
        
    }while number > 0
    return output
}

numberStrings
// ["MenosUnoSeis", "CincoOcho", "CincoUnoCero", "DosUnoDosSiete"]
let digitNames = [0: "Cero", 1:"Uno", 2:"Dos", 3:"Tres", 4:"Cuatro", 5:"Cinco", 6:"Seis", 7:"Siete", 8:"Ocho", 9:"Nueve"]

let numbers = [16, 58, 510, 2127, -321, -9]

let numberStrings = numbers.map { (number) -> String in
    let minus = number < 0 ? "Minus" : ""
    var number = number < 0 ? -number : number
    var output = ""
    
    repeat {
        output = digitNames[number%10]! + output
        number /= 10
    } while number > 0
    
    return minus + output
}
print(numberStrings)

let numbers = [-16, -58, 510, 2127]
let numberStrings = numbers.map { (number) -> String in
    var number = number
    let isSigned = number < 0
    number = abs(number)
    var output = ""
    repeat
    {
        output = digitNames[number % 10]! + output
        number /= 10
    }while number > 0
    output = isSigned ? "Menos" + output : output
    return output
}
let numbers2 = [-16, 58, -510, 2127, 0, -32]
let newNumberString = numbers2.map { (number) -> String in
    let originalNumber = number
    var number = number < 0 ? number * -1 : number
    var output = ""
    repeat {
        output = digitNames[number%10]! + output
        number /= 10
    } while number > 0
    if originalNumber < 0 {
        output = "Menos" + output
    }
    return output
}
// salida: ["MenosUnoSeis", "CincoOcho", "MenosCincoUnoCero", "DosUnoDosSiete", "Cero", "MenosTresDos"]

Muy potente el ejemplo, si se programara con un ciclo normal seria mucho mas larga la sintaxis ademas creo que esto abre las puertas para ejecutar operaciones en paralelo usando tarjetas graficas.

let digitNames = [0: "Cero", 1:"Uno", 2:"Dos", 3:"Tres", 4:"Cuatro", 5:"Cinco", 6:"Seis", 7:"Siete", 8:"Ocho", 9:"Nueve"]
let numbers = [32, 52, 50, 2484, -16, -49]

let numbersClosure = numbers.map { (number) -> String in
    var number =  number
    var output = ""
    var minus = ""
    if number < 0 {
        number = abs(number)
        minus = "-"
    }
    repeat {
        output = digitNames[number%10]! + output
        number /= 10
    } while number > 0
    return minus + output
}
let numbersString = numbers.map{(number)-> String in
    let negative = number<0
    var number = number<0 ? -number : number
    var output=""
    repeat {
        output = digitNames[number%10]! + output
        number = number/10
    }while number > 0
    return negative ? "menos"+output : output
}

Reto

let numberStrings = numbers.map { (number) -> String in
    let isNegative = number < 0
    var number = isNegative ? (number * -1) : number
    var output = ""
    
    repeat {
        output = digitNames[number%10]! + output
        number /= 10
    }while number > 0
    return isNegative ? ("menos" + output) : output
}
//Ejercicio
let numbersIntegers = [-1, 2, 34, 4566, -755]
let numberStrings2 = numbersIntegers.map({ (number) -> String in
    var number = number
    var output = ""
    var isNegative = false
    if number < 0
    {
        number *= -1
        isNegative = true
    }
    repeat
{
        output = digitNames[number % 10]! + output
        number /= 10
        
    }while number > 0
    
    output = isNegative ? "-" + output : output
    
    return output
})
//OUTPUT: ["-Uno", "Dos", "TresCuatro", "CuatroCincoSeisSeis", "-SieteCincoCinco"]

Dejo otra solucion

//Ejemplo
let digitNumbers = [0: "Cero", 1: "Uno", 2: "Dos", 3: "Tres", 4: "Cuatro",
                    5: "Cinco", 6: "Seis", 7: "Siete", 8: "Ocho", 9: "Nueve"]

let numbers : [Int] = [16, 58, 239, 1988, -204]

let numberStrings = numbers.map { (number) -> String in
    
    var (number, symbol) = number < 0 ? (number * -1, "menos") : (number, "")

    var output = ""
    repeat {
        output = digitNumbers[number % 10]! + output
        number /= 10
    } while number > 0
    return symbol + output
}
let digitName = [0:"Zero",1:"One",2:"Two",3:"Three",4:"Four",5:"Five",6:"Six",7:"Seven",8:"Eight",9:"Nine"]
let numbers = [2,58,510,2127,-50,-5789]

let numberStrings = numbers.map{(number) -> String in
    var number = number
    var output = ""
    var isNegative = false
    if number < 0 {
        number *= -1
        isNegative = true
    }
    repeat {
        output = digitName[number%10]! + output
        number /= 10
    } while number > 0
    return (isNegative ? "-" : "").appending(output)
}
print(numberStrings)

// Challenge

let numbers = [-16, -58, -2127, 99, 88]

let numbersStrings = numbers.map{ (number) -> String in
    var number = number
    var output = ""
    let isNegative = number < 0 ? true : false
    number = number > 0 ? number : abs(number)
    repeat{
        output = digitNames[number%10]! + output
        number /= 10
    }while number > 0
    output = isNegative ? "\(output) negativo" : output
    return output
}