From 9843137e3369065bf01a2920f4cc54a26ec6bfa8 Mon Sep 17 00:00:00 2001 From: "ai-sdlc[bot]" Date: Fri, 27 Nov 2026 02:21:54 +0000 Subject: [PATCH] fix(ai-sdlc): address issue #64 [skip release] Signed-off-by: ai-sdlc[bot] --- kv/__init__.py | 3 ++- kv/key_count_with_value.py | 8 ++++++++ kv/test_key_count_with_value.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 1 deletion(-) create mode 100644 kv/key_count_with_value.py create mode 100644 kv/test_key_count_with_value.py diff --git a/kv/__init__.py b/kv/__init__.py index 2857f5c..3e96533 100644 --- a/kv/__init__.py +++ b/kv/__init__.py @@ -2,6 +2,7 @@ from kv.count_prefix import count_prefix from kv.count_values import count_values +from kv.key_count_with_value import key_count_with_value from kv.filter_prefix import filter_prefix from kv.has_all import has_all from kv.invert import invert @@ -14,4 +15,4 @@ from kv.sorted_keys import sorted_keys from kv.values_with_prefix import values_with_prefix -__all__ = ["count_prefix", "count_values", "filter_prefix", "has_all", "invert", "max_value_key", "merge", "min_key", "omit", "pick", "rename_key", "sorted_keys", "values_with_prefix"] +__all__ = ["count_prefix", "count_values", "key_count_with_value", "filter_prefix", "has_all", "invert", "max_value_key", "merge", "min_key", "omit", "pick", "rename_key", "sorted_keys", "values_with_prefix"] diff --git a/kv/key_count_with_value.py b/kv/key_count_with_value.py new file mode 100644 index 0000000..d16b06a --- /dev/null +++ b/kv/key_count_with_value.py @@ -0,0 +1,8 @@ +def key_count_with_value(m: dict[str, str], value: str) -> int: + """Count keys whose value equals the given value. + + Returns 0 for an empty dict. + """ + if not m: + return 0 + return sum(1 for v in m.values() if v == value) diff --git a/kv/test_key_count_with_value.py b/kv/test_key_count_with_value.py new file mode 100644 index 0000000..87ee841 --- /dev/null +++ b/kv/test_key_count_with_value.py @@ -0,0 +1,30 @@ +from kv.key_count_with_value import key_count_with_value + + +def test_empty_dict(): + """Empty dict returns 0.""" + assert key_count_with_value({}, "any") == 0 + + +def test_no_match(): + """No keys have the target value.""" + m = {"a": "1", "b": "2", "c": "3"} + assert key_count_with_value(m, "4") == 0 + + +def test_several_matches(): + """Several keys have the target value.""" + m = {"a": "x", "b": "y", "c": "x", "d": "x"} + assert key_count_with_value(m, "x") == 3 + + +def test_single_match(): + """Single key has the target value.""" + m = {"a": "1", "b": "2", "c": "3"} + assert key_count_with_value(m, "2") == 1 + + +def test_all_match(): + """All keys have the target value.""" + m = {"a": "same", "b": "same", "c": "same"} + assert key_count_with_value(m, "same") == 3