iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 12
1

12 - Greed is Dice

Don't say so much, just coding...

Instruction

Greed is a dice game played with five six-sided dice. Your mission, should you choose to accept it, is to score a throw according to these rules. You will always be given an array with five six-sided dice values.

  Three 1's => 1000 points
  Three 6's =>  600 points
  Three 5's =>  500 points
  Three 4's =>  400 points
  Three 3's =>  300 points
  Three 2's =>  200 points
  One   1   =>  100 points
  One   5   =>   50 point

A single die can only be counted once in each roll. For example, a given "5" can only count as part of a triplet (contributing to the 500 points) or as a single 50 points, but not both in the same roll.

Example scoring

  Throw       Score
  ---------   ------------------
  5 1 3 4 1   250:  50 (for the 5) + 2 * 100 (for the 1s)
  1 1 1 3 1   1100: 1000 (for three 1s) + 100 (for the other 1)
  2 4 4 5 4   450:  400 (for three 4s) + 50 (for the 5)

In some languages, it is possible to mutate the input to the function. This is something that you should never do. If you mutate the input, you will not be able to pass all the tests.

Ruby

Init

  def score( dice )
    # Fill me in!
  end

Sample Testing

  describe "Scorer Function" do
    it "should value this as worthless" do
      Test.expect( score( [2, 3, 4, 6, 2] ) == 0,   "Should be 0 :-(" );
    end
    
    it "should value this triplet correctly" do
      Test.expect( score( [2, 2, 2, 3, 3] ) == 200, "Should be 200" );
    end
    
    it "should value this mixed set correctly" do
      Test.expect( score( [2, 4, 4, 5, 4] ) == 450, "Should be 450" );
    end
  end

Javascript

Init

  function score( dice ) {
    // Fill me in!
  }

Sample Testing

  describe( "Scorer Function", function() {
    it( "should value this as worthless", function() {
      Test.expect( score( [2, 3, 4, 6, 2] ) == 0,   "Should be 0 :-(" );
    });
    
    it( "should value this triplet correctly", function() {
      Test.expect( score( [4, 4, 4, 3, 3] ) == 400, "Should be 400" );
    });
    
    it( "should value this mixed set correctly", function() {
      Test.expect( score( [2, 4, 4, 5, 4] ) == 450, "Should be 450" );
    });
  });

Thinking

想法(1): 除了三個會有額外的分數之外,還要處理三個以上的時候,如果是 1 / 5 會另外需要計算單個
想法(2): 唯獨 1 的分數最高,其他都是該數 * 100 會得到分數
想法(3): 把所有可能會發生的條件列出來並加總

https://ithelp.ithome.com.tw/upload/images/20200927/20120826mK0sZjVH2U.jpg
圖片來源:Unsplash Freddie Marriage

Hint & Reference

  • Ruby
  • JavaScript

突然發現好像這題也沒有用其他多的方法啊 ·_·

Solution

Ruby

  # Solution 1
  def score(dice)
    [
      dice.count(1) / 3 * 1000,
      dice.count(6) / 3 * 600,
      dice.count(5) / 3 * 500,
      dice.count(4) / 3 * 400,
      dice.count(3) / 3 * 300,
      dice.count(2) / 3 * 200,
      dice.count(1) % 1 * 100,
      dice.count(5) % 5 * 50,
    ].reduce(:+)
  end
  
  # Solution 2
  def score (dice)
    sum = 0
    (1..6).each do |i|
      count = dice.select { |d| d == i }.size
  
      sum += (i==1 ? 1000 : i*100) if count >= 3
      sum += (count % 3)  * 100    if i == 1
      sum += (count % 3)  *  50    if i == 5
    end
  
    sum
  end

Javascript

  // Solution 1
  function score(dice) {
    var sum = 0
    for(i=1; i<=6; i++){
      var count = dice.filter( d => d == i).length
      if(count >= 3){
        sum += (i==1 ? 1000 : i*100)
      }
      if(i == 1){
        sum += (count % 3) * 100 
      }
      if(i == 5){
        sum += (count % 3) * 50
      }
    }
    return sum
  }

上一篇
見習村11 - Extract the domain name from a URL
下一篇
見習村13 - int32 to IPv4
系列文
見習村-30 Day CodeWars Challenge30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言