From 525ec004b995f8d61822f1aaf52ab5a084208a53 Mon Sep 17 00:00:00 2001 From: Ilya Nikokoshev Date: Thu, 18 Jan 2024 16:59:11 +0100 Subject: [PATCH] Add the bottles problem --- .github/bottles.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 .github/bottles.py diff --git a/.github/bottles.py b/.github/bottles.py new file mode 100644 index 0000000..0d996df --- /dev/null +++ b/.github/bottles.py @@ -0,0 +1,39 @@ +import unittest + +from collections import Counter +from functools import cache + + +@cache +def howmany(bottles: tuple): + if len(bottles) == 1: + return bottles[0] + 1 + + def sub(ix): + bottles1 = sorted((jb - (jx == ix) for jx, jb in enumerate(bottles))) + return tuple(b for b in bottles1 if b) + + return sum(howmany(sub(i)) for i in range(len(bottles))) + 1 + + +def main(letters): + c = sorted(Counter(letters).values()) + return howmany(tuple(c)) - 1 + + +class TestHowMany(unittest.TestCase): + def test_howmany(self): + self.assertEqual(howmany((5,)), 6) + self.assertEqual(howmany((1, 1)), 5) + self.assertEqual(howmany((1, 1, 1)), 16) + + +class TestMain(unittest.TestCase): + def test_main(self): + self.assertEqual(main("ab"), 4) + self.assertEqual(main("abc"), 15) + self.assertEqual(main("aaaaa"), 5) + + +if __name__ == "__main__": + print(main(input().strip()))