Skip to content

Instantly share code, notes, and snippets.

View alecbz's full-sized avatar

Alec Benzer alecbz

View GitHub Profile
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
class Trie
{
public:
struct Node
{
``` cpp
void inc(int& n)
{
int off = 1;
while(n & off)
{
n ^= off;
off <<= 1;
}
n ^= off;
@alecbz
alecbz / inc.cpp
Created July 4, 2011 21:51
increment an integer without arithmetic operators
void inc(int& n)
{
int off = 1;
while(n & off)
{
n ^= off;
off <<= 1;
}
n ^= off;
}
@alecbz
alecbz / pi.clj
Created July 3, 2011 06:47
compute pi via monte carlo
(defn circle-test [x y]
(not ( > ( + (* (- x 0.5) (- x 0.5)) (* (- y 0.5) (- y 0.5))) 0.25)))
(defn error [est]
(Math/abs (* (/ (- Math/PI est) Math/PI) 100)))
(defn pi [n]
(loop [hits 0 total 0]
(let [x (rand) y (rand)]
(if (< total n)
@alecbz
alecbz / fork.c
Created July 3, 2011 06:44
forking and piping in C
#include <stdio.h>
#include <unistd.h>
int main()
{
pid_t pid = fork();
if(pid == -1)
fprintf(stderr,"There was an error\n");
else if(pid == 0)
printf("I am the child. I do not know my pid.\n");
@alecbz
alecbz / levenshtein.rb
Created July 3, 2011 06:38
ruby function for Levenshtein word distance
def distance(a,b)
matrix = Array.new(a.size + 1) { Array.new(b.size + 1) }
(0..a.size).each { |i| matrix[i][0] = i }
(0..b.size).each { |j| matrix[0][j] = j }
(1..a.size).each do |i|
(1..b.size).each do |j|
if a[i-1] == b[j-1]
matrix[i][j] = matrix[i-1][j-1]
else
matrix[i][j] = [matrix[i-1][j] + 1,matrix[i][j-1] + 1,matrix[i-1][j-1] + 1].min
$ ./test
./test: error while loading shared libraries: libSDL_draw-1.2.so.0: cannot open shared object file: No such file or directory
$ which libSDL_draw-1.2.so.0
/usr/local/lib/libSDL_draw-1.2.so.0
/*
LEAST LIKELY TO COMPILE SUCCESSFULLY:
Ian Phillipps, Cambridge Consultants Ltd., Cambridge, England
*/
#include <stdio.h>
main(t,_,a)
char
*
a;
redis.zrevrangebyscore(@id_info_key, a, b, {:limit => [0,1]})
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <plot.h>
const double PI = 3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067982148086513282;
const double F = 2;
double arr[16];