Updated doctests for nor_gate (#10791)

* added other possible cases

* added test for correct output of truth table

* updating DIRECTORY.md

* Update nor_gate.py

---------

Co-authored-by: = <=>
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
Kento 2023-10-22 20:08:08 +11:00 committed by GitHub
parent 0601b56173
commit 7d0f6e012a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 41 additions and 20 deletions

View File

@ -541,7 +541,7 @@
* [Dimensionality Reduction](machine_learning/dimensionality_reduction.py) * [Dimensionality Reduction](machine_learning/dimensionality_reduction.py)
* Forecasting * Forecasting
* [Run](machine_learning/forecasting/run.py) * [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) * [Gradient Descent](machine_learning/gradient_descent.py)
* [K Means Clust](machine_learning/k_means_clust.py) * [K Means Clust](machine_learning/k_means_clust.py)
* [K Nearest Neighbours](machine_learning/k_nearest_neighbours.py) * [K Nearest Neighbours](machine_learning/k_nearest_neighbours.py)
@ -649,6 +649,7 @@
* [Numerical Integration](maths/numerical_integration.py) * [Numerical Integration](maths/numerical_integration.py)
* [Odd Sieve](maths/odd_sieve.py) * [Odd Sieve](maths/odd_sieve.py)
* [Perfect Cube](maths/perfect_cube.py) * [Perfect Cube](maths/perfect_cube.py)
* [Perfect Number](maths/perfect_number.py)
* [Perfect Square](maths/perfect_square.py) * [Perfect Square](maths/perfect_square.py)
* [Persistence](maths/persistence.py) * [Persistence](maths/persistence.py)
* [Pi Generator](maths/pi_generator.py) * [Pi Generator](maths/pi_generator.py)
@ -767,7 +768,6 @@
* [Swish](neural_network/activation_functions/swish.py) * [Swish](neural_network/activation_functions/swish.py)
* [Back Propagation Neural Network](neural_network/back_propagation_neural_network.py) * [Back Propagation Neural Network](neural_network/back_propagation_neural_network.py)
* [Convolution Neural Network](neural_network/convolution_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) * [Simple Neural Network](neural_network/simple_neural_network.py)
## Other ## Other
@ -803,8 +803,10 @@
* [Archimedes Principle Of Buoyant Force](physics/archimedes_principle_of_buoyant_force.py) * [Archimedes Principle Of Buoyant Force](physics/archimedes_principle_of_buoyant_force.py)
* [Basic Orbital Capture](physics/basic_orbital_capture.py) * [Basic Orbital Capture](physics/basic_orbital_capture.py)
* [Casimir Effect](physics/casimir_effect.py) * [Casimir Effect](physics/casimir_effect.py)
* [Center Of Mass](physics/center_of_mass.py)
* [Centripetal Force](physics/centripetal_force.py) * [Centripetal Force](physics/centripetal_force.py)
* [Coulombs Law](physics/coulombs_law.py) * [Coulombs Law](physics/coulombs_law.py)
* [Doppler Frequency](physics/doppler_frequency.py)
* [Grahams Law](physics/grahams_law.py) * [Grahams Law](physics/grahams_law.py)
* [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py) * [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py)
* [Hubble Parameter](physics/hubble_parameter.py) * [Hubble Parameter](physics/hubble_parameter.py)

View File

@ -1,15 +1,18 @@
""" """
A NOR Gate is a logic gate in boolean algebra which results to false(0) A NOR Gate is a logic gate in boolean algebra which results in false(0) if any of the
if any of the input is 1, and True(1) if both the inputs are 0. inputs is 1, and True(1) if all inputs are 0.
Following is the truth table of a NOR Gate: Following is the truth table of a NOR Gate:
| Input 1 | Input 2 | Output | Truth Table of NOR Gate:
| 0 | 0 | 1 | | Input 1 | Input 2 | Output |
| 0 | 1 | 0 | | 0 | 0 | 1 |
| 1 | 0 | 0 | | 0 | 1 | 0 |
| 1 | 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: def nor_gate(input_1: int, input_2: int) -> int:
@ -30,19 +33,35 @@ def nor_gate(input_1: int, input_2: int) -> int:
return int(input_1 == input_2 == 0) return int(input_1 == input_2 == 0)
def main() -> None: def truth_table(func: Callable) -> str:
print("Truth Table of NOR Gate:") """
print("| Input 1 | Input 2 | Output |") >>> print(truth_table(nor_gate))
print(f"| 0 | 0 | {nor_gate(0, 0)} |") Truth Table of NOR Gate:
print(f"| 0 | 1 | {nor_gate(0, 1)} |") | Input 1 | Input 2 | Output |
print(f"| 1 | 0 | {nor_gate(1, 0)} |") | 0 | 0 | 1 |
print(f"| 1 | 1 | {nor_gate(1, 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__": if __name__ == "__main__":
import doctest import doctest
doctest.testmod() doctest.testmod()
main() print(truth_table(nor_gate))
"""Code provided by Akshaj Vishwanathan"""
"""Reference: https://www.geeksforgeeks.org/logic-gates-in-python/"""