from itertools import permutations, combinations def amountOfBooksWhere(fun): return len([bookOrder for bookOrder in permutations('jjjjccc') if fun(''.join(bookOrder))]) # a) def no_restriction(books): return True # b) def alternating_books(books): return all(books.find(l+l) == -1 for l in set(books)) # c) def all_cs_are_together(books): return not books.find('ccc') == -1 # d) def all_cs_js_are_together(books): return not (books.find('ccc') == -1 or books.find('jjjj') == -1) print(amountOfBooksWhere(no_restriction)) print(amountOfBooksWhere(alternating_books)) print(amountOfBooksWhere(all_cs_are_together)) print(amountOfBooksWhere(all_cs_js_are_together))