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 == 50return [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 == 21end- 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 "knows how to proceed quickly" do expect(best_moves(301)).to eq([Throw.new(20, 'T'), Throw.new(20, 'T'), Throw.new(20, 'T')]) end it "should return the optimal moves" do expect(best_moves(50)).to eq([Throw.new(50)]) expect(best_moves(170)).to eq([Throw.new(20, 'T'), Throw.new(20, 'T'), Throw.new(50)]) # expect(best_moves(5)).to needs to be either [1,4] or [1,2,2] end it "should return optimal moves for 141" do #expect(best_moves(141)).to eq([60,57,24]) expect(best_moves(141).last.last?).to be true expect(best_moves(141).map(&:value).sum).to eq(141) 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 "knows how to proceed quickly" do
expect(best_moves(301)).to eq([60,60,60])- expect(best_moves(301)).to eq([Throw.new(20, 'T'), Throw.new(20, 'T'), Throw.new(20, 'T')])
- end
- it "should return the optimal moves" do
expect(best_moves(50)).to eq([50])- expect(best_moves(50)).to eq([Throw.new(50)])
- expect(best_moves(170)).to eq([Throw.new(20, 'T'), Throw.new(20, 'T'), Throw.new(50)])
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_evenexpect(best_moves(141).last =< 50).to be_true- # expect(best_moves(5)).to needs to be either [1,4] or [1,2,2]
- end
- it "should return optimal moves for 141" do
- #expect(best_moves(141)).to eq([60,57,24])
- expect(best_moves(141).last.last?).to be true
- expect(best_moves(141).map(&:value).sum).to eq(141)
- 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
- 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
# 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