This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
require 'date' | |
class Person | |
def can_drink? | |
age >= 21 | |
end | |
def age |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |