第21回オフラインリアルタイムどう書く


@Nabetani さんに誘われて参加してみました。

問題はこちら
http://d.hatena.ne.jp/torazuka/20140509/yhpg

ちなみに進捗ダメです。

``` lesson.rb
class Lesson
  class << self
    def create(data)
      application_list = data.split("|")
      lessons = {}
      # データを作る
      application_list.each do |a|
        split_data = a.split("_")
        employee = split_data.first
        days = split_data.last
        days.chars.each do |d|
          lessons[d] = [] unless lessons[d]
          if lessons[d].size < 4
            lessons[d].push(employee)
            break
          end
        end
      end
      # 出力用を組み立てる
      result = ""
      lessons.sort.each do |day, emps|
        result += "#{day}_"
        emps.sort_by{|e| e.to_i }.each do |e|
          result += "#{e}:"
        end
        result.chop!
        result += "|"
      end
      result.chop!
      result
    end

  end
end

```

``` lesson_spec.rb
require "spec_helper"
require "lesson"

describe Lesson do
  describe ".create" do
    let(:result){ Lesson.create(data) }
    context "one data" do
      let(:data){ "1_12345" }
      it "returns hoped data" do
        result.should eq "1_1"
      end
    end
    context "one data and other number" do
      let(:data){ "30_32451" }
      it "returns hoped data" do
        result.should eq "3_30"
      end
    end
    context "two data for same lesson" do
      let(:data){ "1_12345|2_12345" }
      it "create one lesson in two person." do
        result.should eq "1_1:2"
      end
    end
    context "three employee for one lesson" do
      let(:data){ "3_12345|2_12345|1_12345" }
      it "create one lesson in three person" do
        result.should eq "1_1:2:3"
      end
    end
    context "three persons for three lesson" do
      let(:data){ "1_23415|2_12435|3_42135" }
      it "create three lesson" do
        result.should eq "1_2|2_1|4_3"
      end
    end
    context "#5" do
      let(:data){ "1_23415|2_12435|3_42135|4_31245|5_53124" }
      it "create three lesson" do
        result.should eq "1_2|2_1|3_4|4_3|5_5"
      end
    end
    [
      # 6
      ["40_12345|21_12345|3_12345|61_12345|10_12345", "1_3:21:40:61|2_10"],
      # 7
      ["1_23415|2_12435|3_42135|4_54321|5_12345|6_13524|7_42351|8_31254|9_24513|10_12345", "1_2:5:6:10|2_1:9|3_8|4_3:7|5_4"],
      # 8
      ["1_54321|2_54321|3_54321|4_54321|5_54321|6_54321|7_54321|8_54321|9_54321|10_54321|11_54321|12_54321|13_54321|14_54321|15_54321|16_54321|17_54321|18_54321|19_54321|20_54321|21_54321", "1_17:18:19:20|2_13:14:15:16|3_9:10:11:12|4_5:6:7:8|5_1:2:3:4"],
      # 9
      ["1_34152|2_23514|3_41325|4_15342|5_45312|6_35124|7_21453|8_52431|9_13245|10_54123|11_13245", "1_4:9:11|2_2:7|3_1:6|4_3:5|5_8:10"],
      # 10
      ["92_41532|58_14325|40_25413|5_45132|71_35124|23_13452|60_35241|77_31542|53_13542|72_12354", "1_23:53:58:72|2_40|3_60:71:77|4_5:92"],
      # 11
      ["55_24153|91_24531|9_12543|41_34215|72_15423|44_42531|6_42351|79_15243|21_35412|31_52413|74_24135|83_31254|33_35421|84_53421|89_53241|16_32415|36_15234|92_34521|62_12345|14_23415|40_23415|88_43251|52_45213|77_32154|59_53241", "1_9:36:72:79|2_14:55:74:91|3_21:33:41:83|4_6:44:52:88|5_31:59:84:89"],
    ].each do |input, output|
      it "should match" do
        Lesson.create(input).should eq output
      end
    end
  end
end
```

悠長にテスト書いてたら途中から時間が足りなくなったし#6からの書き方にしたほうが速いことに気づくのが遅すぎましたね〜。しかも #11 のテストが通らないので進捗ダメですorz

## きちんとした解答は
@Nabetaniさんのを参考に

http://qiita.com/Nabetani/items/7d7c46438339b730cc4b

## 感想
面白かったです。