Skip to content

Instantly share code, notes, and snippets.

@nishimotz
Last active July 16, 2017 07:10
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 nishimotz/7ad6677fd41d3006cc217d1572ebcb8d to your computer and use it in GitHub Desktop.
Save nishimotz/7ad6677fd41d3006cc217d1572ebcb8d to your computer and use it in GitHub Desktop.
170713 「あれ」の例題をやってみた

Ruby で「あれ」の例題 Q2 をやってみた

https://hiroshimarb.connpass.com/event/59910/

プログラミングコンテスト:

https://www.disco.co.jp/procon/

以前は Q1 を Python でやってみた:

https://gist.github.com/nishimotz/3c047c61dba03baf4c59c6b4afca871f

Q2

次の300個の名字の文字列の中で、 最も多いアルファベットの数は?

Q2/Q2.txt (UTF-8 with BOM) の内容

//	以下の文字列を使用して問題を解いてください。
//	データ型は任意
//	mainのローカル変数として定義してください。

{
"SATO","SUZUKI",
...
..."AKASE",
};

コメント:a2.rb の最後の2行は nishimotz 案は下記だった

-sorted = counts.sort { |a,b| a[1] <=> b[1] }
-sorted.reverse.each { |k,v| puts k + " " + v.to_s }
+sorted = counts.sort_by(&:last)
+pp sorted.reverse
# a2.rb
require 'pp'
counts = Hash.new(0)
File.open 'Q2/Q2.txt' do |f|
f.read.scan(/[A-Z]+/) do |w|
#p w
w.each_char do |c|
#puts c
counts[c] += 1
end
end
end
sorted = counts.sort_by(&:last)
pp sorted.reverse
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment