Skip to content

Instantly share code, notes, and snippets.

Math.random = (function(rand){
var salt = 0;
document.addEventListener("mousemove", function(event){
salt = event.pageX * event.pageY;
});
return function(){ return (rand() + (1 / (1 + salt))) % 1; };
})(Math.random);
@renatoargh
renatoargh / nodejs-installation-script.sh
Last active December 18, 2015 03:38
My custom Node.js installation scriptcurl https://gist.github.com/renatoargh/5719218/raw | sh
sudo yum upgrade -y
sudo yum install -y gcc-c++
sudo yum install -y openssl-devel
sudo yum install -y make
sudo yum install -y git
wget http://nodejs.org/dist/node-latest.tar.gz
tar -zxvf node-latest.tar.gz
@renatoargh
renatoargh / loteria.js
Last active December 18, 2015 13:29
Script para verificar quantos números você acertou na loteria, em um ou mais jogos.
function verificarVarios(sorteados, jogos){
return jogos.map(function(jogo){
return encontrarSorteados(sorteados, jogo);
}).sort(function(a, b){ return b.length - a.length; });
}
function encontrarSorteados(sorteados, jogados){
return jogados.filter(function(numero){
return sorteados.indexOf(numero) !== -1;
});
@renatoargh
renatoargh / routeSorter.js
Last active December 19, 2015 09:09
Sorting routes according to their precedence
var routes = ["/:id", "/author/:id", "/authors", "/author/foobar"];
routes.sort(function(a, b){
a = a.split("/");
b = b.split("/");
if(a.length !== b.length){
return a.length - b.length;
}else{
for(var i = 0; i < a.length; i++){
@renatoargh
renatoargh / findPrefix.js
Last active December 21, 2015 18:39
Algorithm to find commom prefixes within an array of strings.
function findPrefix(strings){
if(strings && strings.length > 0){
var prefix = "";
var characters = strings[0].split("");
for(var i = 0; i < characters.length; i++){
var isPrefix = true;
var character = characters[i];
for(var j = 0; j < strings.length; j++) {
@renatoargh
renatoargh / gammarouter.conf
Last active December 22, 2015 21:09
Script do upstart que funciona no Amazon Linux, para iniciar sua app se sua instancia for reiniciada.
#!upstart
description "Starts Gammarouter"
author "Renato Gama"
start on runlevel [2345]
stop on runlevel [^2345]
respawn # restart when job dies
respawn limit 5 60 # give up restart after 5 respawns in 60 seconds
npm http GET https://registry.npmjs.org/express
npm http GET https://registry.npmjs.org/underscore
npm http GET https://registry.npmjs.org/cliff
npm http GET https://registry.npmjs.org/gleak
npm http GET https://registry.npmjs.org/route-registrar/0.0.10
npm http GET https://registry.npmjs.org/jade
npm http GET https://registry.npmjs.org/fonts/0.0.2
npm http GET https://registry.npmjs.org/portfinder
npm http GET https://registry.npmjs.org/mongoose
npm http GET https://registry.npmjs.org/connect-domain
var fs = require('fs'),
path = require('path'),
util = require('util'),
assert = require('assert');
module.exports = function(appPath, testsPath, options){
if ( typeof testsPath === "object" ) {
options = testsPath;
testsPath = undefined;
}
@renatoargh
renatoargh / getPrimeFactors.js
Last active December 27, 2015 15:39
Algorithm to get prime factors of a given number.
function getPrimeFactors(number){
var factors = [], factor;
function getPrimeFactor(number) {
for (var i = 2; i <= Math.sqrt(number); i++) {
if(number % i === 0) {
return i;
}
}
echo "teste"