Skip to content

Instantly share code, notes, and snippets.

View mejibyte's full-sized avatar

Andrés Mejía mejibyte

View GitHub Profile
@mejibyte
mejibyte / bm.rb
Created August 29, 2011 19:12
Benchmark to see if ActiveRecord::Base.include_root_in_json significantly affects performance
# encoding: utf-8
require 'benchmark'
require 'json'
json_normal = <<-EOS
[
{
"order": {
"checkin_id": 298486374,
@mejibyte
mejibyte / CuboRobotV02.java
Created September 6, 2011 21:59
Simple robotic arm in Java3D
/*
* CuboRobot.java
* Este programa demuestra el concepto de "Scenegraph".
* Se arma un "dedo" articulado y el ángulo de rotación se cambia con las
* flechas.
*/
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
@mejibyte
mejibyte / new_syntax.rb
Created September 10, 2011 00:16
New syntax for Active Record.
User.where(:first_name => 'Scarlett').first_or_create!(:last_name => "Johansson", :hot => true)
# => #<User id: 1, first_name: "Scarlett", last_name: "Johansson", hot: true>
User.where(:first_name => 'Scarlett').first_or_create!(:last_name => "O'Hara", :hot => false)
# => #<User id: 1, first_name: "Scarlett", last_name: "Johansson", hot: true>
@mejibyte
mejibyte / gist:1208434
Created September 10, 2011 15:30
My TopCoder template.
using namespace std;
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <sstream>
#include <fstream>
#include <cassert>
#include <climits>
#include <cstdlib>
@mejibyte
mejibyte / gist:1208572
Created September 10, 2011 17:51
A file I deleted that I had to retype quickly.
int num[256] = {0};
vector<string>::iterator iter = text.begin();
while (iter != text.end()){
string st = *iter;
for (int i = 0; i < st.size(); i++){
int pos = st[i];
num[pos]++;
}
}
@mejibyte
mejibyte / gist:1233426
Created September 21, 2011 21:50
My implementation of Aho-Corasick's algorithm for string matching.
using namespace std;
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <sstream>
#include <fstream>
#include <cassert>
#include <climits>
#include <cstdlib>
@mejibyte
mejibyte / gist:1248480
Created September 28, 2011 16:58
C++ template for programming contests
using namespace std;
#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <sstream>
#include <fstream>
#include <cassert>
#include <climits>
#include <cstdlib>
@mejibyte
mejibyte / gist:1258471
Created October 3, 2011 05:04
Division of very big numbers in O(n) where n is the number of digits of the big number.
int D[3000];
int len;
int Mod(int x) {
int res = 0;
for (int i = len-1; i >= 0; --i)
res = (res * 10 + D[i]) % x;
return res;
}
100 120 -20 -100 -160 160
10 8 5 5 5 4
10 8 5 5 10 5
10 8 5 5 10 10
4 2 1 1 1 3
4 2 2 1 2 3
4 2 2 1 4 1
4 2 2 1 6 1
4 2 1 1 2 1
4 2 0 1 4 3
// Andrés Mejía
#include <cstring>
#include <iostream>
#include <vector>
#include <sys/time.h>
using namespace std;
const int MAXN = 1e9, MAXM = 10;
char d[MAXN+1];
char p[MAXM+1];