Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
BrutalForce solution in Clear category for Halloween Monsters by PythonLearner
from collections import Counter
from copy import copy
from functools import partial
from itertools import permutations
from typing import Mapping, Iterable
MONSTERS = '''
skeleton
ghost
jack
vampire
witch
mummy
zombie
werewolf
frankenstein
'''
MONSTERS_LETTERS = list(map(Counter, MONSTERS.split()))
def is_monster_in_spell(spell_letters: Mapping[str, int], monster_letters: Mapping[str, int]) -> bool:
return all(spell_letters.get(letter, 0) >= count for letter, count in monster_letters.items())
def count_monsters(spell_letters: Mapping[str, int], monsters_letters: Iterable[Mapping[str, int]]) -> int:
letters = copy(spell_letters)
total_count = 0
for monster_letters in monsters_letters:
monster_count = min(letters[letter]//count for letter, count in monster_letters.items())
total_count += monster_count
for letter, count in monster_letters.items():
letters[letter] -= monster_count*count
return total_count
def halloween_monsters(spell: str) -> int:
spell_letters = Counter(spell)
monsters_letters = permutations(filter(partial(is_monster_in_spell, spell_letters), MONSTERS_LETTERS))
return max((count_monsters(spell_letters, monster_letters) for monster_letters in monsters_letters), default=0)
if __name__ == '__main__':
assert halloween_monsters('casjokthg') == 2, 'jack ghost'
assert halloween_monsters('leumooeeyzwwmmirbmf') == 3, 'mummy zombie werewolf'
assert halloween_monsters('nafrweiicttwneshhtikcn') == 3, 'witch witch frankenstein'
assert halloween_monsters('kenoistcepajmlvre') == 2, 'skeleton vampire (not jack)'
assert halloween_monsters('miaimavrurymepepv') == 2, 'vampire vampire (not mummy)'
print("Your spell seem to be okay. It's time to check.")
Oct. 18, 2019
Comments: