Last active
January 3, 2016 04:59
-
-
Save diglateam3/8412996 to your computer and use it in GitHub Desktop.
CSVで入力されてきたデータの平均点を求め、名前と平均点を出力する
This file contains 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
=begin | |
CSVで入力されてきたデータの平均点を求め、名前と平均点を出力する | |
(あまりRubyを触ったことがないので、余計に長くなっていたりするかも) | |
Author: 9appat3ch (http://github.com/kptech) | |
=end | |
require "csv" | |
#ループ内で使用する変数の初期化 | |
goukei = 0 | |
basefile = CSV.read('point-data.csv') | |
basefile.each do |data| #<- 配列dataとして、読み取ったbasefileを入れる | |
data.delete_at(0) #<- 名前部分の削除(basefileの一番最初に名前が入っているとして) | |
data.each do |point| #<- 名前部分を消去した配列dataから読み取る | |
goukei += Integer(point) #<- ここでpointをString型からInteger型に変換しておかないとTypeErrorになる | |
#TODO: 名前部分(data[0])以外に文字列が含まれていた場合のエラー処理。含まれていた場合ArgumentErrorになる | |
end | |
puts goukei / data.length #<- 平均点を求める | |
end | |
#TODO: どうやって名前部分と共に出力するか |
This file contains 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
Example A | 30 | 65 | 62 | 70 | 50 | |
---|---|---|---|---|---|---|
Example B | 25 | 70 | 48 | 48 | 82 | |
Example C | 70 | 53 | 21 | 19 | 50 | |
Example D | 11 | 33 | 34 | 72 | 57 | |
Example E | 49 | 73 | 74 | 11 | 47 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
以下なら出るかと思う
!/usr/bin/env ruby
require "csv"
basefile = CSV.read("./score.csv")
basefile.each do |data| #<- 配列dataとして、読み取ったbasefileを入れる
goukei = 0 #<- ループ内で使用する変数の初期化
end