Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
51 changes: 51 additions & 0 deletions boolean_algebra/sr_latch.py
Original file line number Diff line number Diff line change
@@ -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()