Created
December 27, 2009 16:04
-
-
Save banyan/264308 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| # [問題 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