Skip to content

Instantly share code, notes, and snippets.

@moizr
moizr / binary_heap.js
Created August 3, 2013 12:12
Binary heap implementation
function BinaryHeap(type){
this.heap = []
this.comparator = type.comparator;
}
BinaryHeap.prototype.swap = function(index1, index2){
var temp = this.heap[index1];
this.heap[index1] = this.heap[index2];
@moizr
moizr / santa.js
Created July 28, 2013 20:31
Fast santa.js (redistribute gifts in O(n) time)
function Bag(data){
this.data = data.slice(0);
this.misses = 0;
}
/**
* Remove the element at index from the Bag
*
* @param index
* @return {*}
@moizr
moizr / tree.js
Created July 24, 2013 05:04
Binary Tree
function Node(val){
this.left = null;
this.right = null;
this.val = val;
}
function Tree(){
this.root = null;
@moizr
moizr / hashset.js
Created July 22, 2013 19:55
Quick and dirty javascript Set implementation
if(typeof Object.keys === 'undefined'){
Object.keys = function(o){
var out = [];
for(var key in o){
if(o.hasOwnProperty(key)){
out.push(key);
}
}
return out;
}
@moizr
moizr / poll.js
Created May 31, 2013 21:47
http polling program in node
var http = require('http'),
EventEmitter = require('events').EventEmitter;
var nextEvent = new EventEmitter();
var requestId = 0;
function poll(){
requestId++;
http.get("<url>", function(res){
res.on('data', function(chunk){
@moizr
moizr / only_new.rb
Created April 1, 2013 20:26
Compares 2 XML files and return elements from the second file that are not in the first file ruby only_new.rb <first file> <second file> <element path> <id attribute name> The id attribute name should uniquely identify the element
require 'nokogiri'
def read_elements(filename, element_path, id_name, &callback)
f = File.open(filename)
xml = Nokogiri::XML(f)
elements = []
xml.xpath(element_path).each do |element|
element_id = element.attributes[id_name].value.to_s
elements.push(element_id)
@moizr
moizr / main.js
Created March 20, 2013 02:26
A small script I wrote to find which selectors used a particular background color. css-parse rocks!
var parse = require('css-parse')
var fs = require('fs')
var _ = require("underscore");
var contents = fs.readFileSync("style.css", "utf-8");
var x = parse(contents);
var printSelectors = function(regex){
var importantSelectors = [];
_.each(x.stylesheet.rules, function(d){
@moizr
moizr / main.rb
Last active December 14, 2015 19:18
Ruby Program to generate a WebEx Social Basic theme
# To run
# ruby main.rb <header_base_color>
#
# Examples
# ruby main.rb 666666
# ruby main.rb EE7621
# ruby main.rb EE7621 C48E48 FF3300
#
# It looks nicer when the header_base_color is a darker color
#
@moizr
moizr / to_gemfile.rb
Created September 2, 2012 23:21
Convert a list of gems to a gemfile
#!/usr/bin/env ruby
# Save in file to_gemfile.rb
# Use like so
# gem list | ./to_gemfile.rb
gem_file = File.open("Gemfile", "w")
gem_file.write("source :rubygems\n")
STDIN.readlines.each do |line|
line = line.chomp
line =~ /(.*)\s\((.*)\)/
@moizr
moizr / Main.java
Created July 2, 2012 22:40
Single quotes not matched. Why?
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Main {
private final static Pattern SCRIPT_SRC_PATTERN = Pattern.compile("(src)(?:\\s*=\\s*)(?:(?:\"((?:\\.|[^\"])*)\")|(?:\'((?:\\.|[^\'])*)\')|([^>\\s]+))");
public static void main(String[] args){
String works = "<script src=\"test\"></script>";
Matcher m = SCRIPT_SRC_PATTERN.matcher(works);