No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

No se trata de lo que quieres comprar, sino de quién quieres ser. Invierte en tu educación con el precio especial

Antes: $249

Currency
$209

Paga en 4 cuotas sin intereses

Paga en 4 cuotas sin intereses
Suscríbete

Termina en:

12 Días
8 Hrs
58 Min
41 Seg

Trailing Closures

10/16
Recursos

Aportes 47

Preguntas 1

Ordenar por:

¿Quieres ver más aportes, preguntas y respuestas de la comunidad?

// 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)

[“menosNueve”, “TresCuatro”, “menosDosTres”, “menosDosUnoDosSiete”, “CuatroSeis”, “menosSeis”]

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!

Por aca dejo mi implementación ```js let numbersString = numbers.map{ (number) -> String in var numberTem = number < 0 ? -1*number : number var output = "" repeat{ output = digitNames[numberTem%10]! + output numberTem /= 10 }while numberTem > 0 return number < 0 ? "Menos"+output : output } ```
```js let digitNames = [0:"Zero",1: "One",2:"Two",3:"Three",4:"Four",5:"Five",6:"Six",7:"Seven",8:"Eight",9:"Nain"] let numbers = [16,58,510,-2127] let numbersString = numbers.map { (number) -> String in var isNegative = number < 0 var numberEdittable = isNegative ? (number * -1) : number var output = "" repeat{ output = digitNames[numberEdittable%10]! + output numberEdittable /= 10 }while numberEdittable > 0 if isNegative { output.insert("-", at: output.startIndex) } return output } ```**let** digitNames = \[0:"Zero",1: "One",2:"Two",3:"Three",4:"Four",5:"Five",6:"Six",7:"Seven",8:"Eight",9:"Nain"] **let** numbers = \[16,58,510,-2127] **let** numbersString = numbers.map { (number) -> String **in** **var** isNegative = number < 0 **var** numberEdittable = isNegative ? (number \* -1) : number **var** output = "" **repeat**{ output = digitNames\[numberEdittable%10]! + output numberEdittable /= 10 }**while** numberEdittable > 0 **if** isNegative { output.insert("-", at: output.startIndex) } **return** output }

Dejo mi Aporte

let digitNames2 = [0: "Cero", 1: "Uno", 2: "Dos", 3: "Tres", 4: "Cuatro",
                  5: "Cinco", 6:"Seis", 7: "Siete", 8: "Ocho", 9: "Nueve"]
let numbers2 = [-5, -16, 58, 510, -2127]
let numberString2 = numbers2.map { (number) -> String in
    var number = number
    var output = ""

    if number < 0{
        number *= -1
        repeat{
            output = digitNames2[number%10]! + output
            number /= 10
        
        } while number > 0
        output = "menos" + output
        return output
        
    } else {
        repeat{
            output = digitNames2[number%10]! + output
            number /= 10
        
        } while number > 0
        return output
    }
    
}
print()
print("*******")
print(numberString2)
Esta muy bueno swift, se parece mucho a Kotlin
![](https://static.platzi.com/media/user_upload/image-09d88fc9-63a9-47e4-879b-a9d0cfa710b8.jpg)
```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 ", “dos 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
}