This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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});', |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
def data | |
def next | |
Node(data) { | |
this.data = data | |
} | |
void add(data) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.* | |
import junit.framework.* | |
import groovy.util.* | |
class WordPermutater { | |
private wordStack = [] | |
WordPermutater(word) { | |
wordStack.push(word) |