Skip to content

Instantly share code, notes, and snippets.

@jagbolanos
Created March 23, 2013 16:21
Show Gist options
  • Save jagbolanos/5228293 to your computer and use it in GitHub Desktop.
Save jagbolanos/5228293 to your computer and use it in GitHub Desktop.
//Examen Final
//6 Oro Devuelve el elemento maximo de una lista
//e.g. maximo (List(3,4,1,2,9,11,7,5,0)) => 11
def maximoHelper(l:List[Int], max:Int) : Int = l match {
case Nil => max
case x :: xs => if (x > max) maximoHelper(xs, x) else maximoHelper(xs, max)
}
def maximo(l:List[Int]) : Int = l match {
case Nil => 0
case x :: xs => maximoHelper (xs, x)
}
//6 Oro Quita los elementos cuyo valor son múltiplos de 3
//e.g. quitarmultiposde3 (List(3,4,6,2,9,12,7,5,0)) => List(4,2,7,5,0)
def quitarmultiposde3(l:List[Int]) : List[Int] = l match {
case Nil => Nil
case x :: xs => if (x % 3 == 0) quitarmultiposde3(xs) else x :: quitarmultiposde3(xs)
}
//8 Oro Quita n elementos de la lista l a partir del indice i
//e.g. quitarsubsecuencia (List(3,4,6,2,9,12,7,5,0), 2, 3) => List(3,4,12,7,5,0)
def quitarsubsecuencia(l:List[Int], i:Int, n:Int) : List[Int] = (l, i, n) match {
case (Nil, _, _) => Nil
case (_, 0, 0) => l
case (x :: xs, 0, _) => if (n > 0) quitarsubsecuencia(xs, 0, n-1) else l
case (x :: xs, _, _) => if (i > 0) x :: quitarsubsecuencia(xs, i-1, n) else l
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment