Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added new question
  • Loading branch information
Jeet009 committed Sep 23, 2025
commit cd33bcf4a4e337b03dd11f66778c312379bd059b
42 changes: 42 additions & 0 deletions build/184.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"id": "184",
"title": "Empirical Probability Mass Function (PMF)",
"difficulty": "easy",
"category": "Probability & Statistics",
"video": "",
"likes": "0",
"dislikes": "0",
"contributor": [
{
"profile_link": "https://github.com/Jeet009",
"name": "Jeet Mukherjee"
}
],
"description": "## Problem\n\nGiven a list of integer samples drawn from a discrete distribution, implement a function to compute the empirical Probability Mass Function (PMF). The function should return a list of `(value, probability)` pairs sorted by the value in ascending order. If the input is empty, return an empty list.",
"learn_section": "\n# Learn Section\n\n# Probability Mass Function (PMF) — Simple Explanation\n\nA **probability mass function (PMF)** describes how probabilities are assigned to the possible outcomes of a **discrete random variable**.\n\n- It tells you the chance of each specific outcome. \n- Each probability is non-negative. \n- The total of all probabilities adds up to 1.\n\n## Estimating from data\nIf the true probabilities are unknown, you can estimate them with an **empirical PMF**:\n- Count how often each outcome appears. \n- Divide by the total number of observations. \n\n## Example\nObserved sequence: `1, 2, 2, 3, 3, 3` (6 outcomes total)\n- \"1\" appears once → estimated probability = 1/6 \n- \"2\" appears twice → estimated probability = 2/6 = 1/3 \n- \"3\" appears three times → estimated probability = 3/6 = 1/2 \n\n\n ",
"starter_code": "def empirical_pmf(samples):\n \"\"\"\n Given an iterable of integer samples, return a list of (value, probability)\n pairs sorted by value ascending.\n \"\"\"\n # TODO: Implement the function\n pass",
"solution": "from collections import Counter\n\ndef empirical_pmf(samples):\n \"\"\"\n Given an iterable of integer samples, return a list of (value, probability)\n pairs sorted by value ascending.\n \"\"\"\n samples = list(samples)\n if not samples:\n return []\n total = len(samples)\n cnt = Counter(samples)\n result = [(k, cnt[k] / total) for k in sorted(cnt.keys())]\n return result",
"example": {
"input": "samples = [1, 2, 2, 3, 3, 3]",
"output": "[(1, 0.16666666666666666), (2, 0.3333333333333333), (3, 0.5)]",
"reasoning": "Counts are {1:1, 2:2, 3:3} over 6 samples, so probabilities are 1/6, 2/6, and 3/6 respectively, returned sorted by value."
},
"test_cases": [
{
"test": "print(empirical_pmf([1, 2, 2, 3, 3, 3]))",
"expected_output": "[(1, 0.16666666666666666), (2, 0.3333333333333333), (3, 0.5)]"
},
{
"test": "print(empirical_pmf([5, 5, 5, 5]))",
"expected_output": "[(5, 1.0)]"
},
{
"test": "print(empirical_pmf([]))",
"expected_output": "[]"
},
{
"test": "print(empirical_pmf([0, 0, 1, 1, 1, 2]))",
"expected_output": "[(0, 0.3333333333333333), (1, 0.5), (2, 0.16666666666666666)]"
}
]
}
72 changes: 72 additions & 0 deletions build/187.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"id": "187",
"title": "Probability Addition Law: Compute P(A ∪ B)",
"difficulty": "easy",
"category": "Probability & Statistics",
"video": "",
"likes": "0",
"dislikes": "0",
"contributor": [
{
"profile_link": "https://github.com/Jeet009",
"name": "Jeet Mukherjee"
}
],
"tinygrad_difficulty": "easy",
"pytorch_difficulty": "easy",
"description": "## Problem\n\nTwo events `A` and `B` in a probability space have the following probabilities:\n\n- P(A) = 0.6\n- P(B) = 0.5\n- P(A ∩ B) = 0.3\n\nUsing the probability addition law, compute `P(A ∪ B)`.\n\nImplement a function `prob_union(p_a, p_b, p_intersection)` that returns `P(A ∪ B)` as a float.\n\nRecall: P(A ∪ B) = P(A) + P(B) − P(A ∩ B).\n\nNote: If `A` and `B` are mutually exclusive (disjoint), then `P(A ∩ B) = 0` and the rule simplifies to `P(A ∪ B) = P(A) + P(B)`.",
"learn_section": "## Solution Explanation\n\nThe probability addition law for any two events A and B states:\n\n$$\nP(A \\cup B) = P(A) + P(B) - P(A \\cap B)\n$$\n\n- The union counts outcomes in A or B (or both).\n- We subtract the intersection once to correct double-counting.\n\n### Mutually exclusive (disjoint) events\nIf A and B cannot occur together, then \\(P(A \\cap B) = 0\\) and the addition rule simplifies to:\n\\[\nP(A \\cup B) = P(A) + P(B)\n\\]\n\n### Plug in the given values\n\nGiven: \\(P(A)=0.6\\), \\(P(B)=0.5\\), \\(P(A \\cap B)=0.3\\)\n\n\\[\nP(A \\cup B) = 0.6 + 0.5 - 0.3 = 0.8\n\\]\n\n### Validity checks\n- Probabilities must lie in [0, 1]. The result 0.8 is valid.\n- Given inputs must satisfy: \\(0 \\le P(A \\cap B) \\le \\min\\{P(A), P(B)\\}\\) and \\(P(A \\cap B) \\ge P(A) + P(B) - 1\\). Here, 0.3 is within [0.1, 0.5], so inputs are consistent.\n\n### Implementation outline\n- Accept three floats: `p_a`, `p_b`, `p_intersection`.\n- Optionally assert basic bounds to help users catch mistakes.\n- Return `p_a + p_b - p_intersection`.",
"starter_code": "# Implement your function below.\n\ndef prob_union(p_a: float, p_b: float, p_intersection: float) -> float:\n\t\"\"\"Return P(A ∪ B) using the addition law.\n\n\tAuto-detects mutually exclusive events by treating very small P(A ∩ B) as 0.\n\n\tArguments:\n\t- p_a: P(A)\n\t- p_b: P(B)\n\t- p_intersection: P(A ∩ B)\n\n\tReturns:\n\t- float: P(A ∪ B)\n\t\"\"\"\n\t# TODO: if p_intersection is ~0, return p_a + p_b; else return p_a + p_b - p_intersection\n\traise NotImplementedError",
"solution": "def prob_union(p_a: float, p_b: float, p_intersection: float) -> float:\n\t\"\"\"Reference implementation for P(A ∪ B) with auto-detection of mutual exclusivity.\n\n\tIf p_intersection is numerically very small (≤ 1e-12), treat as 0 and\n\tuse the simplified rule P(A ∪ B) = P(A) + P(B).\n\t\"\"\"\n\tepsilon = 1e-12\n\tif p_intersection <= epsilon:\n\t\treturn p_a + p_b\n\treturn p_a + p_b - p_intersection",
"example": {
"input": "prob_union(0.6, 0.5, 0.3)",
"output": "0.8",
"reasoning": "By addition law: 0.6 + 0.5 − 0.3 = 0.8."
},
"test_cases": [
{
"test": "from solution import prob_union; print(prob_union(0.6, 0.5, 0.3))",
"expected_output": "0.8"
},
{
"test": "from solution import prob_union; print(prob_union(0.2, 0.4, 0.1))",
"expected_output": "0.5"
},
{
"test": "from solution import prob_union; print(prob_union(0.3, 0.2, 0.0))",
"expected_output": "0.5"
}
],
"tinygrad_starter_code": "def prob_union(p_a: float, p_b: float, p_intersection: float) -> float:\n\t\"\"\"Return P(A ∪ B). Treat very small P(A ∩ B) as 0 (mutually exclusive).\"\"\"\n\traise NotImplementedError",
"tinygrad_solution": "def prob_union(p_a: float, p_b: float, p_intersection: float) -> float:\n\t\"\"\"Reference implementation for P(A ∪ B) with auto-detection (Tinygrad track).\"\"\"\n\tepsilon = 1e-12\n\tif p_intersection <= epsilon:\n\t\treturn p_a + p_b\n\treturn p_a + p_b - p_intersection",
"tinygrad_test_cases": [
{
"test": "from solution import prob_union; print(prob_union(0.6, 0.5, 0.3))",
"expected_output": "0.8"
},
{
"test": "from solution import prob_union; print(prob_union(0.2, 0.4, 0.1))",
"expected_output": "0.5"
},
{
"test": "from solution import prob_union; print(prob_union(0.3, 0.2, 0.0))",
"expected_output": "0.5"
}
],
"pytorch_starter_code": "def prob_union(p_a: float, p_b: float, p_intersection: float) -> float:\n\t\"\"\"Return P(A ∪ B). Treat very small P(A ∩ B) as 0 (mutually exclusive).\"\"\"\n\traise NotImplementedError",
"pytorch_solution": "def prob_union(p_a: float, p_b: float, p_intersection: float) -> float:\n\t\"\"\"Reference implementation for P(A ∪ B) with auto-detection (PyTorch track).\"\"\"\n\tepsilon = 1e-12\n\tif p_intersection <= epsilon:\n\t\treturn p_a + p_b\n\treturn p_a + p_b - p_intersection",
"pytorch_test_cases": [
{
"test": "from solution import prob_union; print(prob_union(0.6, 0.5, 0.3))",
"expected_output": "0.8"
},
{
"test": "from solution import prob_union; print(prob_union(0.2, 0.4, 0.1))",
"expected_output": "0.5"
},
{
"test": "from solution import prob_union; print(prob_union(0.3, 0.2, 0.0))",
"expected_output": "0.5"
}
]
}
16 changes: 0 additions & 16 deletions questions/186_pmf_normalization_constant 2/description.md

