Skip to content

feat: Add recursive implication function for lists #12855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
26 changes: 26 additions & 0 deletions boolean_algebra/imply_gate.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,32 @@ def imply_gate(input_1: int, input_2: int) -> int:
return int(input_1 == 0 or input_2 == 1)


def recursive_imply_list(input_list: list[int]) -> int:
"""
Recursively calculates the implication of a list.
Strictly The Implication is Applied Consecuteivly left to right:
( (a -> b) -> c ) -> d ...

>>> recursive_imply_list([])
1
>>> recursive_imply_list([0])
0
>>> recursive_imply_list([1])
1
>>> recursive_imply_list([1, 0, 1])
1
>>> recursive_imply_list([1, 1, 0])
0
"""
if not input_list:
return 1
if len(input_list) == 1:
return input_list[0]
first_implication = imply_gate(input_list[0], input_list[1])
new_list = [first_implication, *input_list[2:]]
return recursive_imply_list(new_list)


if __name__ == "__main__":
import doctest

Expand Down