Create a gist now

Instantly share code, notes, and snippets.

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

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