This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def balance(chars: List[Char]): Boolean = { | |
@tailrec | |
def go(acc: Int, chars: List[Char]): Boolean = { | |
if (acc < 0) false | |
else { | |
if (chars.isEmpty) (acc == 0) | |
else { | |
val newAcc = chars.head match { | |
case '(' => acc + 1 | |
case ')' => acc - 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def solution(A: Array[Int]): Int = { | |
@tailrec | |
def go(which: Int): Int = { | |
if ((A.size < which) || (A.size < 2)) -1 | |
else { | |
val left = A.take(which) | |
val right = A.takeRight(A.size - which - 1) | |
if (left.sum == right.sum) which else go(A, which + 1) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def simpleTest(s:String) = { | |
val env = System.getenv(s) | |
if (env!=null) println(env) | |
} | |
def simpleTest(s:String) = | |
System.getenv(s) match { | |
case null => ; | |
case s:String => println(s) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def slice[T](from: Int, to: Int, ts: Seq[T]): Seq[T] = { | |
def between(at: Int): Boolean = (at >= from) && (at < to) | |
ts.zipWithIndex.filter(e => between(e._2)).map(_._1) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def removeAt[T] = (n: Int, ts: Seq[T]) => | |
(ts.zipWithIndex.filter(_._2 != n).map(_._1), ts(n)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DroppedSeq[A](t: Seq[A]){ | |
def myDrop(n: Int): Seq[A] = t.foldRight[Seq[A]](Seq()) { | |
(e,acc) => if (n>acc.size) e+:acc else acc | |
} | |
} | |
implicit def SeqToDroppedSeq[A](s: Seq[A]) = new DroppedSeq[A](s) | |
//> SeqToDroppedSeq: [A](s: Seq[A])tmp.DroppedSeq[A] | |
Seq(1, 2, 3, 4).myDrop(2) //> res0: Seq[Int] = List(3, 4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import annotation.tailrec | |
@tailrec | |
def foldLeft[A](ts: Seq[A], first: A)(f: (A, A) => A): A = ts match { | |
case Nil => first | |
case e +: init => foldLeft(init, f(first, e))(f) | |
} | |
foldLeft(List("A", "B", "C"), "D")(_ + _) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pack[T](ts: Seq[T]): Seq[Seq[T]] = { | |
if (ts.isEmpty) Seq[Seq[T]]() | |
else | |
ts.init.foldRight(Seq(Seq(ts.last)))((e, acc) => | |
if (e != acc.head.head) Seq(e) +: acc | |
else (acc.head :+ e) +: acc.tail) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def flatten(ls: Seq[Any]): Seq[Any] = | |
ls.foldLeft[Seq[Any]](Seq())((acc, e) => e match { | |
case s: Seq[_] => acc ++ flatten(s) | |
case e => acc ++ Seq(e) | |
}) | |
def flatten(ls: Seq[Any]): Seq[Any] = | |
ls.foldLeft(Seq[Any]())((acc, e) => e match { | |
case s: Seq[_] => acc ++ flatten(s) | |
case e => acc ++ Seq(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def combinations[T](count: Int, ts: Seq[T]): Seq[Seq[T]] = { | |
def extendGenerated(generated: Seq[T], unused: Seq[T]): Seq[Seq[T]] = (generated, unused) match { | |
case (l, r) if (l.length + r.length < count) => Nil | |
case (l, r) if (l.length == count) => Seq(l) | |
case (l, r) => r flatMap (e => { | |
val newGenerated = l :+ e | |
val newUnused = r.dropWhile(_ != e).tail | |
extendGenerated(newGenerated, newUnused) | |
}) | |
} |
NewerOlder