Added "Inverter" example
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
from snanosm.mealy import Machine
|
||||
|
||||
def add_to_result(context, c):
|
||||
context["result"] += c
|
||||
|
||||
def inverter(input_string: str) -> str:
|
||||
context = {
|
||||
"result": ""
|
||||
}
|
||||
m = Machine(context)
|
||||
m.add_state("S", True, False)
|
||||
m.add_transition("0", "S", "S", lambda ctx: add_to_result(ctx, "1"))
|
||||
m.add_transition("1", "S", "S", lambda ctx: add_to_result(ctx, "0"))
|
||||
for c in input_string:
|
||||
m.process_input(c)
|
||||
return context["result"]
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(inverter("000011110010"))
|
||||
Reference in New Issue
Block a user