Skip to content

Instantly share code, notes, and snippets.

class CaesarCypher
# https://en.wikipedia.org/wiki/Letter_frequency
CHAR_DISTRIBUTION = {'a'=>8.167,'b'=>1.492,'c'=>2.782,'d'=>4.253,'e'=>12.702,'f'=>2.228,'g'=>2.015,'h'=>6.094,'i'=>6.966,'j'=>0.153,'k'=>0.772,'l'=>4.025,'m'=>2.406,'n'=>6.749,'o'=>7.507,'p'=>1.929,'q'=>0.095,'r'=>5.987,'s'=>6.327,'t'=>9.056,'u'=>2.758,'v'=>0.978,'w'=>2.361,'x'=>0.150,'y'=>1.974,'z'=>0.074}
IDEAL_ORDER = Hash[CHAR_DISTRIBUTION.sort_by { |_, v| -v }].keys
THRESHOLD_PERCENTAGE = 10
def encode(message, shift)
message.chars.map do |char|
if char =~ /[a-z]/
@saxxi
saxxi / fizz_buzz.rb
Last active August 29, 2015 14:27
Classic FizzBuzz in Ruby
class FizzBuzz
def dump(total_numbers)
total_numbers.times do |i|
puts parse_string i + 1
end
end
def parse_string(number)
is_divisible_by_3 = number % 3 == 0
# Sum all the digits
# Write a program that takes a positive number as an input.
# The program should take every single digit of this number,
# and sum them until only a 1 digit number is left.
# Example input: 31337
# Output: 8 (because 3+1+3+3+7=17 and 1+7=8)
@saxxi
saxxi / gist:965b6f3f9562432e62c8
Created November 9, 2014 19:04
Metaprogramming Ruby - Access anything
# Ruby as a particularly flexible language which provides developers a rich toolbox
# to express any source code in several different ways.
# Reading "Metaprogramming Ruby - Program Like the Ruby Pros" (The Pragmatic Programmers)
# I wanted to try my self many of the different `spells` (as Paolo calls them)
# I learned or learned its formal definition.
# Knowing how to use these tools becomes crucially useful as
# we'd often like a slight modification of the behaviour of an external gem.
# In this case we can either send a pull request to the owner
# Instead of monkeypatching globally methods
# it is possible to `refine` methods
# so you know where a method is coming from.
#
# Ref:
# - http://rkh.im/ruby-2.1
# - http://www.ruby-doc.org/core-2.1.1/doc/syntax/refinements_rdoc.html
#
# Note:
# - it was intruced in ruby 2.0 and became stable in ruby 2.1.
@saxxi
saxxi / dependency_injection_spec.rb
Created August 18, 2014 19:37
Ruby Rspec - Testable classes with dependencies
require 'rspec'
require 'date'
class Person
def can_drink?
age >= 21
end
def age
@saxxi
saxxi / gist:6495116
Last active December 22, 2015 15:49
# Elastic search grouping solution
# As at present ElasticSearch does not provide a group_by equivalent, here's my attempt to do it manually.
# In the example we have articles made by some authors and I'd like to have relevant docs, but not more than one per author.
# Assumption.
#
# 1) I'm looking for relevant content
# 2) I've assumed that first 300 docs are relevant,
# So I consider only this selection, regardless many of these are from the same few authors.
# 3) for my needs I didn't "really" needed pagination, for me it was enough a "show more" button updated through ajax
@saxxi
saxxi / Hash.symbolize_keys.rb
Created January 25, 2011 14:08
convert hash object to symbol
# a = {"three"=>3, "two"=>2, "one"=>1}.symbolize_keys = {:three=>3, :one=>1, :two=>2}
# from Greg => http://objectmix.com/ruby/325092-transform-hash-key-string-into-symbol.html
class Hash
def symbolize_keys
replace(inject({}) { |h,(k,v)| h[k.to_sym] = v; h })
end
end