Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # --------- tasks/majority.py --------------
- def is_majority(items: list) -> bool:
- true_count = 0
- false_count = 0
- for i in items:
- if i is True:
- true_count += 1
- else:
- false_count += 1
- return true_count > false_count
- def is_majority_count(items) -> bool:
- true_count = items.count(True)
- false_count = items.count(False)
- return true_count > false_count
- def is_majority_len(items) -> bool:
- true_count = items.count(True)
- return true_count * 2 > len(items)
- def is_majority_sum(items) -> bool:
- true_count = sum(items)
- return true_count * 2 > len(items)
- def is_majority_agg(items) -> bool:
- return sum([-1, 1][i] for i in items) > 0
- # works for python 3.10+
- # def is_majority_match_case(items) -> bool:
- #
- # total = 0
- #
- # for i in items:
- #
- # match i:
- # case True:
- # res = 1
- # case _:
- # res = -1
- #
- # total += res
- #
- # return total > 0
- # ----------- tests/test_majority.py ----------------
- import pytest
- from tasks.majority import (is_majority, is_majority_count, is_majority_len,
- is_majority_sum, is_majority_agg)
- @pytest.mark.parametrize("func", [
- is_majority,
- is_majority_count,
- is_majority_len,
- is_majority_sum,
- is_majority_agg
- ])
- @pytest.mark.parametrize(["data", "res"], [
- [[True, True, False, True, False], True],
- [[True, True, False], True],
- [[True, True, False, False], False],
- [[True, True, False, False, False], False],
- [[False], False],
- [[True], True],
- [[], False],
- ])
- def test_majority(func, data, res):
- assert func(data) == res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement