Added "Inverter" example

This commit is contained in:
David Gil de Gómez Pérez
2026-07-15 17:40:33 +03:00
parent 634b783790
commit a211503cae
+19
View File
@@ -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"))