Skip to content

Instantly share code, notes, and snippets.

@jmettraux jmettraux/README.md Secret
Created Jul 13, 2017

Embed
What would you like to do?

charcount

Variations on Nishimotoさん's Disco challenge.

# brutal: reads the whole file into memory
require 'pp'
pp File.read('names.txt')
.each_char
.inject(Hash.new(0)) { |h, c|
c = c.downcase
h[c] += 1 if c >= 'a' && c <= 'z'
h }
.sort_by { |k, v| -v }
# only loads 1024 chars max
require 'pp'
h = Hash.new(0)
File.open('names.txt') do |f|
loop do
s = f.read(1024); break unless s
s.each_char { |c|
c = c.downcase
h[c] += 1 if c >= 'a' && c <= 'z' }
end
end
pp h.sort_by { |k, v| -v }
sato amano nishida saito fujiwara matsudaira
nishimoto ishibashi takata kitadai mitsuda bussaki himura
kikuchi maru arai ishikawa
takahashi ueda
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.