Skip to content

Instantly share code, notes, and snippets.

View mayonesa's full-sized avatar
🇺🇦

John Jimenez mayonesa

🇺🇦
  • Florida, USA
View GitHub Profile
@mayonesa
mayonesa / Week.scala
Created February 6, 2022 22:40
Find the longest period of time when there are no ongoing meetings
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
@mayonesa
mayonesa / InterestingTimes.scala
Created February 6, 2022 22:35
Time using at most 2 numbers
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
/*
@mayonesa
mayonesa / Assassin.scala
Last active February 6, 2022 22:31
Given a board, `b`, with obstacles, guards, and an assassin, will determine if said assassin can reach the bottom right undetected
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
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
@mayonesa
mayonesa / DivBy3.scala
Last active February 6, 2022 22:25
Given a string representation of a number, returns the number of variations of said input that is divisible by 3
/* 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
@mayonesa
mayonesa / Prime.scala
Last active February 4, 2022 17:23
Prime and count
// 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)
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] =
@mayonesa
mayonesa / Monoids.scala
Created July 14, 2019 01:31
Monoids, Functional Programming in Scala
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
}
@mayonesa
mayonesa / PyramidWins.scala
Last active February 4, 2022 17:25
Probability of the result of 9 4-sided (pyramidal) dice (faces: 1 -> 4) is greater than the result of 6 6-sided (cubic) dice (faces: 1 -> 6)
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
@mayonesa
mayonesa / Mults3or5.scala
Last active February 4, 2022 17:25
Sum of all the multiples of 3 or 5 (up to 1000)
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)