Skip to content

Instantly share code, notes, and snippets.

@nbogie
Created August 17, 2011 22:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nbogie/1152830 to your computer and use it in GitHub Desktop.
Save nbogie/1152830 to your computer and use it in GitHub Desktop.
scala 99 solns poor
object P1 {
def sampleList() = List(1, 10, 100, 2, 20, 200, 5)
def last[A](xs: List[A]): A =
xs match {
case Nil => sys.error("last on empty list")
case x :: Nil => x
case x :: rest => last(rest)
}
def lastButOne[A](xs: List[A]): A =
xs match {
case Nil => sys.error("lastButOne called on empty list")
case x :: Nil => sys.error("lastButOne called on singleton list")
case x :: y :: Nil => x
case x :: rest => lastButOne(rest)
}
def kthElem[A](i: Int, xs: List[A]): A =
(i, xs) match {
case (q, Nil) => sys.error("Index out of range for kthElem")
case (0, x :: rest) => x
case (n, x :: rest) => kthElem(n - 1, rest)
}
def length[A](xs:List[A]):Int =
xs match {
case Nil => 0
case x::rest => 1+(length(rest))
}
def reverse[A](xs:List[A]):List[A] =
xs match {
case Nil => Nil
case x::rest => reverse(rest) ++ List(x) //performance
}
def isPalindrome[A](xs:List[A]):Boolean =
reverse(xs) == xs
def flatten[A](xs:List[A]):List[A] =
xs match {
case Nil => Nil
case (x:List[A])::rest => flatten(x) ++ flatten(rest)
case x::rest => x :: flatten(rest)
}
val nestedSample: List[Any] = List(1,List(2,List(3,4),5), List(), List(6))
def compress[A](xs:List[A]):List[A] =
xs match {
case Nil => Nil
case (x::y::rest) if x == y => compress(y::rest)
case (x::y::rest) => x::compress(y::rest)
//case List(x) => List(x) //using this form leaves unexhausted match, c.f. x::rest
case (x::rest) => List(x)
}
def sampleForCompress = List('a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)
def compressToSublists[A](xs:List[A]) : List[List[A]] =
xs match {
case Nil => Nil
case (x::rest) => comp(rest, List(x))
}
def comp[A](xs:List[A], ys:List[A]):List[List[A]] =
(xs,ys) match {
case (Nil,ys) => List(ys)
case (a::as, Nil) => comp(as, List(a))
case (a::as, b::bs) if a==b => comp(as, a::b::bs)
case (a::as, b::bs) => (b::bs) :: comp(as, List(a))
}
def pack[A](xs:List[A]):List[List[A]] =
xs match {
case (x::rest) =>
val (same, different) = xs.span(_ == x)
same::pack(different)
case Nil => Nil
}
def demoList2 = List('a, 'a, 'a, 'a, 'b, 'c, 'c, 'a, 'a, 'd, 'e, 'e, 'e, 'e)
//P10
def encode[A](xs:List[A]):List[(Int, A)] = {
def f[A](ys:List[A]):(Int, A) = ys match {
case y::rest => (ys.length, y)
case Nil => sys.error("pack returned empty list")
}
pack(xs).map(f)
}
def encode1[A](xs:List[A]):List[(Int, A)] =
pack(xs).map( (ys:List[A]) => ys match {
case y::rest => (ys.length, y)
case Nil => sys.error("pack returned empty list")
})
//P11
def encodeModified[A](xs:List[A]):List[Any] =
encode(xs).map(ys => ys match {
case (1, v) => v
case other => other
})
def demoEncoded = List((4, 'a), (1, 'b), (2, 'c), (2, 'a), (1, 'd), (4, 'e))
def decode[A](ps:List[(Int, A)]) : List[A] =
ps match {
case (n,v)::rest => repeat(n, v)::: decode(rest)
case Nil => Nil
}
def repeat[A](i:Int,v:A) : List[A] = i match {
case 0 => Nil
case n => v::repeat(n-1,v)
}
def encodeDirect[A](xs:List[A]) : List[(Int,A)] = {
def enc[A](ls:List[A], pair:(Int,A)) : List[(Int, A)] =
(ls,pair) match {
case (Nil, p) => List(p)
case (y::ys, (i, cur)) if y==cur => enc(ys,(i+1, cur))
case (y::ys, (i, cur)) => (i, cur)::enc(ys, (1,y))
}
xs match {
case Nil => Nil
case x::rest => enc(rest, (1, x))
}
}
/*
encodeDirect :: (Eq a) => [a] -> [(Int, a)]
encodeDirect [] = []
encodeDirect (x:xs) = enc xs (1, x)
enc [] pair = [pair]
enc (x:xs) (i, y) | x == y = enc xs (i+1, y)
| otherwise = (i,y) : enc xs (1, x)
*/
def duplicate[A](xs:List[A]) : List[A] =
xs.flatMap(x => List(x,x))
//p15
def duplicateN[A](xs:List[A], count:Int) : List[A] = {
def dup[A](v:A, n:Int) : List[A] = n match {
case 0 => Nil
case other => v::dup(v,other - 1)
}
xs.flatMap { x => dup(x,count) }
}
//p16
def dropNth[A](ls:List[A], nth:Int) : List[A] = {
def d(ls:List[A],counter:Int) : List[A] = (ls, counter) match {
case (Nil, _) => Nil
case (x::xs, 0) => d(xs, nth - 1)
case (x::xs, c) => x::d(xs, c - 1)
}
d(ls,nth - 1)
}
//p17
def myspan[A](n:Int, ls:List[A]) : (List[A], List[A]) = {
def sp[A](c:Int, lefts:List[A], xs:List[A]) : (List[A], List[A]) = (c,xs) match {
case (_,Nil) => (lefts, Nil)
case (0,_) => (lefts, xs)
case (i,y::ys) => sp(i-1, lefts:::List(y), ys) //append bad
}
sp(n,List(),ls)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment