From 79833959bcadae4158bce9b850dd9aee59716473 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Gil=20de=20G=C3=B3mez=20P=C3=A9rez?= Date: Thu, 16 Jul 2026 10:45:02 +0300 Subject: [PATCH] Feat: Better typing in inverter.py --- examples/inverter.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/examples/inverter.py b/examples/inverter.py index 8f11f4b..a84002f 100644 --- a/examples/inverter.py +++ b/examples/inverter.py @@ -1,11 +1,19 @@ +from typing import Tuple, TypedDict + from snanosm.mealy import Machine +class Context(TypedDict): + result: str + n_chars: int + def add_to_result(context, c): context["result"] += c + context["n_chars"] += 1 -def inverter(input_string: str) -> str: - context = { - "result": "" +def inverter(input_string: str) -> Tuple[str, int]: + context: Context = { + "result": "", + "n_chars": 0 } m = Machine(context) m.add_state("S", True, False) @@ -13,7 +21,7 @@ def inverter(input_string: str) -> str: 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"] + return context["result"], context["n_chars"] if __name__ == '__main__': print(inverter("000011110010")) \ No newline at end of file