I’m writing a program that’s supposed to rank and sort contenders in a competition.
# 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
I want the sum Their sum is: #{dancers.scores[0..3].inject(:+)} to take only those who have the lowest minimum score. So if we have five contenders, two of them get minimum score of 2, and the other three gets a minimum score of 4, then the “sum part” should only take the contenders with a minimum score of 2. Is it possible without having to rework all of the code, and if so, what is the easiest way to do it?
First establish what the minimum score is:
Then add a conditional before your print:
Or you can use
selectto handle only those dancers with the min_score like so: