Skip to content

Instantly share code, notes, and snippets.

@raphaelcm
Created June 22, 2012 12:28
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 raphaelcm/2972470 to your computer and use it in GitHub Desktop.
Save raphaelcm/2972470 to your computer and use it in GitHub Desktop.
print out characters according to their relative frequencies
#!/usr/bin/env ruby
inputs = {
'a' => 18,
'b' => 13,
'c' => 9
}
total = inputs.values.inject{|sum,x| sum + x }
counts = inputs.dup
i = 0
string = []
while i < total
inputs.each do |k, v|
counts[k] += v
if counts[k] >= total
string << k
counts[k] -= total
i += 1
end
end
end
puts string.join('')
puts "\nLetter: Count (Expected)"
inputs.each do |k, v|
puts "#{k}: #{v} (#{string.count(k)})"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment