Constructing a Sample Space Practice Problem
This data science coding problem helps you practice Probability Fundamentals, constructing a sample space, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Probability Fundamentals.
- Problem ID: 220
- Problem key: 220-constructing-a-sample-space
- URL: https://datacrack.app/solve/220-constructing-a-sample-space
- Difficulty: easy
- Topic: Probability Fundamentals
- Module: Probability
Problem Statement
# 🧩 Constructing a Sample Space
---
### 🎯 Goal
Construct the sample space for flipping a fair coin twice, then calculate the probability of a given event.
---
### 📖 Introduction
In the previous problem, the sample space was already given.
You used:
$$
P(A)=\frac{|A|}{|\Omega|}
$$
In this problem, the event is still given, but you must first construct the sample space from the experiment.
The experiment is:
```text
flip a fair coin twice
```
One coin flip has two possible outcomes:
$$
\{H,T\}
$$
When the coin is flipped twice, each outcome from the first flip can be followed by either outcome from the second flip.
Order matters:
- `HT` means heads first, then tails.
- `TH` means tails first, then heads.
Your job is to construct **all possible two-flip outcomes**, then use the given event to calculate its probability.
---
### 🔍 Explanation of Symbols
| Symbol | Meaning |
|:--:|:--|
| $\Omega$ | Sample space containing all possible outcomes |
| $A$ | Event containing the favorable outcomes |
| $\lvert A\rvert$ | Number of outcomes in event $A$ |
| $\lvert \Omega\rvert$ | Total number of outcomes in the sample space |
| $P(A)$ | Probability that event $A$ occurs |
---
### 📥 Input
- `event`: a valid subset of the two-coin sample space.
### 📤 Output
- Return the probability of the event rounded to 6 decimals.
---
### 💻 Task
Implement `two_coin_probability(event)`.
Your function should:
1. Define the possible result of one coin flip: `H` or `T`.
2. Construct all ordered outcomes of two flips.
3. Use those outcomes as the sample space.
4. Use the provided `event`.
5. Calculate $P(A)=\frac{|A|}{|\Omega|}$.
6. Return the probability rounded to 6 decimals.
Assume the event is valid. Do not add validation logic for invalid events.
---
### 🧩 Starter Code
```python
def two_coin_probability(event):
pass
```
---
### 💡 Example
```python
two_coin_probability({"HT", "TH"})
```
Here, the event contains the outcomes where exactly one flip is heads.
Your function should:
1. Construct the complete sample space for two coin flips.
2. Count the outcomes in the given event.
3. Calculate:
$$
P(A)=\frac{|A|}{|\Omega|}
$$
---
Starter Code
def two_coin_probability(event):
pass