0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

第18回オフラインリアルタイム~横浜へなちょこプログラミング勉強会~に参加しました

Posted at
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
に参加しました。
解けませんでした。。。

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?