我正在编写一个程序,该程序应该对比赛中的竞争者进行排名和排序。
# Defining class. Also sorts the array elements in ascending order.
class Dancer
attr_reader :couplenumber
attr_reader :scores
def initialize(couplenumber,scores)
@couplenumber=couplenumber
@scores=scores.sort
end
end
# Opening file, and sorting into array. Also splits with ",".
results = File.open("danceresult.txt", "r+")
dancescores=[]
results.each do |result|
scores = result.split(',').map(&:to_i)
couplenumber = scores.shift
dancescores << Dancer.new(couplenumber, scores)
end
dancescores.each do |dancers|
dancers.scores
# Prints to screen.
puts "Couple No. #{dancers.couplenumber} got "\
"a minimum score of #{dancers.scores[3]} or better. "\
"Their sum is: #{dancers.scores[0..3].inject(:+)}"
end
我希望总和Their sum is: #{dancers.scores[0..3].inject(:+)}
只取最低分数最低的那些。所以如果我们有五个竞争者,其中两个最低得分为 2,另外三个最低得分为 4,那么“总和部分”应该只取最低得分为 2 的竞争者。是否可能没有重新编写所有代码,如果是这样,最简单的方法是什么?