registers0.rb
class Registers
attr_reader :registers
alias :waiting :registers
def initialize()
@registers = Array.new(5,0)
@transaction = [ 2,7,3,5,2 ]
end
def visit(num)
min_i = @registers.index(@registers.sort.first)
@registers[min_i] += num
end
def transact
@registers.each_index do |idx|
@registers[idx] -= @transaction[idx]
end
end
end
ここから、xの実装のために、列を1の配列に。
registers.rb
class Registers
attr_reader :registers
#alias :waiting :registers
def initialize()
@registers = Array.new(5)
@transaction = [ 2,7,3,5,2 ]
end
def visit(num)
@waiting = self.waiting()
min_i = @waiting.index(@waiting.sort.first)
if ( @waiting[min_i] == 0 )
@registers[min_i] = Array.new(num,1)
else
@registers[min_i] += Array.new(num,1)
end
end
def transact
@registers.each_index do |idx|
@transaction[idx].times { @registers[idx].shift }
end
end
def waiting
tmp = Array.new
@registers.each{|a| tmp.push(a == nil ? 0 : a.size)}
return tmp
end
end
spec
registers_spec.rb
# encoding : utf-8
require 'registers'
describe Registers, "#visit" do
context "最初の客が来る" do
regi = Registers.new()
regi.visit(3)
it { expect(regi.waiting).to eq([3,0,0,0,0]) }
end
context "visit 3,4" do
regi = Registers.new()
regi.visit(3)
regi.visit(4)
it { expect(regi.waiting).to eq([3,4,0,0,0]) }
end
context "visit 3,4,1,1,1,1" do
regi = Registers.new()
regi.visit(3)
regi.visit(4)
regi.visit(1)
regi.visit(1)
regi.visit(1)
regi.visit(1)
it { expect(regi.waiting).to eq([3,4,2,1,1]) }
end
end
describe Registers, "#transact" do
context "transact when [9,9,9,9,9]" do
regi = Registers.new()
5.times { regi.visit(9) }
regi.transact
it { expect(regi.waiting).to eq([7,2,6,4,7]) }
end
end
http://atnd.org/events/47025
に参加しました。
解けませんでした。。。