Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
63
Task/ABC-Problem/Python/abc-problem-1.py
Normal file
63
Task/ABC-Problem/Python/abc-problem-1.py
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
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 can_make_word(word, block_collection=blocks):
|
||||
"""
|
||||
Return True if `word` can be made from the blocks in `block_collection`.
|
||||
|
||||
>>> can_make_word("")
|
||||
False
|
||||
>>> can_make_word("a")
|
||||
True
|
||||
>>> can_make_word("bark")
|
||||
True
|
||||
>>> can_make_word("book")
|
||||
False
|
||||
>>> can_make_word("treat")
|
||||
True
|
||||
>>> can_make_word("common")
|
||||
False
|
||||
>>> can_make_word("squad")
|
||||
True
|
||||
>>> can_make_word("coNFused")
|
||||
True
|
||||
"""
|
||||
if not word:
|
||||
return False
|
||||
|
||||
blocks_remaining = block_collection[:]
|
||||
for char in word.upper():
|
||||
for block in blocks_remaining:
|
||||
if char in block:
|
||||
blocks_remaining.remove(block)
|
||||
break
|
||||
else:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
import doctest
|
||||
doctest.testmod()
|
||||
print(", ".join("'%s': %s" % (w, can_make_word(w)) for w in
|
||||
["", "a", "baRk", "booK", "treat",
|
||||
"COMMON", "squad", "Confused"]))
|
||||
25
Task/ABC-Problem/Python/abc-problem-2.py
Normal file
25
Task/ABC-Problem/Python/abc-problem-2.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
BLOCKS = 'BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM'.split()
|
||||
|
||||
def _abc(word, blocks):
|
||||
for i, ch in enumerate(word):
|
||||
for blk in (b for b in blocks if ch in b):
|
||||
whatsleft = word[i + 1:]
|
||||
blksleft = blocks[:]
|
||||
blksleft.remove(blk)
|
||||
if not whatsleft:
|
||||
return True, blksleft
|
||||
if not blksleft:
|
||||
return False, blksleft
|
||||
ans, blksleft = _abc(whatsleft, blksleft)
|
||||
if ans:
|
||||
return ans, blksleft
|
||||
else:
|
||||
break
|
||||
return False, blocks
|
||||
|
||||
def abc(word, blocks=BLOCKS):
|
||||
return _abc(word.upper(), blocks)[0]
|
||||
|
||||
if __name__ == '__main__':
|
||||
for word in [''] + 'A BARK BoOK TrEAT COmMoN SQUAD conFUsE'.split():
|
||||
print('Can we spell %9r? %r' % (word, abc(word)))
|
||||
16
Task/ABC-Problem/Python/abc-problem-3.py
Normal file
16
Task/ABC-Problem/Python/abc-problem-3.py
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
def mkword(w, b):
|
||||
if not w: return []
|
||||
|
||||
c,w = w[0],w[1:]
|
||||
for i in range(len(b)):
|
||||
if c in b[i]:
|
||||
m = mkword(w, b[0:i] + b[i+1:])
|
||||
if m != None: return [b[i]] + m
|
||||
|
||||
def abc(w, blk):
|
||||
return mkword(w.upper(), [a.upper() for a in blk])
|
||||
|
||||
blocks = 'BO XK DQ CP NA GT RE TG QD FS JW HU VI AN OB ER FS LY PC ZM'.split()
|
||||
|
||||
for w in ", A, bark, book, treat, common, SQUAD, conFUsEd".split(', '):
|
||||
print '\'' + w + '\'' + ' ->', abc(w, blocks)
|
||||
Loading…
Add table
Add a link
Reference in a new issue