Created
October 15, 2014 18:13
-
-
Save emaiax/c14f0adc32573cb09048 to your computer and use it in GitHub Desktop.
Match Ranking
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
class Ranking | |
def initialize(list) | |
@list = list | |
end | |
def ranking | |
@list.map { |c| c.last.zero? ? 0 : 1 + c.last }.reduce(&:+) | |
end | |
def sort(lists) | |
lists.map { |t| [Ranking.new(t).ranking, t] }.sort.reverse.map(&:last) | |
end | |
end |
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
require_relative 'ranking.rb' | |
describe Ranking do | |
subject { described_class } | |
let(:trade) { [[1, 4], [2, 2], [3, 2], [4, 4], [5, 1]] } # 18 | |
let(:trader1) { [[1, 2], [2, 0], [3, 4], [4, 4], [5, 0]] } # 13 | |
let(:trader2) { [[1, 4], [2, 1], [3, 0], [4, 3], [5, 4]] } # 16 | |
it { expect(subject.new(trade).ranking).to eql 18 } | |
it { expect(subject.new(trader1).ranking).to eql 13 } | |
it { expect(subject.new(trader2).ranking).to eql 16 } | |
it { expect(subject.new(trade).sort([trader1, trader2])).to eql([trader2, trader1]) } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment