The challenge
You're given:
- a non-empty, unsorted list h of positive integers (the haystack)
- a positive integer n (the needle)
Your task is to return the list of all unique decimal concatenations of permutations of h whose binary representation contains the binary representation of n.
Examples
h = [ 1, 2, 3 ]
n = 65There's only one matching concatenation, so the expected output is
[321]
.h = [ 1, 2, 3 ]
n = 7This time, there are three concatenations which contain the binary pattern 111. The expected output is
[123, 231, 312]
.h = [ 12, 3 ]
n = 7Only two permutations are available and both are matching. The expected output is
[123, 312]
.h = [ 1, 2, 2 ]
n = 15The only matching concatenation is 122 (1111010 in binary, which contains 1111), so the expected output is
[122]
. Note that two permutations actually lead to 122 but you are not allowed to output[122, 122]
.
Clarifications and rules
- You may take the needle as an integer (
65
), a string representing a decimal value ("65"
) or a string representing a binary value ("1000001"
). - You may take the haystack as a native array/object/set of integers (
[11,12,13]
), a native array/object/set of strings representing decimal values (["11","12","13"]
), or a delimited string of decimal values ("11 12 13"
or"11,12,13"
). You may also opt for a variant using arrays of digits (like[[1,1],[1,2],[1,3]]
). - The output must follow one of the formats described above for the haystack, but not necessarily the same one.
- You're not supposed to handle haystacks whose highest decimal concatenation is greater than the highest representable unsigned integer in your language.
- Apart from that, your code should theoretically support any input -- assuming it's given enough time and memory.
- This is
SPARTA!code-golf, so the shortest answer in bytes win!
Test cases
Haystack | Needle | Output
---------------------+----------+-----------------------------------
[ 1, 2, 3 ] | 65 | [ 321 ]
[ 1, 2, 3 ] | 7 | [ 123, 231, 312 ]
[ 12, 3 ] | 7 | [ 123, 312 ]
[ 1, 2, 2 ] | 15 | [ 122 ]
[ 1, 2 ] | 7 | []
[ 12, 34, 56 ] | 21 | [ 125634, 341256, 345612, 563412 ]
[ 1, 2, 3, 4, 5 ] | 511 | [ 53241 ]
[ 1, 3, 5, 7, 9 ] | 593 | [ 37519, 51793, 75913, 75931 ]
[ 11, 12, 13, 14 ] | 12141311 | [ 12141311 ]
[ 1, 2, 1, 2, 1, 2 ] | 1015 | [ 221112 ]
set([(1, 2, 2)])
. Is it valid or should I get rid ofset
? \$\endgroup\$["12","3"]
and["1","23"]
are two distinct haystacks. \$\endgroup\$