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 / P100.scala
Last active February 4, 2022 17:25
The partition function at 100
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]) =
import util.Random.nextInt
trait RandomMap[K, V] {
def + (kv: (K, V)): RandomMap[K, V]
def apply(k: K): V
def remove(k: K): RandomMap[K, V]
def random: V
}
object RandomMap {
@mayonesa
mayonesa / ContactTrie.scala
Last active December 11, 2017 17:08
Counts the number of names that start with the token provided
import scala.annotation.tailrec
import System.in
import java.util.Scanner
object Solution {
private val ops = Map("add" -> add _, "find" -> find _)
def main(args: Array[String]): Unit = {
val sc = new Scanner(in)
var n = sc.nextInt()
@mayonesa
mayonesa / FreeSched.scala
Last active February 4, 2022 17:26
Given individual schedules and the endpoints of time, it will return all the free times.
import scala.collection.parallel.immutable.ParRange
import scala.collection.GenSeq
object Solution extends App {
private val NoTimes = Seq.empty[(Int, Int)]
private val NoStart = Option.empty[Int]
private val StdListStart = "["
private val StdListEnd = "]"
private val Sample = (-5, 60, Seq(Seq((0, 5), (15, 30), (45, 60)), Seq((5, 15), (30, 40))))
private val stdin = "" // """\[(\d+), (\d+)\]""".r
@mayonesa
mayonesa / BST.scala
Last active February 4, 2022 17:26
package prep
class BST[E <: Ordered[E]](root: Node[E]) {
def this() = this(Empty())
lazy val seq = root.seq
def + (e: E): BST[E] = new BST(root + e)
def smallest(k: Int): E = seq(seq.size - k)
}
import MyMap.Bucket
class MyMap[K, V](buckets: Vector[Bucket[K, V]]) {
def + (kv: (K, V)) = updated(kv._1, _ + kv)
def - (k: K) = updated(k, _ - k)
def get(k: K) = getBucket(k).get(k) map (_._2)
def contains(k: K) = getBucket(k) contains k
private def updated(k: K, f: Bucket[K, V] => Bucket[K, V]) = {
val i = index(k)
@mayonesa
mayonesa / Ride.scala
Last active February 4, 2022 17:26
Driver w/ the longest chain of rides where a chain consists of rides less than 10 minutes apart
import java.time.ZonedDateTime
import Math.max
case class Ride(customerId: Long, driverId: Long, start: ZonedDateTime, stop: ZonedDateTime)
object Ride {
private implicit val zonedDateTimeOrdering: Ordering[ZonedDateTime] = Ordering.fromLessThan(_ isBefore _)
def longestCustomerChainDriverId(rides: Seq[Ride]): Long = {
def maxDriverChain(acc: (Option[Long], Int), driverRides: (Long, Seq[Ride])) = {
package forcomp
object Anagrams {
/** A word is simply a `String`. */
type Word = String
/** A sentence is a `List` of words. */
type Sentence = List[Word]
from math import atan2, cos, sin, sqrt, pi
from robot import angle_trunc
import sys
'''
Created on Jun 2, 2017
@author: john_jimenez
'''
sys.setrecursionlimit(10000)
from math import atan2, cos, sin, sqrt, pi
from robot import angle_trunc
'''
pr1.3
Created on May 26, 2017
@author: john jimenez (jjimenez36)
'''