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 math.max | |
/* | |
James is a businessman. He is on a tight schedule this week. The week starts | |
on Monday at 00:00 and ends on Sunday at 24:00. His schedule consists of M | |
meetings he needs to take part in. Each of them will take place in a period of | |
time, beginning and ending on the same day (there are no two ongoing meetings | |
at the same time). James is very tired, thus he needs to find the longest | |
possible time slot to sleep. In other words, he wants to find the longest | |
period of time when there are no ongoing meetings. The sleeping break can |
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) |
NewerOlder