The Binary Fences

2018-10-08 13:21:58

Input:

An integer n in the range 2 <= n <= 10

A list of positive integers

Output:

Convert the integers to their binary representation (without any leading zeroes), and join them all together.

Then determine all binary substrings that form a 'binary fence' using n amount of fence posts. The spaces (zeros) between each fence post are irrelevant (at least 1), but the fence posts themselves should all be of equal width.

Here the regexes the binary substrings should match for each n:

n Regex to match to be a 'binary fence' Some examples

2 ^(1+)0+\1$ 101; 1100011; 1110111;

3 ^(1+)0+\10+\1$ 10101; 100010101; 110011011001;

4 ^(1+)0+\10+\10+\1$ 1010101; 110110011011; 11110111100001111001111;

etc. etc. You get the point

Looking at the n=4 examples:

1010101

^ ^ ^ ^ All fence posts have a width of one 1

^ ^ ^ with one or more 0s in between them

110110011011

^^ ^^ ^^ ^^ All fenc

  • Python 2, 271 246 223 bytes

    import re

    def f(n,l):S=[bin(x)[2:]for x in l];print[[l[j]for j in range(len(l))if i-len(S[j])

    Try it online!

    Still golfing...

    -1 byte, thanks to Kevin Cruijssen

    2018-10-08 13:36:14