Dice Duel Wager: Expected Profit and Play Decision
Math & Bit ManipulationAuto-graded · 18 test casesLast updated
Compute the expected profit of a two-dice wager where ties favor the opponent, and decide whether a risk-neutral player should accept it.
You are offered a wager against an opponent. You and the opponent each roll one fair six-sided die exactly once. The two rolls are independent, and every face from 1 to 6 is equally likely for each die.
The payment rules are:
- If your roll is strictly greater than the opponent's roll, the opponent pays you
win_amountdollars. - Otherwise (your roll is smaller than or equal to the opponent's roll), you pay the opponent
loss_amountdollars.
Write a function that computes your expected profit from a single round and decides whether a risk-neutral player should accept the wager.
Task
Given win_amount and loss_amount, return a dictionary with exactly two keys:
"expected_value": the expected profit (in dollars) of one round, from your point of view. A loss counts as negative profit."should_play": a boolean that isTrueonly if the expected profit is strictly greater than zero. If the expected profit is exactly zero or negative, it isFalse.
Notes
- Ties are always losses for you, so they use the
loss_amountpayment. - The expected value should be computed from the exact probabilities of the 36 equally likely ordered roll pairs.
- At the break-even point the expected value is zero and the answer to whether to play is
False.
Constraints
win_amountandloss_amountare non-negative numbers (integers or floats).- Both dice are fair, six-sided, and independent.
- The returned dictionary must contain exactly the keys
"expected_value"and"should_play".
Examples
Example 1
Input: dice_wager_decision(10, 5)
Output: {'expected_value': 1.25, 'should_play': True}
Example 2
Input: dice_wager_decision(7, 5)
Output: {'expected_value': 0, 'should_play': False}
Example 3
Input: dice_wager_decision(14, 10)
Output: {'expected_value': 0, 'should_play': False}
Hints
Hint 1
List all 36 ordered pairs of rolls. How many of them have your roll strictly greater than the opponent's roll? Remember that each pair has the same probability.
Hint 2
Ties (6 pairs) count as losses for you, so the loss payment applies to 15 + 6 = 21 outcomes, while the win payment applies to 15 outcomes.
Hint 3
The expected profit is (15 * win_amount - 21 * loss_amount) / 36. Play only when this is strictly positive, so a break-even game returns False.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Two fair dice give 36 equally likely ordered outcomes, each with probability 1/36.
- Ties: 6 outcomes (1,1) through (6,6).
- Your roll is higher: if you roll
k, you beatk - 1of the opponent's faces, so the count is 0 + 1 + 2 + 3 + 4 + 5 = 15. (By symmetry, the 30 non-tie outcomes split evenly into 15 and 15.) - Opponent higher or tie: 15 + 6 = 21 outcomes.
So you receive win_amount with probability 15/36 = 5/12, and you pay loss_amount with probability 21/36 = 7/12.
The expected profit is:
E = (15 * win_amount - 21 * loss_amount) / 36
which simplifies to (5 * win_amount - 7 * loss_amount) / 12.
A risk-neutral player should accept the wager only when E > 0, which is equivalent to win_amount / loss_amount > 7/5. At exactly zero expected value the function returns False.
Why it is correct
The formula sums payoff times probability over all outcomes, and the outcome counts cover all 36 pairs exactly once (15 wins plus 21 non-wins). Ties are placed on the loss side, as the rules require. The decision uses a strict inequality, so break-even cases such as (7, 5) and (14, 10) correctly return False.
Complexity
- Time: O(1)
- Space: O(1)
Reference solution
from typing import Dict
def dice_wager_decision(win_amount, loss_amount):
expected_value = (15 * win_amount - 21 * loss_amount) / 36
return {
"expected_value": expected_value,
"should_play": expected_value > 0,
}