This will work for iterable of any size (not only 3).
We are returning right when the condition of having > 1 True's is satisfied.
No need to iterate to the end of the sequence.
def solution(param: list) -> bool: """Returns True if atleast 2 out of 3 (or more) booleans are True.""" true_count = 0 for p in param: true_count += p is True if true_count > 1: return True return False
- def solution(param: list) -> bool:
"""Returns True if atleast 2 out of 3 booleans are True."""return sum([1 for p in param if p is True]) >= 2- """Returns True if atleast 2 out of 3 (or more) booleans are True."""
- true_count = 0
- for p in param:
- true_count += p is True
- if true_count > 1:
- return True
- return False
from solution import solution import unittest class TestSolution(unittest.TestCase): """Test Solution""" def setUp(self) -> None: self.sample_cases = [((True, True, True), True), ((True, False, True), True), (('True', True, 'True'), False), ((True, False, False, False, True), True), ((None, None, None, False, True), False), ((1, True, False), False), ((True, True), True), ((False, 'False', False), False), (('False', False, False, 'True', True), False), ((False, False, 2, 'True', True), False), ((False, '1', True, False, None, 1, True), True), ((1, 2, 3, 4, True), False), ((True, 1, True), True), ((True, False), False), ] def test_solution(self): for sample, expected in self.sample_cases: self.assertEqual(solution(sample), expected) if __name__ == '__main__': unittest.main()
- from solution import solution
- import unittest
- class TestSolution(unittest.TestCase):
- """Test Solution"""
- def setUp(self) -> None:
- self.sample_cases = [((True, True, True), True),
- ((True, False, True), True),
- (('True', True, 'True'), False),
- ((True, False, False, False, True), True),
- ((None, None, None, False, True), False),
- ((1, True, False), False),
- ((True, True), True),
- ((False, 'False', False), False),
- (('False', False, False, 'True', True), False),
- ((False, False, 2, 'True', True), False),
- ((False, '1', True, False, None, 1, True), True),
- ((1, 2, 3, 4, True), False),
- ((True, 1, True), True),
- ((True, False), False),
- ]
- def test_solution(self):
- for sample, expected in self.sample_cases:
- self.assertEqual(solution(sample), expected)
- if __name__ == '__main__':
- unittest.main()