Skip to content

Instantly share code, notes, and snippets.

View blancosj's full-sized avatar

uranio blancosj

  • NJ
View GitHub Profile
public static int partitionKDim(int[] A, int k, int target) {
int[][][] mem = new int[k + 1][A.length + 1][target + 1];
for (int t = 0; t < k + 1; t++) {
for (int i = 0; i < A.length + 1; i++) {
mem[t][i][0] = 1;
}
}
[
{
"action": "talk",
"text": "Please wait while we connect you."
}
]
[
{
"action": "talk",
"text": "This is Jose, please wait while we connect you."
},
{
"action": "connect",
"timeout": 60,
"from": "12028525606",
"endpoint": [
import scala.annotation.tailrec
case class Pos(x: Int, y: Int)
object Queen {
def isSafe(a: Pos, b: Pos): Boolean = {
a.x != b.x && a.y != b.y && Math.abs(a.x - b.x) != Math.abs(a.y - b.y)
}
def isSafe(a: Pos, queens: List[Pos]): Boolean = queens match {
// =======================
// Arrays: Left Rotation
// =======================
def leftShift(input: Array[Int], rotations: Int): Unit = {
val tmpArray = new Array[Int](total)
val total = input.length
val pos = total - math.abs(rotations % total)
// combinations without repetition
//
//
// n!
// ----------
// r!(n - r)!
//
// n = number of elements to choose from
// r = how many we choose
//
@blancosj
blancosj / searchBlackBox.scala
Last active February 3, 2017 05:57
Algorithm for searching the coordinates of a black box in a 2D image
object Solution extends App {
// Scala
val image = Array(
Array(1, 1, 1, 1, 1, 1, 1),
Array(1, 1, 1, 1, 1, 1, 1),
Array(1, 1, 1, 0, 0, 0, 1),
Array(1, 1, 1, 0, 0, 0, 1),
Array(1, 1, 1, 0, 0, 0, 1),
Array(1, 1, 1, 1, 1, 1, 1)
@blancosj
blancosj / mapTailRec.scala
Last active February 1, 2017 14:47
Implementation of the Map (higher-order function) for generic List collections with tail recursion
def tMap[T](tail: List[T], f: T => T): List[T] = {
@tailrec
def _q(tail: List[T], acc: List[T] = List()): List[T] = {
tail match {
case Nil => rev(acc)
case x :: xs => {
_q(xs, f(x) :: acc)
}
}
def permutator[T](list: List[T]): List[List[T]] = {
def _p(total: Int, list: List[T]): List[List[T]] = {
if (total == 0) {
list.map(List(_))
} else {
for (i <- list;
j <- _p(total - 1, list))
yield { i :: j }
}
object EnumFormat {
implicit def jsonReads[A](enum: Enumeration): Reads[A] = new Reads[A] {
def reads(json: JsValue): JsResult[A] = json match {
case JsString(s) => {
try {
JsSuccess(enum.withName(s).asInstanceOf[A])
} catch {
case _: NoSuchElementException =>
JsError(s"Enumeration expected of type: '${enum.getClass}', but it does not contain '$s'")
}