diff --git a/boolean_algebra/and_gate.py b/boolean_algebra/and_gate.py index 6ae66b5b0a77..650017b7ae10 100644 --- a/boolean_algebra/and_gate.py +++ b/boolean_algebra/and_gate.py @@ -1,8 +1,8 @@ """ -An AND Gate is a logic gate in boolean algebra which results to 1 (True) if both the -inputs are 1, and 0 (False) otherwise. +An AND Gate is a logic gate in boolean algebra which results to 1 (True) if all the +inputs are 1 (True), and 0 (False) otherwise. -Following is the truth table of an AND Gate: +Following is the truth table of a Two Input AND Gate: ------------------------------ | Input 1 | Input 2 | Output | ------------------------------ @@ -12,7 +12,7 @@ | 1 | 1 | 1 | ------------------------------ -Refer - https://www.geeksforgeeks.org/logic-gates-in-python/ +Refer - https://www.geeksforgeeks.org/logic-gates/ """ @@ -32,6 +32,18 @@ def and_gate(input_1: int, input_2: int) -> int: return int(input_1 and input_2) +def n_input_and_gate(inputs: list[int]) -> int: + """ + Calculate AND of a list of input values + + >>> n_input_and_gate([1, 0, 1, 1, 0]) + 0 + >>> n_input_and_gate([1, 1, 1, 1, 1]) + 1 + """ + return int(all(inputs)) + + if __name__ == "__main__": import doctest diff --git a/other/time_algo_exec.py b/other/time_algo_exec.py new file mode 100644 index 000000000000..39b251ef8e64 --- /dev/null +++ b/other/time_algo_exec.py @@ -0,0 +1,42 @@ +# Author : Bosolindo Edhiengene Roger +# email : rogerbosolinndo34@gmail.com + +# This module contains codes about algorithms complexity as to estimate the time +# an algorithm will take to be run. +# Why do we find it usable ? +# Because, knowing this kind of information tells you if your code or solution is +# efficient or not ; it helps you not to fall trying to run such a code. + + +def calc(operations: dict) -> float: + """ + calc(operation: dict) -> float: + This function aims to calculate how long an algorithm take, + knowing only primary operations + :param operations: + A dictionary where the values are tuples, consisting of the number of times + an operation is performed and its execution time, and the key should, + preferably, be the name of the operation for better clarity and usability. + :return: the time needed for the execution of this algorithm + >>> operations1 = {"addition":(2, 0.1), "subtraction":(1, 0.2)} + >>> operations2 = {"addition":(2, 0.1), "subtraction":(1, 0.2, 1)} + >>> calc(operations1) + 0.4 + >>> calc(operations2) + 0 + """ + temps = 0 + for couple in operations.values(): + # Case you give a shorter or a longer tuple + if len(couple) != 2: + return 0 + # Otherwise + temps += couple[0] * couple[1] + + return temps + + +if __name__ == "__main__": + import doctest + + doctest.testmod()
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: