Skip to content

Instantly share code, notes, and snippets.

Avatar
🌍
🎶 around the world 🕺🎶

Philipe Fatio fphilipe

🌍
🎶 around the world 🕺🎶
View GitHub Profile
@fphilipe
fphilipe / README.md
Last active Dec 20, 2015
Monte Carlo Sampling of π
View README.md

From the Wikipedia article on Monte Carlo method:

Monte Carlo methods (or Monte Carlo experiments) are a broad class of computational algorithms that rely on repeated random sampling to obtain numerical results; [...]

Given that the circle and the square have a ratio of areas that is π/4, the value of π can be approximated using a Monte Carlo method:

  1. Draw a square on the ground, then inscribe a circle within it.
  2. Uniformly scatter some objects of uniform size (grains of rice or sand) over the square.
  3. Count the number of objects inside the circle and the total number of objects.
  4. The ratio of the two counts is an estimate of the ratio of the two areas, which is π/4. Multiply the result by 4 to estimate π.
View gist:4058176
module Base62
BASE_CHARS = '0SKVnQFHhGs9qo7p8cRW54duMYNXTCErx2tDmwO3kabfUB1elLvjiIgyJAZ6Pz'.split('')
BASE = BASE_CHARS.length
BASE_ENCODED_MIN_LENGTH = 1
def self.decode(number)
# assure string
number = number.to_s.reverse.split('')
decoded = 0
@fphilipe
fphilipe / main.m
Created Aug 21, 2012
Objective-C -hash method clash
View main.m
// This code demonstrates -hash collision on NSString. Two strings have the same
// hash when the first, center, and last 32 chars are identical. The other chars
// don't matter at all.
//
// I had to find this out the hard way when loading images from cache while
// using the url string hash as the cache key. Unfortunately the urls were all
// gravatar urls, thus first 32 chars were identical. Further, all urls also had
// a fallback image url appended as query which covered the 32
// center and end chars. The varying part was between the start and center parts
// and thus the hash was identical for all urls.
@fphilipe
fphilipe / gist:2658685
Created May 11, 2012
API Versioning and Inheritance
View gist:2658685
module API
module V1
class UsersController
def index
'v1'
end
def show
'v1'
end
end
@fphilipe
fphilipe / assets.rake
Created Sep 23, 2011
rake assets:precompile without hitting the DB
View assets.rake
namespace :assets do
# Prepend the assets:precompile_prepare task to assets:precompile.
task :precompile => :precompile_prepare
# This task will be called before assets:precompile to optimize the
# compilation, i.e. to prevent any DB calls.
task 'precompile_prepare' do
# Without this assets:precompile will call itself again with this var set.
# This basically speeds things up.
ENV['RAILS_GROUPS'] = 'assets'
@fphilipe
fphilipe / assets.rb
Created Sep 21, 2011
Easier access to assets in Rails 3.1
View assets.rb
# coding: UTF-8
# A module to facilitate the retrieval of asset paths and urls in Rails 3.1.
module Assets
extend self
def url(file)
host + path(file)
end
View output.txt
Fixnum
user system total real
true
0.030000 0.000000 0.030000 ( 0.027810)
true
0.010000 0.000000 0.010000 ( 0.006959)
--------------------------------------------------
String
user system total real
true
View update_chromium.sh
#!/bin/sh
#### CONFIG ################################
USE_OLD_ICON=true # replace the new simplified icon with the old one
APP_DESTINATION=/Applications/Chromium.app # where to put the app
SUPPORT_DIR="`dirname "$0"`/.support" # where the icon and version number will be stored
############################################
VERSION_FILE="$SUPPORT_DIR/chromium_version"
NEWEST_VERSION=`curl -s http://commondatastorage.googleapis.com/chromium-browser-snapshots/Mac/LAST_CHANGE`
View gist:509369
def genitivize(name)
name.strip.sub(/(s)?$/i) { |s| s.downcase == 's' ? "#{s}’" : '’s' }
end