+/foobar
google programming puzzles
git clone https://git.julia.blue/+/foobar
6422597
master
1from itertools import combinations 2 3def verify (los ,num ): 4target = max (map (max ,los ))+ 1 5for subset in combinations (los ,num ): 6if len (reduce (lambda x ,y :x | y ,subset ))!= target : 7return False 8for subset in combinations (los ,num - 1 ): 9if len (reduce (lambda x ,y :x | y ,subset ))== target : 10return False 11return True 12 13 14def num2bin (s ,size ): 15result = [0 ]* size 16for n in s : 17result [n ]= 1 18return result 19 20 21def bin2num (s ): 22result = set ([]) 23for i in range (len (s )): 24if s [i ]: 25result .add (i ) 26return result 27 28 29def answer (x ,n ): 30# test = [[1, 1, 1, 1, 1, 1, 0, 0, 0, 0], 31# [1, 1, 1, 0, 0, 0, 1, 1, 1, 0], 32# [1, 0, 0, 1, 1, 0, 1, 1, 0, 1], 33# [0, 1, 0, 1, 0, 1, 1, 0, 1, 1], 34# [0, 0, 1, 0, 1, 1, 0, 1, 1, 1]] 35# numeric = map(bin2num, test) 36# print verify(numeric, 3) 37test = """@@@@@@@@@@.............. 38@@@@......@@@@@@....@@@@ 39@...@@@...@@@...@@@.@@@. 40.@..@..@@.@..@@.@@.@@@.@ 41..@..@.@.@.@.@.@@.@@@.@@ 42...@..@.@@..@.@@.@@@.@@@ 43""" .strip ().split ('\n' ) 44test = map (lambda l :bin2num ([1 if i is '@' else 0 for i in l ]),test ) 45verify (test ,4 ) 46 47 48answer (5 ,3 )