ABC problem: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: added an archaic form of spelling. -- ~~~~) |
|||
Line 131: | Line 131: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
<lang python> |
|||
blocks = [("B", "O"), |
|||
("X", "K"), |
|||
("D", "Q"), |
|||
("C", "P"), |
|||
("N", "A"), |
|||
("G", "T"), |
|||
("R", "E"), |
|||
("T", "G"), |
|||
("Q", "D"), |
|||
("F", "S"), |
|||
("J", "W"), |
|||
("H", "U"), |
|||
("V", "I"), |
|||
("A", "N"), |
|||
("O", "B"), |
|||
("E", "R"), |
|||
("F", "S"), |
|||
("L", "Y"), |
|||
("P", "C"), |
|||
("Z", "M")] |
|||
def make_word(word, block_collection=blocks): |
|||
""" |
|||
Return True if `word` can be made from the blocks in `block_collection`. |
|||
>>> make_word("") |
|||
False |
|||
>>> make_word("a") |
|||
True |
|||
>>> make_word("bark") |
|||
True |
|||
>>> make_word("book") |
|||
False |
|||
>>> make_word("treat") |
|||
True |
|||
>>> make_word("common") |
|||
False |
|||
>>> make_word("squad") |
|||
True |
|||
>>> make_word("coNFused") |
|||
True |
|||
""" |
|||
if len(word) == 0: |
|||
return False |
|||
blocks_remaining = block_collection[:] |
|||
found_letters = 0 |
|||
for char in word: |
|||
char = char.upper() |
|||
for block in blocks_remaining: |
|||
if char in block: |
|||
blocks_remaining.remove(block) |
|||
found_letters += 1 |
|||
if found_letters == len(word): |
|||
return True |
|||
else: |
|||
break |
|||
return False |
|||
if __name__ == "__main__": |
|||
import doctest |
|||
doctest.testmod() |
|||
</lang> |
|||
<lang python>BLOCKS = 'BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM'.split() |
<lang python>BLOCKS = 'BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM'.split() |
||