From 557e03c80da8e24b3c46de1e456d9dbd7048454a Mon Sep 17 00:00:00 2001 From: Ivan Malison Date: Sun, 30 Aug 2015 13:22:44 -0700 Subject: [PATCH] parenthesizations_as_ints.py --- .../lib/python/parenthesizations_as_ints.py | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 dotfiles/lib/python/parenthesizations_as_ints.py diff --git a/dotfiles/lib/python/parenthesizations_as_ints.py b/dotfiles/lib/python/parenthesizations_as_ints.py new file mode 100644 index 00000000..ac81dc5c --- /dev/null +++ b/dotfiles/lib/python/parenthesizations_as_ints.py @@ -0,0 +1,31 @@ +from backports.functools_lru_cache import lru_cache + + +def parenthesizations2h(pair_count): + return [ + '{0:b}'.format(item).replace('1', '(').replace('0', ')') + for item in parenthesizations.parenthesizations(pair_count) + ] + +def parenthesizations2(pair_count): + """Parenthesizations returned encoded as numbers + """ + parenthesizations = _parenthesizations2(pair_count * 2, net=0) + return parenthesizations + + +@lru_cache(maxsize=None) +def _parenthesizations2(length, net=0): + if net > length or net < 0: + raise Exception() + if net == length: + return [0] + res = add_bit(length-1, _parenthesizations(length-1, net=net + 1)) + if net > 0: + res.extend(_parenthesizations(length-1, net=net - 1)) + return res + + +def add_bit(bitindex, items): + value = 2 ** bitindex + return [value + item for item in items]