Advertisement
cardel

Solucion examen 05 de DIc

Dec 5th, 2023
1,513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.94 KB | None | 0 0
  1. /**
  2.  * Punto 1 del opcional de programación funciona
  3.  */
  4.  
  5. object Punto1 {
  6.  
  7.   def inversiones(lst:List[Int]):Int = {
  8.     def inversionesAux(lst:List[Int], acc:Int):Int = lst match {
  9.       case Nil => acc
  10.       case x::xs => inversionesAux(xs, acc + xs.filter(_ < x).length)
  11.     }
  12.     inversionesAux(lst, 0)
  13.   }
  14.  
  15.   def main(args: Array[String]): Unit = {
  16.     println(inversiones(List(2,3,8,6,1)))
  17.     println(inversiones(List(1,2,3,4)))
  18.     println(inversiones(List(3,2,1)))
  19.   }
  20. }
  21. /**
  22.  * Punto 2 del examen opcional
  23.  */
  24.  
  25. object Punto2{
  26.  
  27.   def flatten(lst:List[Any]):List[Int] = {
  28.     lst match{
  29.       case Nil => Nil
  30.       case h::t =>
  31.         h match{
  32.           case x:Int => x::flatten(t)
  33.           case x:List[Any] => flatten(x):::flatten(t)
  34.         }
  35.     }
  36.      
  37.   }
  38.  
  39.   def main(arr:Array[String]){
  40.     println(flatten(List(2,List(3,4),8,6,1)))
  41.     println(flatten(List(1,2,List(3,List(4,List(5,6))))))
  42.   }
  43. }
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement