Skip to content

Instantly share code, notes, and snippets.

View fphilipe's full-sized avatar
🌍
🎶 around the world 🕺🎶

Philipe Fatio fphilipe

🌍
🎶 around the world 🕺🎶
View GitHub Profile
@fphilipe
fphilipe / exclude.sql
Last active January 9, 2024 14:59
PostgreSQL EXCLUDE constraint
CREATE EXTENSION btree_gist;
CREATE TABLE room_reservations (
room_id integer,
reserved_at timestamptz,
reserved_until timestamptz,
canceled boolean DEFAULT false,
EXCLUDE USING gist (
room_id WITH =, tstzrange(reserved_at, reserved_until) WITH &&
) WHERE (not canceled)
@fphilipe
fphilipe / README.md
Last active May 7, 2019 12:26
Badiwassertemperaturen und -öffnungszeiten

Badiwassertemperaturen und -öffnungszeiten

A simple one-liner that results in this:

|-------------------------------+-------------+-------------------------|
|  Badi                         | Wassertemp. | Öffnungszeit            |
|-------------------------------+-------------+-------------------------|
|  Flussbad Au-Höngg            | 17          | geschlossen             |
|  Flussbad Oberer Letten       | 16          | geschlossen             |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@fphilipe
fphilipe / gist:4058176
Created November 12, 2012 08:35
Obfuscated ids
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 / _setup.sql
Last active September 16, 2016 08:07
-- Create users table with username as PK.
CREATE TABLE users_no_id (username varchar primary key, age int);
-- Create posts table with reference to users table using its PK and index on the reference.
CREATE TABLE posts_no_id (id serial primary key, username varchar references users_no_id (username), body text);
CREATE INDEX ON posts_no_id (username);
-- Create 1000000 users:
INSERT INTO users_no_id (username, age) SELECT substring(md5(random()::text) || md5(random()::text), (random()*32)::int, 16 + (random()*16)::int), 18 + (random()*70)::int FROM generate_series(1, 1000000);
-- Create 200000 posts for random users (multiple queries so users can have mutliple posts).
INSERT INTO posts_no_id (username, body) SELECT username, md5(random()::text) || md5(random()::text) FROM users_no_id ORDER BY random() LIMIT 25000;
INSERT INTO posts_no_id (username, body) SELECT username, md5(random()::text) || md5(random()::text) FROM users_no_id ORDER BY random() LIMIT 25000;
@fphilipe
fphilipe / main.m
Created August 21, 2012 09:01
Objective-C -hash method clash
// 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 / README.md
Last active December 20, 2015 19:38 — forked from mbostock/.block
@fphilipe
fphilipe / README.md
Last active December 20, 2015 07:38
Monte Carlo Sampling of π

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 π.
Calculating -------------------------------------
on_server_error :notify_honeybadger
90.832k i/100ms
on_server_error &Honeybadger.method(:notify)
96.540k i/100ms
on_server_error { |e| Honeybadger.notify(e) }
90.455k i/100ms
-------------------------------------------------
on_server_error :notify_honeybadger
2.007M (± 7.9%) i/s - 9.992M
@fphilipe
fphilipe / gist:2658685
Created May 11, 2012 09:50
API Versioning and Inheritance
module API
module V1
class UsersController
def index
'v1'
end
def show
'v1'
end
end