Skip to content

Commit ab1d258

Browse files
committed
refactor: enhance input validation for n_input_or_gate to handle empty lists and values are not 0 and 1
1 parent cfea4c3 commit ab1d258

1 file changed

Lines changed: 18 additions & 1 deletion

File tree

boolean_algebra/or_gate.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ def or_gate(input_1: int, input_2: int) -> int:
3131

3232
def n_input_or_gate(inputs: list[int]) -> int:
3333
"""
34-
Calculate OR of a list of input values
34+
Generalization of or_gate() to support n inputs.
35+
Calculate OR of a list of input values.
36+
Returns 1 if any input is 1, 0 otherwise.
37+
3538
>>> n_input_or_gate([0, 0, 0, 0, 0])
3639
0
3740
>>> n_input_or_gate([0, 1, 0, 0, 0])
@@ -42,13 +45,27 @@ def n_input_or_gate(inputs: list[int]) -> int:
4245
>>> n_input_or_gate([0, 1])
4346
1
4447
48+
>>> n_input_or_gate([])
49+
Traceback (most recent call last):
50+
...
51+
ValueError: Input list cannot be empty
52+
4553
>>> n_input_or_gate([1])
4654
Traceback (most recent call last):
4755
...
4856
ValueError: Input list must contain at least two elements
57+
58+
>>> n_input_or_gate([2, 1])
59+
Traceback (most recent call last):
60+
...
61+
ValueError: All inputs must be 0 or 1
4962
"""
63+
if len(inputs) == 0:
64+
raise ValueError("Input list cannot be empty")
5065
if len(inputs) < 2:
5166
raise ValueError("Input list must contain at least two elements")
67+
if not all(i in (0, 1) for i in inputs):
68+
raise ValueError("All inputs must be 0 or 1")
5269

5370
return int(any(inputs))
5471

0 commit comments

Comments
 (0)