Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
46
Task/Set-puzzle/Python/set-puzzle-1.py
Normal file
46
Task/Set-puzzle/Python/set-puzzle-1.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#!/usr/bin/python
|
||||
|
||||
from itertools import product, combinations
|
||||
from random import sample
|
||||
|
||||
## Major constants
|
||||
features = [ 'green purple red'.split(),
|
||||
'one two three'.split(),
|
||||
'oval diamond squiggle'.split(),
|
||||
'open striped solid'.split() ]
|
||||
|
||||
deck = list(product(list(range(3)), repeat=4))
|
||||
|
||||
dealt = 9
|
||||
|
||||
## Functions
|
||||
def printcard(card):
|
||||
print(' '.join('%8s' % f[i] for f,i in zip(features, card)))
|
||||
|
||||
def getdeal(dealt=dealt):
|
||||
deal = sample(deck, dealt)
|
||||
return deal
|
||||
|
||||
def getsets(deal):
|
||||
good_feature_count = set([1, 3])
|
||||
sets = [ comb for comb in combinations(deal, 3)
|
||||
if all( [(len(set(feature)) in good_feature_count)
|
||||
for feature in zip(*comb)]
|
||||
) ]
|
||||
return sets
|
||||
|
||||
def printit(deal, sets):
|
||||
print('Dealt %i cards:' % len(deal))
|
||||
for card in deal: printcard(card)
|
||||
print('\nFound %i sets:' % len(sets))
|
||||
for s in sets:
|
||||
for card in s: printcard(card)
|
||||
print('')
|
||||
|
||||
if __name__ == '__main__':
|
||||
while True:
|
||||
deal = getdeal()
|
||||
sets = getsets(deal)
|
||||
if len(sets) == dealt / 2:
|
||||
break
|
||||
printit(deal, sets)
|
||||
21
Task/Set-puzzle/Python/set-puzzle-2.py
Normal file
21
Task/Set-puzzle/Python/set-puzzle-2.py
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import random, pprint
|
||||
from itertools import product, combinations
|
||||
|
||||
N_DRAW = 9
|
||||
N_GOAL = N_DRAW // 2
|
||||
|
||||
deck = list(product("red green purple".split(),
|
||||
"one two three".split(),
|
||||
"oval squiggle diamond".split(),
|
||||
"solid open striped".split()))
|
||||
|
||||
sets = []
|
||||
while len(sets) != N_GOAL:
|
||||
draw = random.sample(deck, N_DRAW)
|
||||
sets = [cs for cs in combinations(draw, 3)
|
||||
if all(len(set(t)) in [1, 3] for t in zip(*cs))]
|
||||
|
||||
print "Dealt %d cards:" % len(draw)
|
||||
pprint.pprint(draw)
|
||||
print "\nContaining %d sets:" % len(sets)
|
||||
pprint.pprint(sets)
|
||||
Loading…
Add table
Add a link
Reference in a new issue