Skip to content

Instantly share code, notes, and snippets.

@varmas
varmas / dp-study-plan.md
Last active July 28, 2022 19:26
Effective LC

From https://leetcode.com/discuss/general-discussion/475924/my-experience-and-notes-for-learning-dp/427348

A littile bit of my history of learning DP

DP has always been an obstacle when preparing for interviews. For me it is one of the hardest topic. There were several times in the past that I tried to master it, but all attempts failed. Either because I could not find good resources, or because I did not have enough time to really dive into it, have a lot of practice, and identify different patterns. To tell the truth, I even feared that I would never be able to understand it well.

This winter I had another attempt, and made up my mind to grasp the techique. I solved/read 45 DP problems of different patterns in 4 days (yes, you might think that is quite slow). At the begining, I struggled as much as all my previous attempts, but slowly I found I am getting better and I start to be able to think in the DP-way. Today I solved several problems independently, with memoization and tabulation and even spac

@varmas
varmas / CSVParserSpec.scala
Created November 15, 2019 15:00
csv spec test
import java.io.FileReader
import com.opencsv.enums.CSVReaderNullFieldIndicator
import com.opencsv.{CSVReader, CSVReaderBuilder}
import org.scalatest.{FlatSpec, Matchers}
import scala.collection.JavaConverters._
class CSVParserSpec extends FlatSpec with Matchers {
val names = List("Son Goku",
"Bulma",
"Master Roshi",
"Yamcha",
"Krillin",
"Tien Shinhan",
"Piccolo",
"Son Gohan",
"Vegeta",
"Trunks",
@varmas
varmas / ExceptionWrapper2.scala
Created August 23, 2019 17:07
exception wrapper 2
def errorWrapper[T](message: String, fn: => T): T = {
try {
logger.warn(s"Attempting [$message]")
fn
} catch {
case ex: Throwable => logger.error(s"[$message] error", ex)
throw ex
}
}
@varmas
varmas / dynamicInstantiationInheritance.scala
Last active July 31, 2019 15:18
dynamic class instantiation in scala [using inheritance]
class ABC {
def foo(bar: String): String = s"wololo.... $bar"
}
class CDA extends ABC
object ABCRunner {
def main(args: Array[String]) {
val baz = Class.forName("a.b.c.CDA").newInstance.asInstanceOf[ABC]
@varmas
varmas / dynamicInstantiationStructuralTyping.scala
Created July 31, 2019 15:16
dynamic class instantiation in scala [using structural typing]
package a.b.c
class ABC {
def foo(bar: String): String = s"wololo.... $bar"
}
object ABCRunner {
type Sample = { def foo(bar: String): String }
@varmas
varmas / withSafeClose.scala
Created July 12, 2019 21:33
a safe resource closer
private def withSafeClose[A](source: Closeable)(op: Closeable => A): A = {
try {
op(source)
} finally {
source.close()
}
}
@varmas
varmas / docker-run-kafka-with-zookeeper.sh
Created June 22, 2019 17:08
kafka and zookeeper docker
# run zookeeper
docker run -p 2181:2181 --name zookeeper --rm wurstmeister/zookeeper
# run a kafka server
docker run -p 9092 \
-e KAFKA_ADVERTISED_HOST_NAME=$YOUR_IP \
-e KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181 \
-e KAFKA_BROKER_ID=1 \
-v /var/run/docker.sock:/var/run/docker.sock \
--link zookeeper:zookeeper \
@varmas
varmas / ExceptionWrapper.scala
Created June 7, 2019 21:57
exception handler wrapper
private def handleExceptions[T](fn: => T, message: String): T = {
try {
fn
} catch {
case ex: Exception =>
println(message)
throw ex
}
}
@varmas
varmas / overrideMethodForMock.scala
Created May 29, 2019 00:25
override methods for test
it should "something dirty" in {
val list: java.util.List[String]= new java.util.ArrayList[String] {
@throws(classOf[NullPointerException])
override def get(a: Int): String = {
throw new NullPointerException()
}
}
a[NullPointerException] should be thrownBy { // Result type: Assertion
list.get(0)