From 040951fd5261c14437391f0479fa98235decd80a Mon Sep 17 00:00:00 2001 From: Yosef Sirak Date: Fri, 6 Oct 2023 06:14:27 -0700 Subject: [PATCH 1/4] added SR Latch module --- boolean_algebra/sr_latch.py | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 boolean_algebra/sr_latch.py diff --git a/boolean_algebra/sr_latch.py b/boolean_algebra/sr_latch.py new file mode 100644 index 000000000000..59ae1e61b72a --- /dev/null +++ b/boolean_algebra/sr_latch.py @@ -0,0 +1,46 @@ +""" +SR latch (this is a cross-coupled NOR implementation; for a cross-coupled NAND, just complement the inputs before applying them): +is a simple memory element that stores 1 bit of information +State table: + | Input 1(set pin) | Input 2(reset pin) | q (not q) | + | 0 | 0 | no change | + | 0 | 1 | 0 1 | + | 1 | 0 | 1 0 | + | 1 | 1 | undefined | +Note: get_current_state() return value of [q,!q] +""" + + +class SrLatch: + """ + Example: + >>> sr_latch = SrLatch(True) + >>> sr_latch.get_current_state() + [True, False] + >>> sr_latch.set_current_state(False,True) + >>> sr_latch.get_current_state() + [False, True] + >>> sr_latch.set_current_state(False,False) + >>> sr_latch.get_current_state() + [False, True] + >>> sr_latch.set_current_state(True,True) + Traceback (most recent call last): + ... + ValueError: undefined state. + """ + def __init__(self,initial_state:bool) -> None: + self.__initial_state = initial_state + def get_current_state(self)->list: + return [self.__initial_state,not self.__initial_state] + def set_current_state(self, set_pin:bool,reset_pin:bool) -> None: + if set_pin and reset_pin: + raise ValueError("undefined state.") + elif not set_pin and reset_pin: + self.__initial_state = False + elif set_pin and not reset_pin: + self.__initial_state = True + +if __name__ == "__main__": + from doctest import testmod + + testmod() From 742549f1b2c467e1c8929c26061ab7fb3088566d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 6 Oct 2023 14:10:10 +0000 Subject: [PATCH 2/4] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- boolean_algebra/sr_latch.py | 60 ++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 28 deletions(-) diff --git a/boolean_algebra/sr_latch.py b/boolean_algebra/sr_latch.py index 59ae1e61b72a..5d24474bcb69 100644 --- a/boolean_algebra/sr_latch.py +++ b/boolean_algebra/sr_latch.py @@ -12,34 +12,38 @@ class SrLatch: - """ - Example: - >>> sr_latch = SrLatch(True) - >>> sr_latch.get_current_state() - [True, False] - >>> sr_latch.set_current_state(False,True) - >>> sr_latch.get_current_state() - [False, True] - >>> sr_latch.set_current_state(False,False) - >>> sr_latch.get_current_state() - [False, True] - >>> sr_latch.set_current_state(True,True) - Traceback (most recent call last): - ... - ValueError: undefined state. - """ - def __init__(self,initial_state:bool) -> None: - self.__initial_state = initial_state - def get_current_state(self)->list: - return [self.__initial_state,not self.__initial_state] - def set_current_state(self, set_pin:bool,reset_pin:bool) -> None: - if set_pin and reset_pin: - raise ValueError("undefined state.") - elif not set_pin and reset_pin: - self.__initial_state = False - elif set_pin and not reset_pin: - self.__initial_state = True - + """ + Example: + >>> sr_latch = SrLatch(True) + >>> sr_latch.get_current_state() + [True, False] + >>> sr_latch.set_current_state(False,True) + >>> sr_latch.get_current_state() + [False, True] + >>> sr_latch.set_current_state(False,False) + >>> sr_latch.get_current_state() + [False, True] + >>> sr_latch.set_current_state(True,True) + Traceback (most recent call last): + ... + ValueError: undefined state. + """ + + def __init__(self, initial_state: bool) -> None: + self.__initial_state = initial_state + + def get_current_state(self) -> list: + return [self.__initial_state, not self.__initial_state] + + def set_current_state(self, set_pin: bool, reset_pin: bool) -> None: + if set_pin and reset_pin: + raise ValueError("undefined state.") + elif not set_pin and reset_pin: + self.__initial_state = False + elif set_pin and not reset_pin: + self.__initial_state = True + + if __name__ == "__main__": from doctest import testmod From a814e0c8b47c1ebc961a2978fda5dda9fed33094 Mon Sep 17 00:00:00 2001 From: Yosef Sirak <95920190+Yosef-6@users.noreply.github.com> Date: Fri, 6 Oct 2023 07:16:40 -0700 Subject: [PATCH 3/4] Fixed line to long --- boolean_algebra/sr_latch.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/boolean_algebra/sr_latch.py b/boolean_algebra/sr_latch.py index 5d24474bcb69..1801f8234bea 100644 --- a/boolean_algebra/sr_latch.py +++ b/boolean_algebra/sr_latch.py @@ -1,5 +1,6 @@ """ -SR latch (this is a cross-coupled NOR implementation; for a cross-coupled NAND, just complement the inputs before applying them): +SR latch (this is a cross-coupled NOR implementation; +for a cross-coupled NAND, just complement the inputs before applying them): is a simple memory element that stores 1 bit of information State table: | Input 1(set pin) | Input 2(reset pin) | q (not q) | From 9679c5bed81ef55c8781bf220c0ba0dc8edd8ce7 Mon Sep 17 00:00:00 2001 From: cclauss Date: Sun, 13 Sep 2026 05:47:52 +0000 Subject: [PATCH 4/4] updating DIRECTORY.md --- DIRECTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index 56ac6b94994c..7cd37ce4ecfd 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -117,6 +117,7 @@ * [Not Gate](boolean_algebra/not_gate.py) * [Or Gate](boolean_algebra/or_gate.py) * [Quine Mc Cluskey](boolean_algebra/quine_mc_cluskey.py) + * [Sr Latch](boolean_algebra/sr_latch.py) * [Xnor Gate](boolean_algebra/xnor_gate.py) * [Xor Gate](boolean_algebra/xor_gate.py) @@ -425,6 +426,7 @@ * [Climbing Stairs](dynamic_programming/climbing_stairs.py) * [Combination Sum Iv](dynamic_programming/combination_sum_iv.py) * [Edit Distance](dynamic_programming/edit_distance.py) + * [Egg Dropping](dynamic_programming/egg_dropping.py) * [Factorial](dynamic_programming/factorial.py) * [Fast Fibonacci](dynamic_programming/fast_fibonacci.py) * [Fibonacci](dynamic_programming/fibonacci.py) @@ -591,6 +593,7 @@ * [Graphs Floyd Warshall](graphs/graphs_floyd_warshall.py) * [Greedy Best First](graphs/greedy_best_first.py) * [Greedy Min Vertex Cover](graphs/greedy_min_vertex_cover.py) + * [Hopcroft Karp](graphs/hopcroft_karp.py) * [Johnson](graphs/johnson.py) * [Kahns Algorithm Long](graphs/kahns_algorithm_long.py) * [Kahns Algorithm Topo](graphs/kahns_algorithm_topo.py)