This file was deleted.

5 changes: 0 additions & 5 deletions questions/186_pmf_normalization_constant 2/example.json

This file was deleted.

50 changes: 0 additions & 50 deletions questions/186_pmf_normalization_constant 2/learn.md

This file was deleted.

14 changes: 0 additions & 14 deletions questions/186_pmf_normalization_constant 2/solution.py

This file was deleted.

6 changes: 0 additions & 6 deletions questions/186_pmf_normalization_constant 2/starter_code.py

This file was deleted.

10 changes: 0 additions & 10 deletions questions/186_pmf_normalization_constant 2/tests.json

This file was deleted.

15 changes: 15 additions & 0 deletions questions/187_probability-addition-law/description.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## Problem

Two events `A` and `B` in a probability space have the following probabilities:

- P(A) = 0.6
- P(B) = 0.5
- P(A ∩ B) = 0.3

Using the probability addition law, compute `P(A ∪ B)`.

Implement a function `prob_union(p_a, p_b, p_intersection)` that returns `P(A ∪ B)` as a float.

Recall: P(A ∪ B) = P(A) + P(B) − P(A ∩ B).

Note: If `A` and `B` are mutually exclusive (disjoint), then `P(A ∩ B) = 0` and the rule simplifies to `P(A ∪ B) = P(A) + P(B)`.
5 changes: 5 additions & 0 deletions questions/187_probability-addition-law/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"input": "prob_union(0.6, 0.5, 0.3)",
"output": "0.8",
"reasoning": "By addition law: 0.6 + 0.5 − 0.3 = 0.8."
}
33 changes: 33 additions & 0 deletions questions/187_probability-addition-law/learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
## Solution Explanation

