Skip to content

Instantly share code, notes, and snippets.

@pheuter
Created October 14, 2010 01:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pheuter/625369 to your computer and use it in GitHub Desktop.
Save pheuter/625369 to your computer and use it in GitHub Desktop.
def uncipher(str)
1.upto(26) do |shift|
key = Hash[('a'..'z').zip((97..122).collect{ |i| if (i+shift > 122) then ((i + shift) % 122) + 96 else i+shift end })].merge({' '=>' '.ord,','=>','.ord,'.'=>'.'.ord,'?'=>'?'.ord,'!'=>'!'.ord})
freqs = Hash.new(0)
tmp = str.split('').collect { |letter| switched = (key[letter]).chr; freqs[switched] += 1; switched }
total = 0
freqs.each { |e| total += e[1] }
puts tmp.join if freqs['e']/total.to_f * 100 > 7 and freqs['t']/total.to_f * 100 > 5 # Might want to eventually make more accomodating
end
end
uncipher(gets.chomp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment