Skip to content

Instantly share code, notes, and snippets.

@emaiax
Created October 15, 2014 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emaiax/c14f0adc32573cb09048 to your computer and use it in GitHub Desktop.
Save emaiax/c14f0adc32573cb09048 to your computer and use it in GitHub Desktop.
Match Ranking
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
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