The probability addition law for any two events A and B states:

$$
P(A \cup B) = P(A) + P(B) - P(A \cap B)
$$

- The union counts outcomes in A or B (or both).
- We subtract the intersection once to correct double-counting.

### Mutually exclusive (disjoint) events
If A and B cannot occur together, then \(P(A \cap B) = 0\) and the addition rule simplifies to:
\[
P(A \cup B) = P(A) + P(B)
\]

### Plug in the given values

Given: \(P(A)=0.6\), \(P(B)=0.5\), \(P(A \cap B)=0.3\)

\[
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for single line formulas use $ and for multiple lines use $$

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay Sure. Will keep this in mind.

P(A \cup B) = 0.6 + 0.5 - 0.3 = 0.8
\]

### Validity checks
- Probabilities must lie in [0, 1]. The result 0.8 is valid.
- Given inputs must satisfy: \(0 \le P(A \cap B) \le \min\{P(A), P(B)\}\) and \(P(A \cap B) \ge P(A) + P(B) - 1\). Here, 0.3 is within [0.1, 0.5], so inputs are consistent.

### Implementation outline
- Accept three floats: `p_a`, `p_b`, `p_intersection`.
- Optionally assert basic bounds to help users catch mistakes.
- Return `p_a + p_b - p_intersection`.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"id": "185",
"title": "Find PMF Normalization Constant",
"difficulty": "medium",
"category": "Probability & Statistics",
"id": "187",
"title": "Probability Addition Law: Compute P(A ∪ B)",
"difficulty": "easy",
"category": "Probability",
"video": "",
"likes": "0",
"dislikes": "0",
Expand Down
10 changes: 10 additions & 0 deletions questions/187_probability-addition-law/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
def prob_union(p_a: float, p_b: float, p_intersection: float) -> float:
"""Reference implementation for P(A ∪ B) with auto-detection of mutual exclusivity.

If p_intersection is numerically very small (≤ 1e-12), treat as 0 and
use the simplified rule P(A ∪ B) = P(A) + P(B).
"""
epsilon = 1e-12
if p_intersection <= epsilon:
return p_a + p_b
return p_a + p_b - p_intersection
17 changes: 17 additions & 0 deletions questions/187_probability-addition-law/starter_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Implement your function below.

def prob_union(p_a: float, p_b: float, p_intersection: float) -> float:
"""Return P(A ∪ B) using the addition law.

Auto-detects mutually exclusive events by treating very small P(A ∩ B) as 0.

Arguments:
- p_a: P(A)
- p_b: P(B)
- p_intersection: P(A ∩ B)

Returns:
- float: P(A ∪ B)
"""
# TODO: if p_intersection is ~0, return p_a + p_b; else return p_a + p_b - p_intersection
raise NotImplementedError
5 changes: 5 additions & 0 deletions questions/187_probability-addition-law/tests.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{ "test": "from solution import prob_union; print(prob_union(0.6, 0.5, 0.3))", "expected_output": "0.8" },
{ "test": "from solution import prob_union; print(prob_union(0.2, 0.4, 0.1))", "expected_output": "0.5" },
{ "test": "from solution import prob_union; print(prob_union(0.3, 0.2, 0.0))", "expected_output": "0.5" }
]