Skip to content

Instantly share code, notes, and snippets.

@jonelf
Created April 10, 2011 21:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonelf/912723 to your computer and use it in GitHub Desktop.
Save jonelf/912723 to your computer and use it in GitHub Desktop.
A follow-up to https://gist.github.com/908911 now it's showing the histogram for each slice of 3 characters.
require 'base64'
def histogram(arr)
arr.inject(Hash.new(0)) { |h, k| h[k] += 1; h}
end
def graph_histogram(h, skip_less_than)
h.to_a.select{|v| v[1]>=skip_less_than}.sort_by{|kv| kv[1]*-1}.
map{|a| "#{a[0]} |#{('+'*a[1])}#{a[1].to_s}\n"}.
join
end
def caesarb64(text,n)
base64_chars = (('A'..'Z').to_a+('a'..'z').to_a+('0'..'9').to_a+["+","/"])*2
text.tr('A-Za-z0-9/+', base64_chars[n..n+64].join)
end
plaintext=<<EOF
The first chapter "A Scandal in Bohemia" from http://www.gutenberg.org/cache/epub/1661/pg1661.txt
It's 19119 characters.
EOF
puts graph_histogram(histogram(Base64.decode64(caesarb64(Base64.encode64(plaintext),7)).each_char.each_slice(3)),10)
puts graph_histogram(histogram(plaintext.each_char.each_slice(3)),10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment