A煤n no tienes acceso a esta clase

Crea una cuenta y contin煤a viendo este curso

Trailing Closures

10/16
Recursos

Aportes 27

Preguntas 0

Ordenar por:

驴Quieres ver m谩s aportes, preguntas y respuestas de la comunidad? Crea una cuenta 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!

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
}