Skip to content

Updated doctests for nor_gate #10791

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

Merged
merged 5 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@
* [Dimensionality Reduction](machine_learning/dimensionality_reduction.py)
* Forecasting
* [Run](machine_learning/forecasting/run.py)
* [Frequent Pattern Growth Algorithm](machine_learning/frequent_pattern_growth.py)
* [Frequent Pattern Growth](machine_learning/frequent_pattern_growth.py)
* [Gradient Descent](machine_learning/gradient_descent.py)
* [K Means Clust](machine_learning/k_means_clust.py)
* [K Nearest Neighbours](machine_learning/k_nearest_neighbours.py)
Expand Down Expand Up @@ -649,6 +649,7 @@
* [Numerical Integration](maths/numerical_integration.py)
* [Odd Sieve](maths/odd_sieve.py)
* [Perfect Cube](maths/perfect_cube.py)
* [Perfect Number](maths/perfect_number.py)
* [Perfect Square](maths/perfect_square.py)
* [Persistence](maths/persistence.py)
* [Pi Generator](maths/pi_generator.py)
Expand Down Expand Up @@ -767,7 +768,6 @@
* [Swish](neural_network/activation_functions/swish.py)
* [Back Propagation Neural Network](neural_network/back_propagation_neural_network.py)
* [Convolution Neural Network](neural_network/convolution_neural_network.py)
* [Perceptron](neural_network/perceptron.py)
* [Simple Neural Network](neural_network/simple_neural_network.py)

## Other
Expand Down Expand Up @@ -803,8 +803,10 @@
* [Archimedes Principle Of Buoyant Force](physics/archimedes_principle_of_buoyant_force.py)
* [Basic Orbital Capture](physics/basic_orbital_capture.py)
* [Casimir Effect](physics/casimir_effect.py)
* [Center Of Mass](physics/center_of_mass.py)
* [Centripetal Force](physics/centripetal_force.py)
* [Coulombs Law](physics/coulombs_law.py)
* [Doppler Frequency](physics/doppler_frequency.py)
* [Grahams Law](physics/grahams_law.py)
* [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py)
* [Hubble Parameter](physics/hubble_parameter.py)
Expand Down
55 changes: 37 additions & 18 deletions boolean_algebra/nor_gate.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
"""
A NOR Gate is a logic gate in boolean algebra which results to false(0)
if any of the input is 1, and True(1) if both the inputs are 0.
A NOR Gate is a logic gate in boolean algebra which results in false(0) if any of the
inputs is 1, and True(1) if all inputs are 0.
Following is the truth table of a NOR Gate:
| Input 1 | Input 2 | Output |
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 0 |
Truth Table of NOR Gate:
| Input 1 | Input 2 | Output |
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 0 |

Following is the code implementation of the NOR Gate
Code provided by Akshaj Vishwanathan
https://www.geeksforgeeks.org/logic-gates-in-python
"""
from collections.abc import Callable


def nor_gate(input_1: int, input_2: int) -> int:
Expand All @@ -30,19 +33,35 @@ def nor_gate(input_1: int, input_2: int) -> int:
return int(input_1 == input_2 == 0)


def main() -> None:
print("Truth Table of NOR Gate:")
print("| Input 1 | Input 2 | Output |")
print(f"| 0 | 0 | {nor_gate(0, 0)} |")
print(f"| 0 | 1 | {nor_gate(0, 1)} |")
print(f"| 1 | 0 | {nor_gate(1, 0)} |")
print(f"| 1 | 1 | {nor_gate(1, 1)} |")
def truth_table(func: Callable) -> str:
"""
>>> print(truth_table(nor_gate))
Truth Table of NOR Gate:
| Input 1 | Input 2 | Output |
| 0 | 0 | 1 |
| 0 | 1 | 0 |
| 1 | 0 | 0 |
| 1 | 1 | 0 |
"""

def make_table_row(items: list | tuple) -> str:
"""
>>> make_table_row(("One", "Two", "Three"))
'| One | Two | Three |'
"""
return f"| {' | '.join(f'{item:^8}' for item in items)} |"

return "\n".join(
(
"Truth Table of NOR Gate:",
make_table_row(("Input 1", "Input 2", "Output")),
*[make_table_row((i, j, func(i, j))) for i in (0, 1) for j in (0, 1)],
)
)


if __name__ == "__main__":
import doctest

doctest.testmod()
main()
"""Code provided by Akshaj Vishwanathan"""
"""Reference: https://www.geeksforgeeks.org/logic-gates-in-python/"""
print(truth_table(nor_gate))