0

I am having problem sorting array into an ascending order, and then picking a value from the array to put into a new array.

# Splitting dance scores with "," and putting into arrays.
for dancers in results
  a = dancers.split(",")

  couplenumber = a[0]
  score1 = a[1]
  score2 = a[2]
  score3 = a[3]
  score4 = a[4]
  score5 = a[5]
  score6 = a[6]
  score7 = a[7]
  dancescores << Dancer.new(couplenumber, score1, score2, score3, score4, score5, score6, score7)
  # Sorts the array into ascending order, and shows the 4 lowest values.
  #p dancescores.sort.take(4)

  # Getting the m value, from picking the 4th lowest number.
  m = a[4]
  newtest = [couplenumber, m]
  coupleandscore << newtest
  coupleandscore
end
puts coupleandscore

Right now it gives me the original values in the new array, which it should. But if i try to do

p dancescores.sort.take(4)

I will get this error:

[#<Dancer:0x10604a388 @score7=5, @score3=3, @score6=6, @score2=2, @score5=1, @score1=1,     @couplenumber="34", @score4=3>]
examtest.rb:43:in `sort': undefined method `<=>' for #<Dancer:0x10604a388> (NoMethodError)

Any kind of help would be greatly appreciated!

2
  • Your scoring isn't exactly clear; Can you explain it better? The <=> error is because you haven't defined a <=> method. See the documentation for the Comparable module and Enumerable.sort. Commented Nov 29, 2012 at 14:46
  • Basically each couple gets scores between 1-6, 1 being the best. They get a score, from 7 judges. They could be getting scores like this (1,4,2,6,6,5,3). So i want to sort it so i goes in an ascending order like this (1,2,3,4,5,6,6) and then pick the 4th value (in this case the number 4) and compare it for each dancer (to find the winner, which is the one with lowest value in the 4th number) Commented Nov 29, 2012 at 14:53

2 Answers 2

2

You should explain more precisely what you want to do.

From what I understood :

class Dancer

  attr_reader :number
  attr_reader :scores

  def initialize(number,scores)
    @number=number
    @scores=scores.sort
  end  
end

dancescores=[]

results.each do |result|
  scores = result.split(',')
  couplenumber = scores.shift
  dancescores << Dancer.new(couplenumber, scores)
end

# Now you can get the 4th score for each couple
dancescores.each do |dancers|
  p dancers.scores[3]
end
Sign up to request clarification or add additional context in comments.

6 Comments

I'm getting the results from a file i load in, i've named that one results = File.open("danceresult.txt", "r+")so when i run the code, i get this error: aa.rb:17: undefined method ``scores' for #<Dancer:0x1042dd750 @number="34", @scores=[" 1"]> (NoMethodError) from aa.rb:16:in each'`
I forgot to add the attr_reader to access the number and scores from the Dancer class. It should be ok now. You still have to clean your input with chomp and strip to get rid of EOF and leading space
Bonus question, if i needed a way of getting couple number, and their score displayed, how would you go about that?
in the dancescores loop : puts "#{dancers.number} => #{dancers.scores[3]}"
Ah ofcourse! If i wanted to add numbers in the 0..3 array and display them, how is the easiest way to do that? I'ved tried to just + them in the loop, but it doesnt work as it just shows every single arrays content, and not the plussed total.
|
0

You could implement <=> on Dancer like so

class Dancer
  def sum_scores
    @score1 + @score2 + @score3 + @score4 + @score5 + @score6 + @score7
  end
  def <=> other_dancer
    sum_scores <=> other_dancer.sum_scores
  end
end

I am assuming the scores just add up to a grand total.

Update: Scoring is based on 4th value in sorted scores

class Dancer
  def scores
    [@score1,@score2,@score3,@score4,@score5,@score6,@score7]
  end
  def <=> other_dancer
   scores.sort[3] <=> other_dancer.scores.sort[3]
  end
end

Now in your code do the following:

for dancers in results
  a = dancers.split(",")

  couplenumber = a[0]
  score1 = a[1]
  score2 = a[2]
  score3 = a[3]
  score4 = a[4]
  score5 = a[5]
  score6 = a[6]
  score7 = a[7]
  dancescores << Dancer.new(couplenumber, score1, score2, score3, score4, score5, score6, score7)
end
puts dancescores.sort.map{|d| [d.couplenumber,d.scores.sort[3]]}

5 Comments

Actually i need to compare the values that im getting from each Dancer. It has to be the 4th lowest, which was why i was trying to sort it into ascending order, and then picking the 4th value.
so you mean the four lowest scores or the four lowest dancers ?
Each dancer will be given 7 scores. Lets say a dancer gets (1,3,6,1,5,5,3) i want that sorted, so it goes (1,1,3,3,5,5,6) instead, and then pick the 4th value, in this case the number 3. Or is there an easier way to do it?
When i try that out, i get this error: 25:in <=>': undefined method <=>' for nil:NilClass (NoMethodError) from examtest.rb:50:in sort'
use @Yoann Le Touche's answer its way better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.