Skip to content

Instantly share code, notes, and snippets.

/*
* Copyright (c) 2011 Dhruv Matani
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
@dhruvbird
dhruvbird / example.js
Created May 23, 2011 18:11
JsonSelect
const js = require('../jsonselect.js');
var obj = {
books: [
{ name: "A Rough Ride", author: "Unknown" },
{ name: "A Smooth Ride", author: "Known" },
{ name: "Javascript", author: "Crockford" },
{ name: "Node.js", author: "Dahl" },
{ name: "C++", author: "Stroustrup" }
],
@dhruvbird
dhruvbird / dns_test.js
Created June 13, 2011 11:20
DNS callbacks are synchronous at times
var dns = require('dns');
console.log('Calling resolveSrv(localhost)');
dns.lookup('localhost', function() {
console.log('Callback for resolve(localhost)');
});
console.log('After calling resolveSrv(localhost)');
console.log('Calling resolveSrv(127.0.0.1)');
dns.lookup('127.0.0.0', function() {
@dhruvbird
dhruvbird / list_sort.c
Created April 11, 2012 13:24
Comparing merge sort implementations for doubly linked lists
/* Compiling: Create a file named list_sort.c in the src/ folder and
* run:
*
* gcc list_sort.c lcthw/list.o lcthw/list_algos.o -I . -O2
*
* Time: 14.025s
*
*/
#include "lcthw/list.h"
#include "lcthw/list_algos.h"
@dhruvbird
dhruvbird / sorting_algos.cpp
Created April 12, 2012 17:34
Optimized Bottom-up Merge Sort beats std::sort()
/*
* Output on my machine (i686 32-bit):
*
* qsort: 3.08 sec
* Bottom up Merge Sort: 1.98 sec
* Top down Merge Sort: 2.43 sec
* std::sort: 2.22 sec
*
*/
@dhruvbird
dhruvbird / stress.js
Created June 21, 2012 15:19
Performance test for new Error() on node.js
var path = require('path');
var filename = path.basename(path.normalize(__filename))
// Install deps:
// $> npm install log4js
// $> npm install node-lumberjack
//
// Run as:
// $> time node stress.js > /dev/null
@dhruvbird
dhruvbird / Makefile
Created July 16, 2012 23:02 — forked from utaal/Makefile
webserver using libuv
webserver: webserver.c libuv/uv.a http-parser/http_parser.o
gcc -I libuv/include \
-lrt -lm -lpthread -o \
webserver webserver.c \
libuv/uv.a http-parser/http_parser.o
libuv/uv.a:
$(MAKE) -C libuv
http-parser/http_parser.o:
@dhruvbird
dhruvbird / README.md
Created July 24, 2012 16:30
README.md for node-xmpp-bosh
def merge_sort(msg, m, depth=0):
print " " * depth, msg, m
result=[]
#Exit condition
if len(m) < 2:
return m
mid = int(len(m)/2)
left = m[:mid]
@dhruvbird
dhruvbird / smwc.py
Created February 11, 2015 06:53
Smallest multiple with constraints
"""
Problem statement:
------------------
Given a number 'n', design an algorithm that will output the smallest
integer number 'X' which contains only the digits {0, 1} such that
X mod n = 0 and X > 0
(1 <= n <= 100000)
"""
def solve(n):
if n < 2: