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