Ad
Code
Diff
  • def best_moves(score)
      # return the three best moves to reach 0 from the given score
      if (score.even? && score <= 40)
        return [Throw.new(score / 2, 'D')]
      end
      if score == 50 
        return [Throw.new(50)]
      end
      if score >= 182
        return [Throw.new(20, 'T'), Throw.new(20, 'T'), Throw.new(20, 'T')]
      end
      if score == 170
        return [Throw.new(20, 'T'), Throw.new(20, 'T'), Throw.new(50)]
      end
      if score == 141
        return [Throw.new(19, 'T'), Throw.new(16, 'T'), Throw.new(18, 'D')]
      end
    end
    • def best_moves(score)
    • # return the three best moves to reach 0 from the given score
    • if (score.even? && score <= 40) || score == 50
    • return [score]
    • if (score.even? && score <= 40)
    • return [Throw.new(score / 2, 'D')]
    • end
    • if score == 50
    • return [Throw.new(50)]
    • end
    • if score >= 182
    • return [60, 60, 60]
    • return [Throw.new(20, 'T'), Throw.new(20, 'T'), Throw.new(20, 'T')]
    • end
    • if score == 170
    • return [60,60,50]
    • return [Throw.new(20, 'T'), Throw.new(20, 'T'), Throw.new(50)]
    • end
    • if score == 141
    • return [Throw.new(19, 'T'), Throw.new(16, 'T'), Throw.new(18, 'D')]
    • end
    • if score == 21
    • end
    • end
Code
Diff
  • def best_moves(score)
      # return the three best moves to reach 0 from the given score
      if (score.is_even && score <= 40) || score == 50 do
        return [score]
      end
      if score >= 182 do
        return [60, 60, 60]
      end
      if score == 21 do
      end  
    end
    • def best_moves(score)
    • # return the three best moves to reach 0 from the given score
    • if (score.is_even && score <= 40) || score == 50 do
    • return [score]
    • end
    • if score >= 182 do
    • return [60, 60, 60]
    • end
    • if score == 21 do
    • end
    • end
Test Cases
Diff
  • # From Ruby 3.0, RSpec is used under the hood.
    # See https://rspec.info/
    # Defaults to the global `describe` for backwards compatibility, but `RSpec.desribe` works as well.
    describe "Example" do
      it "should return the optimal moves" do
        expect(best_moves(301)).to eq([60,60,60])
        expect(best_moves(170)).to eq([60,60,50])
        expect(best_moves(141)).to eq([60,57,24])
        expect(best_moves(141).last).to be_even
        # expect(best_moves(5)).to needs to be either [1,4] or [1,2,2]
      end
      it "" do
      end
    end
    
    • # From Ruby 3.0, RSpec is used under the hood.
    • # See https://rspec.info/
    • # Defaults to the global `describe` for backwards compatibility, but `RSpec.desribe` works as well.
    • describe "Example" do
    • it "should return the optimal moves" do
    • expect(best_moves(301)).to eq([60,60,60])
    • expect(best_moves(170)).to eq([60,60,50])
    • expect(best_moves(141)).to eq([60,57,24])
    • expect(best_moves(141).last.even?).to be_true
    • expect(best_moves(141).last).to be_even
    • # expect(best_moves(5)).to needs to be either [1,4] or [1,2,2]
    • # The following is still supported, but new tests should now use them.
    • # Test.assert_equals(add(1, 1), 2)
    • end
    • it "" do
    • end
    • end