Skip to content

Instantly share code, notes, and snippets.

Avatar
coding

Thiago Araujo thdaraujo

coding
View GitHub Profile
@thdaraujo
thdaraujo / group_query_example.rb
Last active November 16, 2022 05:08
Group orders by customers and products
View group_query_example.rb
# frozen_string_literal: true
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem 'rails'
@thdaraujo
thdaraujo / complex_arel_ordering_example.rb
Last active June 2, 2021 05:26
Example of a complex order-by Arel query
View complex_arel_ordering_example.rb
# how to run: `$ ruby complex_arel_ordering_example.rb`
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem 'rails'
@thdaraujo
thdaraujo / cover.md
Last active March 11, 2021 19:01
Carbon-neutral Rails
View cover.md

Carbon-Neutral Rails

# Gemfile
gem 'patch_ruby'
require 'patch_ruby'
@thdaraujo
thdaraujo / foxyproxy.json
Created April 3, 2019 19:11 — forked from apetresc/foxyproxy.json
A FoxyProxy 6.x (for Firefox) version of the AWS EMR filters
View foxyproxy.json
{
"mode": "patterns",
"proxySettings": [
{
"title": "EMR SOCKS Proxy",
"type": 3,
"color": "#cc8c2a",
"address": "localhost",
"port": 8157,
"proxyDNS": true,
View square_root_newton.clj
(defn average [x y] (* 0.5 (+ x y)))
(defn improve [guess x] (average guess (/ x guess)))
(defn square [x] (* x x))
(defn abs [x] (if (< 0 x) x (- 0 x)))
(defn good-enough? [guess x]
(< (abs (- x (square guess))) 0.0000001))
@thdaraujo
thdaraujo / min_abs_sum.rb
Created April 7, 2018 23:13
Given array of integers, find the lowest absolute sum of elements.
View min_abs_sum.rb
def min_abs_sum(a)
n = a.size
[1, -1].repeated_permutation(n).map do |s|
val(a,s).abs
end.min
end
def val(a,s)
a.map.with_index{|e, i| a[i] * s[i]}.inject(:+)
end
@thdaraujo
thdaraujo / cyclic_rotation.rb
Created April 7, 2018 18:17
Cyclic rotation of array k times
View cyclic_rotation.rb
def cyclic_rotation(a, k)
size = a.size
return a if k == size || k == 0
result = a.clone
a.each_with_index{|e, i|
result[(i + k) % size] = e
}
result
end
@thdaraujo
thdaraujo / max_binary_gap.rb
Last active April 8, 2018 02:07
Calculates the max binary gap of an integer.
View max_binary_gap.rb
# Returns the maximum binary gap of a number.
# e.g.: max binary gap of 529 (1000010001 in binary) is 4.
#
def max_binary_gap(n)
return 0 if (n < 1)
result = n.to_s(2).scan(/10+1/).map{|m|
m.gsub('1', '').size
}.select{|gap|
gap >= 0
@thdaraujo
thdaraujo / rails_pg_timezone_lookup.sql
Created April 5, 2018 23:14
Mapping between rails timezones (names) and their ISO definition on postgres.
View rails_pg_timezone_lookup.sql
-- see http://api.rubyonrails.org/classes/ActiveSupport/TimeZone.html MAPPING
WITH timezone_lookup(rails_timezone, pg_timezone)
AS (
VALUES
('International Date Line West', 'Pacific/Midway'),
('Midway Island', 'Pacific/Midway'),
('American Samoa', 'Pacific/Pago_Pago'),
('Hawaii', 'Pacific/Honolulu'),
('Alaska', 'America/Juneau'),
('Pacific Time (US & Canada)', 'America/Los_Angeles'),
@thdaraujo
thdaraujo / inbox.sol
Last active April 8, 2018 02:07
A simple smart contract with Solidity.
View inbox.sol
pragma solidity ^0.4.17;
contract Inbox{
string public message;
function Inbox(string initialMessage) public {
message = initialMessage;
}
function setMessage(string newMessage) public {