+/foobar

google programming puzzles

git clone https://git.julia.blue/+/foobar

Joseph RyanStart on sets6422597

master
1.2 KiB48 linesraw
1from itertools import combinations
2
3def verify(los, num):
4    target = max(map(max, los)) + 1
5    for subset in combinations(los, num):
6        if len(reduce(lambda x, y: x | y, subset)) != target:
7            return False
8    for subset in combinations(los, num - 1):
9        if len(reduce(lambda x, y: x | y, subset)) == target:
10            return False
11    return True
12
13
14def num2bin(s, size):
15    result = [0] * size
16    for n in s:
17        result[n] = 1
18    return result
19
20
21def bin2num(s):
22    result = set([])
23    for i in range(len(s)):
24        if s[i]:
25            result.add(i)
26    return 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)
37    test = """@@@@@@@@@@..............
38@@@@......@@@@@@....@@@@
39@...@@@...@@@...@@@.@@@.
40.@..@..@@.@..@@.@@.@@@.@
41..@..@.@.@.@.@.@@.@@@.@@
42...@..@.@@..@.@@.@@@.@@@
43""".strip().split('\n')
44    test = map(lambda l: bin2num([1 if i is '@' else 0 for i in l]), test)
45    print verify(test, 4)
46
47
48answer(5, 3)