Advertisement
cardel

Solucion examen 07 de DIc

Dec 7th, 2023
1,593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.29 KB | None | 0 0
  1. /**
  2. * Solución primer punto
  3. */
  4. import scala.annotation.tailrec
  5. object Punto1 {
  6.  
  7.   def zipf(f:(Int,Int)=>Int, lstA:List[Int], lstB:List[Int]):List[Int] = {
  8.     @tailrec
  9.     def zipfAux(f:(Int,Int)=>Int, lstA:List[Int], lstB:List[Int], acc:List[Int]):List[Int] = {
  10.       if (lstA.isEmpty || lstB.isEmpty) acc
  11.       else zipfAux(f, lstA.tail, lstB.tail, acc :+ f(lstA.head, lstB.head))
  12.     }
  13.     zipfAux(f, lstA, lstB, List())
  14.      
  15.   }
  16.  
  17.   def main(args: Array[String]): Unit = {
  18.     println(zipf((x,y)=>x+y, List(1,2,3), List(2,4,6)))
  19.     println(zipf((x,y)=>x*y, List(1,2,3), List(2,4,6)))
  20.   }
  21. }
  22.  
  23.  
  24. /**
  25.  * Punto 2 examen opcional
  26. */
  27.  
  28. object Punto2 {
  29.   type Arbol = List[Any]
  30.  
  31.   def path(arb:Arbol, x:Int):List[String] = {
  32.     arb match {
  33.       case Nil => Nil
  34.       case List(v,izq,der) => {
  35.         if (x < v.asInstanceOf[Int]) "izq"::path(izq.asInstanceOf[List[Any]],x)
  36.         else if (x > v.asInstanceOf[Int]) "der"::path(der.asInstanceOf[List[Any]],x)
  37.             else Nil //Tambien puede ser case _
  38.       }
  39.       case _ => Nil //Warning
  40.          
  41.     }
  42.   }
  43.  
  44.   def main(args: Array[String]): Unit = {
  45.       val a = List(4,List(2, List(1, List(), List()), List(3, List(), List())),List(6, List(), List()))
  46.       println(path(a, 4))
  47.       println(path(a, 3))
  48.     }
  49. }
  50.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement