import Foundation
func maxOpt(_ x: Int?, _ y: Int?) -> Int? {
    
    // Equivalente a tu: if (x != nil)
    // Intentamos extraer el valor de 'x' y guardarlo en 'valorX'
    if let valorX = x {
        
        // Si entramos aquí, 'x' NO es nil. 
        // Ahora hacemos lo mismo con 'y' (Equivalente a tu: if (y != nil))
        if let valorY = y {
            
            // Ambos tienen valor, usamos las constantes seguras extraídas
            return max(valorX, valorY)
            
        } else {
            // 'x' tiene valor, pero 'y' es nil
            return valorX
        }
        
    } else {
        // Si llegamos aquí, 'x' ES nil.
        // Comprobamos si 'y' tiene valor.
        if let valorY = y {
            return valorY
        } else {
            // Ambos son nil (Equivalente a tu primer if)
            return nil
        }
    }
}
let res1 = maxOpt(nil, nil)  
let res2 = maxOpt(10, nil)
let res3 = maxOpt(-10, 30)
print("res1 = \(String(describing: res1))") // res1 = nil
print("res2 = \(String(describing: res2))") // res2 = Optional(10)
print("res3 = \(String(describing: res3))") // res3 = Optional(30)
//b
func parejaMayorParImpar2(numeros: [Int]) -> (Int?, Int?) {
    // Caso base
    if numeros.isEmpty {
        return (nil, nil)
    }
    
    let primero = numeros[0]
    let resto = Array(numeros[1...])
    
    // CORRECCIÓN 1: Los nombres de las variables coinciden con los del return
    // CORRECCIÓN 2: El nombre de la función empieza por minúscula
    let (maxImparResto, maxParResto) = parejaMayorParImpar2(numeros: resto)
    
    if primero % 2 == 0 {
        return (maxImparResto, maxOpt(primero, maxParResto))
    // CORRECCIÓN 3: Eliminada la llave extra que rompía el 'else'
    } else {
        return (maxOpt(primero, maxImparResto), maxParResto)
    }
}

func sumaMaxParesImpares(numeros: [Int]) -> Int {
    // 1. Llamamos a la función del apartado b1 (ahora el nombre coincide)
    let (maxImpar, maxPar) = parejaMayorParImpar2(numeros: numeros)
    
    // 2. Desempaquetamos con valores por defecto
    let valorImpar = maxImpar ?? 0
    let valorPar = maxPar ?? 0
    
    // 3. Devolvemos la suma de los valores seguros
    return valorImpar + valorPar
}

let arrayPrueba = [-10, 202, 12, 100, 204, 2]
print(sumaMaxParesImpares(numeros: arrayPrueba)) // Imprimirá 204

//3

func suma(palabras: [String], contienen: Character) -> Int{
    palabras.filter({$0.contains(contienen)}).map({$0.count}).reduce(0, +)
}
let listaDePalabras = ["swift", "programacion", "funcional", "clausura", "opcional"]
let caracterABuscar: Character = "o"

let resultado = suma(palabras: listaDePalabras, contienen: caracterABuscar)
print("El resultado esperado es 29. Tu resultado es: \(resultado)")
//c.2
func sumaMenoresMayores(nums: [Int], pivote: Int) -> (Int, Int){
    return(nums.filter({$0 < pivote}).reduce(0,+),nums.filter({$0 >= pivote}).reduce(0,+))
}
enum Arbol<T>{
    case nodo(T,[Arbol<T>])
}
func toArray<T>(_ arbol : Arbol<T>) -> [T]{
    switch arbol{
        case .nodo(let dato, let hijos):
            return [dato] + bosqueToArray(hijos)
    }
}
func bosqueToArray<T>(_ bosque: [Arbol<T>]) -> [T]{
    if bosque.isEmpty{
        return[]
    }
    let primero = bosque[0]
    let resto = Array(bosque[1...])
    return toArray(primero) + bosqueToArray(resto)
}
func toArrayFOS<T>(_ arbol: Arbol<T>) -> [T] {
        switch arbol{
        case .nodo(let dato, let hijos):
            return [dato] + hijos.flatMap{toArrayFOS($0)}
    }
}
func imprimirListadoAlumnos(_ alumnos: [(String, Double, Double, Double, Int)]) {
    print("Alumno   Parcial1   Parcial2   Parcial3  Años")
    for alu in alumnos {
        alu.0.withCString {
            print(String(format:"%-10s %5.2f      %5.2f    %5.2f  %3d", $0, alu.1, alu.2, alu.3, alu.4))
        }
    }
}
func imprimirListadosNotas(alumnos: [(String, Double, Double, Double, Int)]) {
    imprimirListadoAlumnos(alumnos.sorted { $0.0 < $1.0 })
    imprimirListadoAlumnos(alumnos.sorted { $0.1 < $1.1 })
    imprimirListadoAlumnos(alumnos.sorted { $0.2 < $1.2 })
    imprimirListadoAlumnos(alumnos.sorted { if $0.4 != $1.4 {return $0.4 > $1.4}
                                          return $0.3 > $1.3})
    imprimirListadoAlumnos(alumnos.sorted {
    let notaA = ($0.1 * 0.35) + ($0.2 * 0.3) + ($0.3 * 0.35)
    let notaB = ($1.1 * 0.35) + ($1.2 * 0.3) + ($1.3 * 0.35)
    return notaA > notaB})
}
//6
func construye(operador: Character) -> (Int, Int) -> Int{
    switch operador{
    case "+":
        return { $0 + $1 }
    case "-":
        return { $0 - $1 }
    case "*":
        return { $0 * $1 }
    case "/":
        return { $0 / $1 }
    default:
        return { _, _ in 0 }}
    }
}


Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: