Skip to content

Instantly share code, notes, and snippets.

@jmettraux
Created July 13, 2017 21:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmettraux/1308c411c74f8429ed4447d8fe0d55ad to your computer and use it in GitHub Desktop.
Save jmettraux/1308c411c74f8429ed4447d8fe0d55ad to your computer and use it in GitHub Desktop.

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