From 3ceb9fbb57cc423d350e81d831e9d0682a818a27 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sat, 21 Oct 2023 19:22:11 +1100 Subject: [PATCH 1/4] added other possible cases --- boolean_algebra/nor_gate.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/boolean_algebra/nor_gate.py b/boolean_algebra/nor_gate.py index 2c27b80afdbe..81b4f9153cdf 100644 --- a/boolean_algebra/nor_gate.py +++ b/boolean_algebra/nor_gate.py @@ -26,6 +26,15 @@ def nor_gate(input_1: int, input_2: int) -> int: 1 >>> nor_gate(0, -7) 0 + >>> nor_gate(0, 2) + 0 + >>> nor_gate(-3, 0) + 0 + >>> nor_gate(0.0, 1.0) + 0 + >>> nor_gate(1.5, 0) + 0 + >>> nor_gate(0, -2.5) """ return int(input_1 == input_2 == 0) From 1441533ccb2ab292418001cb3d46bca64c4e4a52 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sat, 21 Oct 2023 19:33:50 +1100 Subject: [PATCH 2/4] added test for correct output of truth table --- boolean_algebra/nor_gate.py | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/boolean_algebra/nor_gate.py b/boolean_algebra/nor_gate.py index 81b4f9153cdf..e1e500b745eb 100644 --- a/boolean_algebra/nor_gate.py +++ b/boolean_algebra/nor_gate.py @@ -26,15 +26,13 @@ def nor_gate(input_1: int, input_2: int) -> int: 1 >>> nor_gate(0, -7) 0 - >>> nor_gate(0, 2) - 0 - >>> nor_gate(-3, 0) - 0 - >>> nor_gate(0.0, 1.0) - 0 - >>> nor_gate(1.5, 0) - 0 - >>> nor_gate(0, -2.5) + >>> main() + Truth Table of NOR Gate: + | Input 1 | Input 2 | Output | + | 0 | 0 | 1 | + | 0 | 1 | 0 | + | 1 | 0 | 0 | + | 1 | 1 | 0 | """ return int(input_1 == input_2 == 0) From d87a2ccb630d2a95eb219f33d8e7a9bd66b5e7c4 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 22 Oct 2023 02:53:55 +0000 Subject: [PATCH 3/4] updating DIRECTORY.md --- DIRECTORY.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 9e0166ad80c5..c37c4f99ba88 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -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) @@ -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) @@ -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 @@ -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) From e83ebb61000358f278089cbe5a7e7b751efb986e Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 22 Oct 2023 11:04:02 +0200 Subject: [PATCH 4/4] Update nor_gate.py --- boolean_algebra/nor_gate.py | 62 ++++++++++++++++++++++--------------- 1 file changed, 37 insertions(+), 25 deletions(-) diff --git a/boolean_algebra/nor_gate.py b/boolean_algebra/nor_gate.py index e1e500b745eb..0c8ab1c0af61 100644 --- a/boolean_algebra/nor_gate.py +++ b/boolean_algebra/nor_gate.py @@ -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: @@ -26,30 +29,39 @@ def nor_gate(input_1: int, input_2: int) -> int: 1 >>> nor_gate(0, -7) 0 - >>> main() - Truth Table of NOR Gate: - | Input 1 | Input 2 | Output | - | 0 | 0 | 1 | - | 0 | 1 | 0 | - | 1 | 0 | 0 | - | 1 | 1 | 0 | """ 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))