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) diff --git a/boolean_algebra/sr_latch.py b/boolean_algebra/sr_latch.py new file mode 100644 index 000000000000..1801f8234bea --- /dev/null +++ b/boolean_algebra/sr_latch.py @@ -0,0 +1,51 @@ +""" +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()