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 java.text.SimpleDateFormat | |
import java.time.Instant | |
import java.util.Date | |
import java.util.concurrent.TimeUnit | |
import TimeUnit.{MILLISECONDS => Milliseconds} | |
import TimeUnit.{SECONDS => Seconds} | |
import scala.annotation.tailrec | |
/* |
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 scala.annotation.tailrec | |
/* | |
Assassin | |
-------- | |
Write a method that given a board, `b`, with obstacles, guards, and an assassin, will determine if said assassin can | |
reach the bottom right undetected: | |
object Assassin { | |
def undetected(b: Array[String]): Boolean |
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 ArrayRotation { | |
def rotate(a: Array[Int], k: Int): Array[Int] = { | |
val n = a.length | |
lazy val kRn = k % n | |
if (n == 0 || kRn == 0) | |
a | |
else | |
Array.tabulate(n) { i => | |
val j = i - kRn |
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
/* Divisible by 3 | |
-------------- | |
Given a string representation of a number, returns the number of variations of said input that is divisible by 3. The variations consist of changing one digit for each variation. | |
For example, "21"'s variations are: | |
01 | |
11 | |
21 | |
31 |
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
// prime: cannot be divided by any number other than itself and 1 //import cats.data.State | |
trait Prime0 { | |
def isPrime(n: Int): Boolean | |
def countPrimes(n: Int): (Int, Prime0) //trait Prime | |
} | |
object Prime0 extends App { | |
private val InitPrimeCount = Seq(0, 0, 1, 2) | |
//object Prime | |
def apply(): Prime0 = new PrimeImpl(InitPrimeCount) |
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 cats.data.State | |
import cats.syntax.applicative._ // for pure | |
object PostOrderCalc { | |
type CalcState[A] = State[List[Int], A] | |
def evalInput(str: String): Int = | |
evalAll(str.split(" ").toList).runA(Nil).value | |
def evalAll(input: List[String]): CalcState[Int] = |
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
trait Monoid[A] { | |
def op(a1: A, a2: A): A | |
def zero: A | |
} | |
val intAddition = new Monoid[Int] { | |
def op(int1: Int, int2: Int) = int1 + int2 | |
def zero = 0 | |
} |
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 PyramidWins extends App { | |
private def pdf(v: IndexedSeq[Int]) = | |
v.groupBy(identity).map { pair => | |
(pair._1, pair._2.size / v.size.toFloat) | |
} | |
private val pyramids = pdf(for { | |
p1 <- 1 to 4 | |
p2 <- 1 to 4 | |
p3 <- 1 to 4 | |
p4 <- 1 to 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 | |
object Mults3or5sum extends App { | |
private val limit = 1000 | |
private def mults(multOf: Int) = { | |
@tailrec | |
def mults(i: Int, acc: Set[Int]): Set[Int] = { | |
val mult = multOf * i | |
if (mult >= limit) acc | |
else mults(i + 1, acc + mult) |
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 P100 extends App { | |
private def p(bal: Int, summand: Int, m: Map[(Int, Int), Int]): (Int, Map[(Int, Int), Int]) = | |
if (bal < 0 || summand == 100) (0, m) | |
else if (bal == 0) (1, m) | |
else { | |
val (nSumsWithSummand, m1) = p0(bal - summand, summand, m) | |
val (nSumsWithoutSummand, m2) = p0(bal, summand + 1, m1) | |
(nSumsWithSummand + nSumsWithoutSummand, m2) | |
} | |
private def p0(b: Int, s: Int, m: Map[(Int, Int), Int]) = |
NewerOlder