From 3ceb9fbb57cc423d350e81d831e9d0682a818a27 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sat, 21 Oct 2023 19:22:11 +1100 Subject: [PATCH 1/8] 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/8] 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 ac233f4397f52da10ead59e4ddb058a3e80bcab5 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sun, 22 Oct 2023 13:51:58 +1100 Subject: [PATCH 3/8] few fibonacci tests added --- maths/fibonacci.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/maths/fibonacci.py b/maths/fibonacci.py index e810add69dc7..f4b197cea7f7 100644 --- a/maths/fibonacci.py +++ b/maths/fibonacci.py @@ -60,7 +60,6 @@ def fib_iterative(n: int) -> list[int]: fib.append(fib[-1] + fib[-2]) return fib - def fib_recursive(n: int) -> list[int]: """ Calculates the first n (0-indexed) Fibonacci numbers using recursion @@ -81,6 +80,18 @@ def fib_recursive(n: int) -> list[int]: def fib_recursive_term(i: int) -> int: """ Calculates the i-th (0-indexed) Fibonacci number using recursion + >>> fib_recursive_term(0) + 0 + >>> fib_recursive_term(1) + 1 + >>> fib_recursive_term(5) + 5 + >>> fib_recursive_term(10) + 55 + >>> fib_recursive_term(-1) + Traceback (most recent call last): + ... + Exception: n is negative """ if i < 0: raise Exception("n is negative") @@ -197,6 +208,10 @@ def fib_binet(n: int) -> list[int]: if __name__ == "__main__": + + import doctest + doctest.testmod() + num = 30 time_func(fib_iterative, num) time_func(fib_recursive, num) # Around 3s runtime 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 4/8] 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 5/8] 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)) From 3b3148ef2e331469a43dd37c8ce240f6a16a7498 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Mon, 23 Oct 2023 10:08:42 +0000 Subject: [PATCH 6/8] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 1e3711fe8dda..61823a241c8d 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -826,6 +826,7 @@ * [Shear Stress](physics/shear_stress.py) * [Speed Of Sound](physics/speed_of_sound.py) * [Speeds Of Gas Molecules](physics/speeds_of_gas_molecules.py) + * [Terminal Velocity](physics/terminal_velocity.py) ## Project Euler * Problem 001 From 3fe0359a3d4445cd311ec4744bda1e9b7c6d3e86 Mon Sep 17 00:00:00 2001 From: Kento <75509362+nkstonks@users.noreply.github.com> Date: Mon, 23 Oct 2023 21:10:36 +1100 Subject: [PATCH 7/8] Update fibonacci.py removed whitespace --- maths/fibonacci.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/fibonacci.py b/maths/fibonacci.py index f4b197cea7f7..0693fdda3291 100644 --- a/maths/fibonacci.py +++ b/maths/fibonacci.py @@ -211,7 +211,7 @@ def fib_binet(n: int) -> list[int]: import doctest doctest.testmod() - + num = 30 time_func(fib_iterative, num) time_func(fib_recursive, num) # Around 3s runtime From 5ceacc6a78c1df9c34036e3389155b399302c58e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 23 Oct 2023 10:13:42 +0000 Subject: [PATCH 8/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/fibonacci.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maths/fibonacci.py b/maths/fibonacci.py index 0693fdda3291..8cdd6cdb160e 100644 --- a/maths/fibonacci.py +++ b/maths/fibonacci.py @@ -60,6 +60,7 @@ def fib_iterative(n: int) -> list[int]: fib.append(fib[-1] + fib[-2]) return fib + def fib_recursive(n: int) -> list[int]: """ Calculates the first n (0-indexed) Fibonacci numbers using recursion @@ -208,8 +209,8 @@ def fib_binet(n: int) -> list[int]: if __name__ == "__main__": - import doctest + doctest.testmod() num = 30