ようへいの日々精進XP

よかろうもん

2018 年 08 月 15 日 (水)

ジョギング

  • 香椎浜 x 2 周
  • ギョーム後, 夜の方が涼しくて走りやすい...でも, 汗だくだった

日課

  • 腰と腰に痛みがあるので止めといた

香椎ミートアップ

  • 香椎近隣に住んでいるエンジニアの皆さんともりすで呑む
  • 色々と喋れて楽しかった

今日のるびぃ exercism 編 〜 Raindrops (1) 〜

ギョームで Ruby を触ることが無さすぎるので, exercism の問題を解くことにした.

概要

fizzbuzz っぽいやつ.

  • If the number has 3 as a factor, output 'Pling'.
  • If the number has 5 as a factor, output 'Plang'.
  • If the number has 7 as a factor, output 'Plong'.
  • If the number does not have 3, 5, or 7 as a factor,

テスト

require 'minitest/autorun'
require_relative 'raindrops'

# Common test data version: 1.0.0 9db5371
class RaindropsTest < Minitest::Test
  def test_the_sound_for_1_is_1
    # skip
    assert_equal "1", Raindrops.convert(1)
  end

  def test_the_sound_for_3_is_pling
    # skip
    assert_equal "Pling", Raindrops.convert(3)
  end

  def test_the_sound_for_5_is_plang
    # skip
    assert_equal "Plang", Raindrops.convert(5)
  end

  def test_the_sound_for_7_is_plong
    # skip
    assert_equal "Plong", Raindrops.convert(7)
  end

  def test_the_sound_for_6_is_pling_as_it_has_a_factor_3
    # skip
    assert_equal "Pling", Raindrops.convert(6)
  end

  def test_2_to_the_power_3_does_not_make_a_raindrop_sound_as_3_is_the_exponent_not_the_base
    # skip
    assert_equal "8", Raindrops.convert(8)
  end

  def test_the_sound_for_9_is_pling_as_it_has_a_factor_3
    # skip
    assert_equal "Pling", Raindrops.convert(9)
  end

  def test_the_sound_for_10_is_plang_as_it_has_a_factor_5
    # skip
    assert_equal "Plang", Raindrops.convert(10)
  end

  def test_the_sound_for_14_is_plong_as_it_has_a_factor_of_7
    # skip
    assert_equal "Plong", Raindrops.convert(14)
  end

  def test_the_sound_for_15_is_plingplang_as_it_has_factors_3_and_5
    # skip
    assert_equal "PlingPlang", Raindrops.convert(15)
  end

  def test_the_sound_for_21_is_plingplong_as_it_has_factors_3_and_7
    # skip
    assert_equal "PlingPlong", Raindrops.convert(21)
  end

  def test_the_sound_for_25_is_plang_as_it_has_a_factor_5
    # skip
    assert_equal "Plang", Raindrops.convert(25)
  end

  def test_the_sound_for_27_is_pling_as_it_has_a_factor_3
    # skip
    assert_equal "Pling", Raindrops.convert(27)
  end

  def test_the_sound_for_35_is_plangplong_as_it_has_factors_5_and_7
    # skip
    assert_equal "PlangPlong", Raindrops.convert(35)
  end

  def test_the_sound_for_49_is_plong_as_it_has_a_factor_7
    # skip
    assert_equal "Plong", Raindrops.convert(49)
  end

  def test_the_sound_for_52_is_52
    # skip
    assert_equal "52", Raindrops.convert(52)
  end

  def test_the_sound_for_105_is_plingplangplong_as_it_has_factors_3_5_and_7
    # skip
    assert_equal "PlingPlangPlong", Raindrops.convert(105)
  end

  def test_the_sound_for_3125_is_plang_as_it_has_a_factor_5
    # skip
    assert_equal "Plang", Raindrops.convert(3125)
  end

... 略 ...

  def test_bookkeeping
    skip
    assert_equal 3, BookKeeping::VERSION
  end
end

実装

最初の実装 (だいぶんイケてない)

class Raindrops
  class << self
    def convert(num)
      if num % 3 == 0 && num % 5 == 0 && num % 7 == 0 then
        'PlingPlangPlong'
      elsif num % 3 == 0 && num % 5 == 0 then
        'PlingPlang'
      elsif num % 3 == 0 && num % 7 == 0 then
        'PlingPlong'
      elsif num % 5 == 0 && num % 7 == 0 then
        'PlangPlong'
      elsif num % 3 == 0 then
        'Pling'
      elsif num % 5 == 0 then
        'Plang'
      elsif num % 7 == 0 then
        'Plong'
      else
        num.to_s
      end
    end
  end
end

一応, テストは通る.

$ ruby raindrops_test.rb
Run options: --seed 43598

# Running:

..............S....

Finished in 0.001604s, 11845.3866 runs/s, 11221.9452 assertions/s.

19 runs, 18 assertions, 0 failures, 0 errors, 1 skips

You have skipped tests. Run with --verbose for details.

次の実装 (ググった)

あまりにイケてなかったので, ググったらいくつかサンプルが公開されていたので, 参考にさせて頂いて以下のような実装にしてみた.

class Raindrops
  def self.convert(num)
    res = ''

    {
      3 => 'Pling',
      5 => 'Plang',
      7 => 'Plong'
    }.each do |n, w|
      res << w if num % n == 0
    end

    res.empty? ? num.to_s : res
  end
end

Run Test

$ ruby raindrops_test.rb
Run options: --seed 32293

# Running:

...............S...

Finished in 0.002184s, 8699.6337 runs/s, 8241.7582 assertions/s.

19 runs, 18 assertions, 0 failures, 0 errors, 1 skips

You have skipped tests. Run with --verbose for details.

いい感じ.

メモ

  • 最初の実装くらいしか出てこないのが, 今の自分の実力である...