Skip to content

Instantly share code, notes, and snippets.

@arcesino
arcesino / traceable-express-routes.js
Created November 24, 2023 12:00
Traceable Express route definition
const express = require('express');
const { getProfile } = require('../middleware/getProfile');
const validators = require('../middleware/validators');
const {
contractsController,
jobsController,
balancesController,
adminController
} = require('../controllers')
@arcesino
arcesino / BinaryGap.java
Created January 21, 2017 20:38
Binary Gap (Java)
public class BinaryGap {
public static int maxBinaryGap(int N) {
int maxGap = 0;
int currentGap = 0;
boolean oneFound = false;
while (N > 0) {
if (N % 2 == 1) {
oneFound = true;
maxGap = Math.max(currentGap, maxGap);
@arcesino
arcesino / MatrixIslands.sc
Created November 16, 2016 23:20
Find largest island in a matrix
val matrix = Array(
Array(0, 1, 0, 0, 1),
Array(0, 1, 1, 1, 1),
Array(1, 0, 1, 1, 0),
Array(1, 1, 0, 0, 1)
)
val rows = matrix.indices
val columns = matrix(0).indices
@arcesino
arcesino / next-lexicographically.groovy
Created March 14, 2016 03:04
Next lexicographically word
def makeItLarge(inputs) {
inputs.collect { input ->
nextLexicographically(input)
}
}
def nextLexicographically(input) {
def queue = new java.util.PriorityQueue<Character>()
def a = input.toCharArray()
def pivot = -1
@arcesino
arcesino / snake.js
Last active September 6, 2015 02:14
Write a function that prints the matrix to the standard output (stdout) on a single line starting in the upper-left corner and continuing clockwise, in circles, from exterior towards interior.
var snakePrint = function(numbers, width) {
var xyTo1D = function(x, y) {
return y * width + x;
};
var matrix = numbers.split(','),
directions = [[1, 0], [0, 1], [-1, 0], [0, -1]],
output = '',
snake = {
length: 0,
@arcesino
arcesino / consecutive-array.js
Created July 3, 2015 20:08
Given an array of non-repeated numbers check whether the numbers are consecutive.
var isConsecutiveArray = function(numbers) {
var min = numbers[0],
max = numbers[0]
n = numbers.length;
for (var i = 1; i < n; i++) {
if (numbers[i] < min) min = numbers[i];
if (numbers[i] > max) max = numbers[i];
}
@arcesino
arcesino / braces-balancer.js
Last active August 29, 2015 14:24
Braces balancer
var isBalanced = function(str) {
var openers = '{[(<';
var closers = '}])>';
var stack = [];
str = str.split('');
for (var i = 0; str.length; i++) {
var index;
if (openers.indexOf(str[i]) != -1) stack.push(str[i]);
else if ((index = closers.indexOf(str[i])) != -1) {
@arcesino
arcesino / CrackingTheCode1.java
Created July 22, 2014 21:35
Check unique ASCII chars using bit flags
public class CrackingTheCode1 {
public static void main(String[] args) {
CrackingTheCode1 app = new CrackingTheCode1();
String[] tests = new String[] {
"abc123", "abc12a", "abc122", "s3cr3t0", "!#%&/()", "#abc123#"
};
for (int i = 0; i < tests.length; i++) {
String s = tests[i];
System.out.printf("%s : %b\n", s, app.isUniqueChars(s));
}
@arcesino
arcesino / load-script-file-cross-browser
Created April 3, 2014 17:46
To load a script file with great cross-browser support
loadScript = (src, callback) ->
script = document.createElement("script")
loaded = undefined
script.setAttribute "type", "text/javascript"
script.setAttribute "src", src
if script.readyState
script.onreadystatechange = -> # For old versions of IE
callback() if @readyState is "complete" or @readyState is "loaded"
return
else # Other browsers
# Chai assert extensions
###
Check an object is rendered.
Expects @_obj to be a jQuery object.
###
chai.Assertion.addProperty 'rendered', ->
selector = @_obj.selector
@assert @_obj.length > 0
, "expected #{selector} is not rendered"