Skip to content

Commit

Permalink
Add the bottles problem
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyannn committed Jan 18, 2024
1 parent e8fae6a commit 525ec00
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions .github/bottles.py
Original file line number Diff line number Diff line change
@@ -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()))

0 comments on commit 525ec00

Please sign in to comment.