Skip to content

Instantly share code, notes, and snippets.

View al2o3cr's full-sized avatar

Matt Jones al2o3cr

View GitHub Profile
@al2o3cr
al2o3cr / README.md
Created May 23, 2021 03:28
Solution for "four fours" problem

Fours

A partial solution to the problem posed here: https://twitter.com/mikelikesbikes/status/1396159019837018117

This solution represents the expressions in reverse Polish notation (RPN) - see also Forth and HP48s.

The version presented here does NOT produce all values from 1 to 100, as it does not use enough operators.

Extending the search to additional operators only requires adding the value to @operators and a clause to eval to handle it.

@al2o3cr
al2o3cr / presentation.md
Last active March 1, 2019 23:02
50 Ways To Leave Your Method
[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]][([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+([][[]]+[])[+!+[]]+(![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[+!+[]]+([][[]]+[])[+[]]+([][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]])[+!+[]+[+[]]]+(!![]+[])[+!+[]]]((![]+[])[+!+[]]+(![]+[])[!+[]+!+[]]+(!![]+[])[!+[]+!+[]+!+[]]+(!![]+[])[+!+[]]+(!![]+[])[+[]]+(![]+[][(![]+[])[+[]]+([![]]+[][[]])[+!+[]+[+[]]]+(![]+[])[!+[]+!+[]]+(!![]+[])[+[]]+(!![]+[])[!+[]+!+
@al2o3cr
al2o3cr / README.md
Created June 25, 2018 10:44
STI method generation optimization

Reducing STI memory usage

if there are many STI child classes, ActiveRecord generates methods for each one individually. This is wasteful in the typical case, since all the children share the same underlying columns.

The small patch included here causes those methods to instead be generated in the parent class, shared amongst all the subclasses.

DO NOT use this if you call methods like attribute :foo to adjust the typecasting of columns in individual models.

@al2o3cr
al2o3cr / test_association_save.rb
Last active September 29, 2017 22:07
Test case for rails/rails #28536
# frozen_string_literal: true
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
@al2o3cr
al2o3cr / test_aliased_through.rb
Created January 14, 2017 23:04
Reproduction script for rails/rails #27666
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "http://rubygems.org"
# Activate the gem you are reporting the issue against.
require 'thwait'
class Voice
BASELINE = %w( there is no - poop ing - on - the bus - )
VOICES = `say -v '?'`.lines.map { |line| line.split.first }
attr_reader :voice_name, :rate
def initialize(voice_name, rate)
@i = 0
@al2o3cr
al2o3cr / bd_test.rb
Last active January 1, 2016 17:09
Test rounding behavior of BigDecimal
require 'bigdecimal'
def to_decimal(digits, dp)
digits = digits.dup
if dp < -digits.length
digits.unshift(*([nil]*(-digits.length-dp)))
end
digits.insert(dp+digits.length, '.').map { |x| x || '0' }.join
end
@al2o3cr
al2o3cr / gist:5073866
Created March 3, 2013 00:14
Silly SQL trick: computing pi very very inefficiently

Inspired by Postgres: The Bits You Haven't Found:

\set n 1000;

SELECT 4*COUNT(DISTINCT(x, y))/(1.0*:n*:n) AS result FROM generate_series(0,:n) AS x, generate_series(0,:n) AS y WHERE x*x + y*y <= :n*:n;

This uses the simple method of checking every integer-valued point in [0,n] x [0,n] to see if it's inside the circle - the Wikipedia approximations to pi article has more details.