Skip to content

Instantly share code, notes, and snippets.

@banyan
Created December 27, 2009 16:04
Show Gist options
  • Select an option

  • Save banyan/264308 to your computer and use it in GitHub Desktop.

Select an option

Save banyan/264308 to your computer and use it in GitHub Desktop.
# [問題 6.3]
# * 文字列が与えられたとき,アルファベットを大文字,小文字を区別せずに出現頻度順に小文字で並べた文字列を返すプログラムを作りなさい.
# http://www.aoni.waseda.jp/ichiji/2009/second-term/ruby-06-1.html
def freq(str)
h = {}
str.downcase.each_byte do | s |
if h.has_key?(s.chr)
h[s.chr] = h[s.chr] + 1
else
h[s.chr] = 1
end
end
buf = []
h.sort{|a, b| b[1] <=> a[1]}.each do|key, value|
buf << key
end
buf.to_s
end
p freq("Lq fubswrjudskb, d Fdhvdu flskhu, dovr nqrzq dv d Fdhvdu'v flskhu, wkh vkliw flskhu, Fdhvdu'v frgh ru Fdhvdu vkliw, lv rqh ri wkh vlpsohvw dqg prvw zlghob nqrzq hqfubswlrq whfkqltxhv.")
# " hvdulfrqwks,bgzoip'nt.jx"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment