DEV Community

Discussion on: Daily Challenge #299 - Time to Grille!

Collapse
 
agtoever profile image
agtoever

Python 3-liner with test cases and TIO link.
Apart from some type conversions, itertools.compress does the job.

import itertools
filterlist = lambda value, length: map(int, bin(value)[2:].zfill(length))
grille = lambda string, grille: ''.join(itertools.compress(string, filterlist(grille, len(string))))

cases = [
    ('abcdef', 5),
    ('0abc', 2),
    ('abcde', 32),
    ('abcd', 1)
]
for case in cases:
    print(f'grille{case} has filter {list(filterlist(case[1], len(case[0])))} and outcome: {grille(*case)}')
Enter fullscreen mode Exit fullscreen mode

Try it online!