Skip to content

Instantly share code, notes, and snippets.

@defuse
Created October 22, 2013 23:30
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 defuse/7109985 to your computer and use it in GitHub Desktop.
Save defuse/7109985 to your computer and use it in GitHub Desktop.
Cryptography assignment or something?
lines = [
"ABCDEFGHIJFG",
"ABIKLAKGCMAIHDJACKNKCKNMDH",
"MADHDPACLDHIKH",
"AILDQIGCRIPACKNGHPACLDSDKLDPD",
"ALCIHPIQTIDCEAPAHIG"
]
ct = lines.join("")
freq1 = Hash.new { 0 }
freq2 = Hash.new { 0 }
freq3 = Hash.new { 0 }
freq4 = Hash.new { 0 }
ct.each_char do |c|
freq1[c] += 1
end
ct.each_char.to_a.each_cons(2) do |cons|
cons = cons.join("")
freq2[cons] += 1
end
ct.each_char.to_a.each_cons(3) do |cons|
cons = cons.join("")
freq3[cons] += 1
end
ct.each_char.to_a.each_cons(4) do |cons|
cons = cons.join("")
freq4[cons] += 1
end
freq1a = freq1.map { |k,v| [k,v] }
freq2a = freq2.map { |k,v| [k,v] }
freq3a = freq3.map { |k,v| [k,v] }
freq4a = freq4.map { |k,v| [k,v] }
[freq1a, freq2a, freq3a, freq4a].each do |array|
array.sort! { |a, b| -a[1] <=> -b[1] }
end
[freq1a, freq2a, freq3a, freq4a].each do |array|
puts "---------------------------------------"
array.each do |ary|
puts "#{ary[0]}: #{ary[1]}"
end
end
changes = Hash.new { |h,x| x }
loop do
puts (ct.each_char.to_a.map { |c| changes[c] }.join(""))
sub = gets
a = sub[0]
b = sub[1]
changes[a] = b
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment