๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿƒ‍โ™€๏ธ ํ™œ๋™/ํ”„๋กœ์ ํŠธ ์ž‘์—…์ผ์ง€

Phase 3: Strategies of the Game

Problem 8: make_averaged function ๊ตฌํ˜„

  • make_averaged function
    • original_function์„ ์ธ์ž๋กœ ๋ฐ›๋Š” high-order function
    • return๊ฐ’์€ original_function๊ณผ ๋™์ผํ•œ ์ˆ˜์˜ ์ธ์ˆ˜๋ฅผ ๋ฐ›๋Š” ํ•จ์ˆ˜์ด๋‹ค.
    • ์ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์ด๊ฒƒ์€ original_function์„ ๋ฐ˜๋ณต์ ์œผ๋กœ ํ˜ธ์ถœํ•œ ํ‰๊ท ๊ฐ’์„ ๋ฆฌํ„ดํ•  ๊ฒƒ์ด๋‹ค.
    • ํŠนํžˆ, ์ด ํ•จ์ˆ˜๋Š” original_function์„ total_samplesํšŸ์ˆ˜๋งŒํผ ํ˜ธ์ถœํ•ด์•ผํ•˜๊ณ  ์ด ํ˜ธ์ถœ๋“ค์˜ ๊ฒฐ๊ณผ์˜ ํ‰๊ท ์„ ๋ฐ˜ํ™˜ํ•ด์•ผ ํ•œ๋‹ค.
  • ์ฃผ์˜ํ•  ์ 
    • ๊ธฐ๋ณธ ๋ฃฐ์€ ๊ตด๋ฆฐ dice์˜ ์ˆ˜์˜ ํ•ฉ์„ ๊ตฌํ•˜๋Š” ๊ฒƒ. ์—ฌ๊ธฐ์— ๊ทœ์น™์„ ์ ์šฉํ•˜๋Š” ๊ฒƒ์ž„ ์ฃผ์˜
def make_averaged(original_function, total_samples=1000):
    """Return a function that returns the average value of ORIGINAL_FUNCTION
    called TOTAL_SAMPLES times.

    To implement this function, you will have to use *args syntax, a new Python
    feature introduced in this project.  See the project description.

    >>> dice = make_test_dice(4, 2, 5, 1)
    >>> averaged_dice = make_averaged(roll_dice, 1000)
    >>> averaged_dice(1, dice)
    3.0
    """
    # BEGIN PROBLEM 8
    def f(*args):
        sum = 0
        for i in range(total_samples):
            sum += original_function(*args)
        return sum / total_samples
    return f

    # END PROBLEM 8

Problem 9: max_scoring_num_rolls ํ•จ์ˆ˜ ๊ตฌํ˜„ํ•˜๊ธฐ

  • max_scoring_num_rolls
    • turn๋งˆ๋‹ค ์ตœ๋Œ€ ํ‰๊ท  ์ ์ˆ˜๋ฅผ ์ œ๊ณตํ•˜๋Š” ํ•จ์ˆ˜
      • ์ฃผ์‚ฌ์œ„๋ฅผ 1ํšŒ๋ถ€ํ„ฐ 10ํšŒ๊นŒ์ง€ ๋˜์ง€๊ณ (๊ทธ๊ฒŒ ๊ฐ๊ฐ์˜ turn)
      • turn๋งˆ๋‹ค ๋‚˜์˜จ average๊ฐ’๋“ค์„ ๋น„๊ตํ•ด์„œ ๊ทธ์ค‘์— max๊ฐ’์„ ๋‚˜์˜ค๊ฒŒ ํ•˜๋Š” number of rolls๋ฅผ ๋ฆฌํ„ดํ•จ
    • make_averaged์™€ roll_dice๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค.
    • ๋งŒ์•ฝ ๋‘ roll์ˆซ์ž๊ฐ€ ์ตœ๋Œ€ํ‰๊ท ์ ์ˆ˜์— ๋Œ€ํ•ด ๋™์ ์ธ ๊ฒฝ์šฐ ๋” ๋‚ฎ์€ ์ˆซ์ž๋ฅผ ๋ฐ˜ํ™˜ํ•จ
      • if both 3 and 6 achieve a maximum average score, return 3.
def max_scoring_num_rolls(dice=six_sided, total_samples=1000):
    """Return the number of dice (1 to 10) that gives the highest average turn score
    by calling roll_dice with the provided DICE a total of TOTAL_SAMPLES times.
    Assume that the dice always return positive outcomes.

    >>> dice = make_test_dice(1, 6)
    >>> max_scoring_num_rolls(dice)
    1
    """
    # BEGIN PROBLEM 9
    max = 0
    max_index = 0
    dice_average = make_averaged(roll_dice, total_samples)
    for i in range(1, 11):
        tmp = dice_average(i, dice)
        if tmp > max:
            max_index = i
            max = tmp
    return max_index

    # END PROBLEM 9

Problem 10: oink_points_strategy ๊ตฌํ˜„ํ•˜๊ธฐ

  • oink_points_strategy
    • 0์„ ๊ตด๋ฆฌ๋Š”๊ฒŒ ๊ฐ€์žฅ ์œ ๋ฆฌํ•  ๋•Œ ์ด ์ „๋žต์„ ์‚ฌ์šฉํ•œ๋‹ค.
    • ์ด ์ „๋žต์€ threshold๋กœ ๋„˜๊ฒจ์ค€ ๊ฐ’๋ณด๋‹ค ๊ฐ™๊ฑฐ๋‚˜ ํฌ๋ฉด 0์„ ๋ฆฌํ„ดํ•˜๊ฒŒ ํ•˜๊ณ 
      ์•ˆ ๋„˜์œผ๋ฉด num_rolls๋กœ ๋ฐ›์•„์˜จ ๊ฐ’์„ ์ด์šฉํ•œ๋‹ค.
    • ์ด ์ „๋žต์€ Pigs on Prime ๊ทœ์น™์„ ๊ณ ๋ คํ•˜์ง€ ์•Š๋Š”๋‹ค.
def oink_points_strategy(score, opponent_score, threshold=8, num_rolls=6):
    """This strategy returns 0 dice if that gives at least THRESHOLD points, and
    returns NUM_ROLLS otherwise.
    """
    # BEGIN PROBLEM 10
    o_points = oink_points(score, opponent_score)
    if o_points >= threshold:
        return 0
    return num_rolls    
    # END PROBLEM 10

Problem 11: pigs_on_prime_strategy ๊ตฌํ˜„ํ•˜๊ธฐ

  • pigs_on_prime_strategy
    • ์ด ์ „๋žต์€ ์ด ๊ทœ์น™์„ ์œ ๋ฐœํ•˜๊ณ  0์ด ์•„๋‹Œ ์ ์ˆ˜๋ฅผ ์–ป๋Š” ๊ฒฝ์šฐ์— ํ•ญ์ƒ 0์„ ๊ตด๋ฆฐ๋‹ค
    • ๋‹ค๋ฅธ ๊ฒฝ์šฐ์—๋Š” ๋งŒ์•ฝ threshold๊ฐ’ ์ด์ƒ์ด๋ฉด 0์„ ๊ตด๋ฆฌ๊ณ  ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด num_rolls๊ฐ’์„ ๊ตด๋ฆฐ๋‹ค.
def pigs_on_prime_strategy(score, opponent_score, threshold=8, num_rolls=6):
    """This strategy returns 0 dice when this would result in Pigs on Prime taking
    effect. It also returns 0 dice if it gives at least THRESHOLD points.
    Otherwise, it returns NUM_ROLLS.
    """
    # BEGIN PROBLEM 11
    if is_prime(oink_points(score, opponent_score) + score):
        return 0
    else:
        return oink_points_strategy(score, opponent_score, threshold, num_rolls)
    # END PROBLEM 11