Created
March 27, 2019 02:22
-
-
Save daviscodesbugs/e30d18fed2dc5c9497ef41129e98fdba to your computer and use it in GitHub Desktop.
Crount
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
# Get the count of each character in a file | |
filename = ARGV[0] | |
file = File.new(filename) | |
chars = {} of String => Int32 | |
while true | |
r = file.gets(1) | |
next if r == " " || r == "\n" || r == "\r" | |
break if r.nil? | |
if chars[r]?.nil? | |
chars[r] = 1 | |
else | |
chars[r] += 1 | |
end | |
end | |
keys = chars.keys | |
counts = chars.values | |
while true | |
max = -1 | |
maxi = -1 | |
counts.size.times do |i| | |
if counts[i] > max | |
max = counts[i] | |
maxi = i | |
end | |
end | |
break if max < 0 | |
puts "#{max}\t#{keys[maxi]}" | |
keys[maxi] = "" | |
counts[maxi] = -1 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment