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 = { | |
def solution(left: Long, right: Long, index: Int): Int = index match { | |
case _ if left == right => | |
index | |
case x if x == a.length -1 => | |
-1 | |
case _ => | |
solution(left + a(index), right - a(index + 1), index + 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
object Euler04 { | |
def largestPalindrome(digits: Int): Long = { | |
val min = Math.pow(10, digits - 1).toInt | |
val max = min * 10 - 1 | |
def largestPalindrome(x: Long, y: Long, acc: Long): Long = (x, y) match { | |
case (`max`, `max`) => acc | |
case (_, `max`) => | |
val newAcc = computeAcc(x, y, acc) |
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
object P04 { | |
def groupIndexed[T](ts: Seq[(T, Int)]): Map[T, Seq[Int]] = { | |
val (toMap, size) = convertToMapAndSize(ts) | |
toMap.map { | |
case (t, seq) => | |
val range = (1 to size).map(i => if (seq.contains(i)) 1 else 0) | |
(t, range) | |
} | |
} |
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
object P04 { | |
def groupIndexed[T](ts: Seq[(T, Int)]): Map[T, Seq[Int]] = { | |
val (toMap, max) = splitToMap(ts) | |
toMap.map { | |
case (t, seq) => | |
val array = new Array[Int](max) | |
seq.foreach { i => | |
array(i - 1) = 1 | |
} | |
(t, array.toSeq) |
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
object Grouped { | |
def grouped[A](ts: Seq[A], s: Int): Iterator[Seq[A]] = new Iterator[Seq[A]] { | |
private var tsSeq: Seq[A] = ts | |
override def next(): Seq[A] = { | |
val (h, t) = tsSeq.splitAt(s) | |
tsSeq = t | |
h |
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 filterNot[A](ts: Seq[A])(p: (A) ⇒ Boolean): Seq[A] = { | |
val reverted = ts.foldLeft[Seq[A]](Seq()) { | |
(acc, el) => if (p(el)) acc else el +: acc | |
} | |
reverted.reverse | |
} |
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
object Drop { | |
@tailrec | |
def drop[A](as: Seq[A], n: Int): Seq[A] = as match { | |
case Seq() => Seq() | |
case h +: t if n > 0 => drop(t, n - 1) | |
case _ => as | |
} | |
} |
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
object S99_P14 { | |
def duplicate[T](as: Seq[T]): Seq[T] = as.flatMap(el => Seq.fill(2)(el)) | |
} | |
object S99_P14 { | |
def duplicate[T](as: Seq[T]): Seq[T] = { | |
val reversed = as.foldLeft[Seq[T]](Nil) { | |
(acc, el) => el +: (el +: acc) | |
} | |
reversed.reverse |
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
object S99_P26 { | |
def combinations[T](count: Int, ts: Seq[T]): Seq[Seq[T]] = { | |
def addHeadToSeqs[T](tss: Seq[Seq[T]], elt: T): Seq[Seq[T]] = | |
tss.map(ts => elt +: ts) | |
ts match { | |
case h +: tail if count == 1 => | |
ts.flatMap(e => Seq(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
object S99_P10 { | |
def encode[T](ts: Seq[T]): Seq[(Int, T)] = { | |
val (_, _, l, last) = ts.foldRight[(Int, Option[T], Seq[(Int, T)], (Int, Option[T]))]((0, None, Nil, (0, None))) { | |
(el, q) => | |
val (nr, option, acc, _) = q | |
option match { | |
case None => (1, Some(el), acc, (1, Some(el))) | |
case Some(x) => if (x == el) { | |
(nr + 1, Some(el), acc, (nr + 1, Some(el))) | |
} else { |
NewerOlder