Skip to content

Instantly share code, notes, and snippets.

@ruado1987
ruado1987 / CombinatorialAlg.scala
Last active December 17, 2015 11:49
Generate Next Larger Bit String
def nextBs(n: Int) = {
// compute the right-most 0-bit position
val rmz = ~n & (n + 1)
// bitmask
var shift = 1
// next larger bit string, computed by repeatedly applying the bit mask
var next = n
// mask must be less than or equal to the value of rmz
while (shift <= rmz) {
next = next ^ shift // mask out the bit representing by the bitmask
@ruado1987
ruado1987 / EraSieve.java
Last active December 17, 2015 08:49
Sieve of Era
import java.util.BitSet;
public class EraSieve {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int cap = (int) Math.sqrt(n);
BitSet bits = new BitSet(n);
bits.set(0, n);
@ruado1987
ruado1987 / Week1.scala
Last active December 17, 2015 02:58
Pascal triangle, parentheses balancing, and count coin change
def pascal(c: Int, r: Int): Int = {
if (r < 0 || c < 0) throw new IllegalArgumentException
def pascal_(c: Int, r: Int): Int = {
if (r <= 1) 1
else {
if (c == 0 || c >= r) 1
else pascal_(c - 1, r - 1) + pascal_(c, r - 1)
}
}
@ruado1987
ruado1987 / CountingBitsFunctionally.scala
Last active December 17, 2015 00:29
Counting number of bits in an integer number
def countBits(x : Int) = {
def isolateRightMost1Bit(num: Int) = num & (-num)
def loop (y: Int, count: Int) : Int = {
val z = isolateRightMost1Bit(y)
if (z <= 0) count
else loop( y ^ z, count + 1)
}
loop(x, 0)
var fs = require('fs'),
argv = require('optimist').argv;
fs.readFile('raw.txt', 'utf-8', function(err, data) {
var regex = /.*(vo\.get[^;]+)\){1};{1}/g,
/* contains match each time the regular expression
is executed against the file content*/
match,
/* the template to generate object image */
tmpl = 'image.put("{0}", {1});',
@ruado1987
ruado1987 / stringSplitter.js
Last active December 11, 2015 10:08
Mimic the built-in split function of String class
var assert = require('assert');
function parseData(str, d) {
d = d || ":";
var regex =
new RegExp("(\\w+|\\S*?)(?:(" + d + "+?))", "g"),
data = [], lastIndex, match;
while ((match = regex.exec(str))!= null) {
data.push(match[1]);
lastIndex = regex.lastIndex;
@ruado1987
ruado1987 / DecToBin.groovy
Created November 8, 2012 08:24
Convert decimal number to binary representation
def input = '3.75'
decimalToBinary('3.14')
assert decimalToBinary(input) == '11.11'
assert fractionToBinary('0.1') == 'ERROR'
def decimalToBinary(decimal) {
def firstDot = decimal.indexOf('.')
def integral = decimal.substring(0, firstDot) as Integer
def fraction = '0.' + decimal.substring(firstDot + 1)
@ruado1987
ruado1987 / SimpleLinkedList.groovy
Created October 17, 2012 07:45
Simple linked list implementation
class Node {
def data
def next
Node(data) {
this.data = data
}
void add(data) {
@ruado1987
ruado1987 / WordPermt.groovy
Created October 15, 2012 10:30
Word permutation generator
import org.junit.*
import junit.framework.*
import groovy.util.*
class WordPermutater {
private wordStack = []
WordPermutater(word) {
wordStack.push(word)