From 0f35fa3701cf100ea867f4c41462e7059c08c938 Mon Sep 17 00:00:00 2001 From: Joscha Date: Fri, 24 Oct 2025 02:01:40 +0200 Subject: [PATCH] Print copy-paste-ready parentheses for balancing Though capped at 80 parentheses per type, if you don't supply --all. --- count_parentheses.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/count_parentheses.py b/count_parentheses.py index 7dc6524..54a181e 100644 --- a/count_parentheses.py +++ b/count_parentheses.py @@ -7,9 +7,25 @@ PARENS = list("()[]{}<>") SMILEYS = [":D", "D:"] -def main(): +def print_a_bunch(what: str, how_much: int, all: bool) -> None: + while how_much > 0: + print(what * min(80, how_much)) + if not all: + break + how_much -= 80 + + +def balance(count: Counter[str], a: str, b: str, all: bool) -> None: + if count[a] > count[b]: + print_a_bunch(b, count[a] - count[b], all) + if count[b] > count[a]: + print_a_bunch(a, count[b] - count[a], all) + + +def main() -> None: parser = ArgumentParser() parser.add_argument("path", type=Path) + parser.add_argument("--all", "-a", action="store_true") args = parser.parse_args() count = Counter[str]() @@ -26,6 +42,11 @@ def main(): for smiley in SMILEYS: print(f"{smiley} - {count[smiley]:6}") + balance(count, "(", ")", args.all) + balance(count, "[", "]", args.all) + balance(count, "{", "}", args.all) + balance(count, "<", ">", args.all) + if __name__ == "__main__": main()