Skip to content

Commit 7d0f6e0

Browse files
nkstonksgithub-actionscclauss
authored
Updated doctests for nor_gate (TheAlgorithms#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 <[email protected]>
1 parent 0601b56 commit 7d0f6e0

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

DIRECTORY.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@
541541
* [Dimensionality Reduction](machine_learning/dimensionality_reduction.py)
542542
* Forecasting
543543
* [Run](machine_learning/forecasting/run.py)
544-
* [Frequent Pattern Growth Algorithm](machine_learning/frequent_pattern_growth.py)
544+
* [Frequent Pattern Growth](machine_learning/frequent_pattern_growth.py)
545545
* [Gradient Descent](machine_learning/gradient_descent.py)
546546
* [K Means Clust](machine_learning/k_means_clust.py)
547547
* [K Nearest Neighbours](machine_learning/k_nearest_neighbours.py)
@@ -649,6 +649,7 @@
649649
* [Numerical Integration](maths/numerical_integration.py)
650650
* [Odd Sieve](maths/odd_sieve.py)
651651
* [Perfect Cube](maths/perfect_cube.py)
652+
* [Perfect Number](maths/perfect_number.py)
652653
* [Perfect Square](maths/perfect_square.py)
653654
* [Persistence](maths/persistence.py)
654655
* [Pi Generator](maths/pi_generator.py)
@@ -767,7 +768,6 @@
767768
* [Swish](neural_network/activation_functions/swish.py)
768769
* [Back Propagation Neural Network](neural_network/back_propagation_neural_network.py)
769770
* [Convolution Neural Network](neural_network/convolution_neural_network.py)
770-
* [Perceptron](neural_network/perceptron.py)
771771
* [Simple Neural Network](neural_network/simple_neural_network.py)
772772

773773
## Other
@@ -803,8 +803,10 @@
803803
* [Archimedes Principle Of Buoyant Force](physics/archimedes_principle_of_buoyant_force.py)
804804
* [Basic Orbital Capture](physics/basic_orbital_capture.py)
805805
* [Casimir Effect](physics/casimir_effect.py)
806+
* [Center Of Mass](physics/center_of_mass.py)
806807
* [Centripetal Force](physics/centripetal_force.py)
807808
* [Coulombs Law](physics/coulombs_law.py)
809+
* [Doppler Frequency](physics/doppler_frequency.py)
808810
* [Grahams Law](physics/grahams_law.py)
809811
* [Horizontal Projectile Motion](physics/horizontal_projectile_motion.py)
810812
* [Hubble Parameter](physics/hubble_parameter.py)

boolean_algebra/nor_gate.py

+37-18
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
"""
2-
A NOR Gate is a logic gate in boolean algebra which results to false(0)
3-
if any of the input is 1, and True(1) if both the inputs are 0.
2+
A NOR Gate is a logic gate in boolean algebra which results in false(0) if any of the
3+
inputs is 1, and True(1) if all inputs are 0.
44
Following is the truth table of a NOR Gate:
5-
| Input 1 | Input 2 | Output |
6-
| 0 | 0 | 1 |
7-
| 0 | 1 | 0 |
8-
| 1 | 0 | 0 |
9-
| 1 | 1 | 0 |
5+
Truth Table of NOR Gate:
6+
| Input 1 | Input 2 | Output |
7+
| 0 | 0 | 1 |
8+
| 0 | 1 | 0 |
9+
| 1 | 0 | 0 |
10+
| 1 | 1 | 0 |
1011
11-
Following is the code implementation of the NOR Gate
12+
Code provided by Akshaj Vishwanathan
13+
https://www.geeksforgeeks.org/logic-gates-in-python
1214
"""
15+
from collections.abc import Callable
1316

1417

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

3235

33-
def main() -> None:
34-
print("Truth Table of NOR Gate:")
35-
print("| Input 1 | Input 2 | Output |")
36-
print(f"| 0 | 0 | {nor_gate(0, 0)} |")
37-
print(f"| 0 | 1 | {nor_gate(0, 1)} |")
38-
print(f"| 1 | 0 | {nor_gate(1, 0)} |")
39-
print(f"| 1 | 1 | {nor_gate(1, 1)} |")
36+
def truth_table(func: Callable) -> str:
37+
"""
38+
>>> print(truth_table(nor_gate))
39+
Truth Table of NOR Gate:
40+
| Input 1 | Input 2 | Output |
41+
| 0 | 0 | 1 |
42+
| 0 | 1 | 0 |
43+
| 1 | 0 | 0 |
44+
| 1 | 1 | 0 |
45+
"""
46+
47+
def make_table_row(items: list | tuple) -> str:
48+
"""
49+
>>> make_table_row(("One", "Two", "Three"))
50+
'| One | Two | Three |'
51+
"""
52+
return f"| {' | '.join(f'{item:^8}' for item in items)} |"
53+
54+
return "\n".join(
55+
(
56+
"Truth Table of NOR Gate:",
57+
make_table_row(("Input 1", "Input 2", "Output")),
58+
*[make_table_row((i, j, func(i, j))) for i in (0, 1) for j in (0, 1)],
59+
)
60+
)
4061

4162

4263
if __name__ == "__main__":
4364
import doctest
4465

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

0 commit comments

Comments
 (0)