# Generate all combinations of given list of elements, sorted

Make a code that takes a list and a number as input, and generates all possible combinations with the length of the number. For example, with the list {0,1} and the number 2:

00
01
10
11


Your program doesn't have to expect characters twice or more often in the list, such as {0,0,0,0,0,1,1,5,5}

Make sure you print the combinations out sorted, in the order of the list:

With the list {0,1} and the number 5 (generated by some of my code, which is a way too long to win):

00000
00001
00010
00011
00100
00101
00110
00111
01000
01001
01010
01011
01100
01101
01110
01111
10000
10001
10010
10011
10100
10101
10110
10111
11000
11001
11010
11011
11100
11101
11110
11111


But with the list {1,0} and the number 2:

11
10
01
00


As you can see, reversed list means reversed order.

Take a look at the structure, it's like a tree.

This is code-golf, so the shortest code in bytes wins!

• I thought this would be duplicate, but I can't find one – Luis Mendo Dec 10 '16 at 19:31
• How does the sorting work if the input list isnt sorted? – JAD Dec 10 '16 at 19:35
• @Jarko I'd assume the tuples of indices of the input are sorted in the output – Luis Mendo Dec 10 '16 at 19:36
• @brad, the default answer is yes unless OP has said something else. – Stewie Griffin Dec 10 '16 at 21:51
• I don't understand... what's wrong with using my own name as a username? – Stewie Griffin Dec 11 '16 at 12:20

# Jelly, 1 byte

ṗ


TryItOnline

Cartesian power built-in atom, as a dyadic link with left argument the items and right argument the count, or as a full program with first argument the items and second argument the count.

• One byte ! That solved it ! – LMD Dec 10 '16 at 23:01
• You know what, I'm gonna submit a ZERO byte answer! In JAVA! How do you like that, huh? :) (Seriously, though. Nice golfing.) – OldBunny2800 Dec 11 '16 at 0:37

(mapM id.).replicate


Usage exaple:

*Main> ( (mapM id.).replicate )  2 "01"
["00","01","10","11"]
*Main> ( (mapM id.).replicate )  2 "10"
["11","10","01","00"]


replicate makes n copies of the 2nd parameter and mapM id builds the combinations. Btw, mapM id is the same as sequence, but 1 byte less.

# MATL, 2 bytes

Z^


Cartesian power builtin...

Try it online!

# Pyth, 2 bytes

^F


A program that takes input in the form list,number and prints a list of lists.

Test suite

How it works

^F   Program. Input: Q
^FQ  Implicit input fill
F   Fold
^    repeated Cartesian power
Q  over Q
Implicitly print

• this seems to solve it, but probably somebody else is able to do this with one byte ? – LMD Dec 10 '16 at 23:00
• yes, somebody else won(jelly, one byte) but nice solution, anyway – LMD Dec 11 '16 at 19:26

# Perl 6, 15 bytes

{[X] @^a xx$^b}  ## Explanation: {[X] @^a xx$^b}

{             } # bare block lambda

@^a        # declare first parameter as Positional
$^b # declare second parameter xx # list repeat ｢@a｣, ｢$b｣ times

# at this point given ｢ (0,1), 5 ｣
# ((0 1) (0 1) (0 1) (0 1) (0 1))

[ ]            # list reduce
X             #    using cross meta-operator

# results in a list of lists
# ((0 0 0 0 0)
#  (0 0 0 0 1)
#  (0 0 0 1 0)
#  (0 0 0 1 1)
#  (0 0 1 0 0)
#  (0 0 1 0 1)
#  ...
#  (1 1 1 1 1))

say {[X] $^a xx$^b}( (0,1), 2 ); # ((0 0) (0 1) (1 0) (1 1))
say {[X] $^a xx$^b}( (1,0), 2 ); # ((1 1) (1 0) (0 1) (0 0))
say {[X] $^a xx$^b}( (0,1,2), 2 );
# ((0 0) (0 1) (0 2) (1 0) (1 1) (1 2) (2 0) (2 1) (2 2))

put {[X] $^a xx$^b}( (0,1), 5 )».join;
# 00000 00001 00010 00011 00100 00101 00110 00111 01000 01001 01010 01011 01100 01101 01110 01111 10000 10001 10010 10011 10100 10101 10110 10111 11000 11001 11010 11011 11100 11101 11110 11111

• If they ever upgrade to a release of Rakudo from after the official release of Perl 6 you will be able to run this at Ideone. – Brad Gilbert b2gills Dec 10 '16 at 21:30

# JavaScript (Firefox 30+), 55 bytes

f=(a,n)=>n?[for(b of a)for(c of f(a,n-1))[b,...c]]:[[]]


## Perl, 30 bytes

28 bytes of code + -nl flag.

$"=",";say for glob"{@F}"x<>  To run it: perl -alE '$"=",";say for glob"{@F}"x<>' <<< "1 0
2"


I think that taking the input as a list of numbers is logical for Perl. However, if we allow some fantasy, and take the input with the brackets and comma (as shown in the question), we can go down to 20 bytes:

perl -nlE 'say for glob$_ x<>' <<< "{1,0} 2"  Explanations: glob initial purpose in Perl is list and iterate through filenames, but when its argument contains curly brackets, it generates combinations formed of one element of each bracket group. -a autosplit on spaces the input, and put the result inside @F array. $" is the list separator: it's the separator inserted between the elements of a list inside a string. We set it to ,, so "{@F"} produces {.,.} (if @F contains 0 and 1).
Then x is the string repetition operator (and <> gets one line of input).
And finally, say for iterates through the list generated by glob and prints the elements.

# Mathematica, 6 bytes

Tuples


Still worse than Jelly :(

# Usage

Tuples[{0, 1}, 5]


{{0, 0, 0, 0, 0}, {0, 0, 0, 0, 1}, {0, 0, 0, 1, 0}, {0, 0, 0, 1, 1}, {0, 0, 1, 0, 0}, {0, 0, 1, 0, 1}, {0, 0, 1, 1, 0}, {0, 0, 1, 1, 1}, {0, 1, 0, 0, 0}, {0, 1, 0, 0, 1}, {0, 1, 0, 1, 0}, {0, 1, 0, 1, 1}, {0, 1, 1, 0, 0}, {0, 1, 1, 0, 1}, {0, 1, 1, 1, 0}, {0, 1, 1, 1, 1}, {1, 0, 0, 0, 0}, {1, 0, 0, 0, 1}, {1, 0, 0, 1, 0}, {1, 0, 0, 1, 1}, {1, 0, 1, 0, 0}, {1, 0, 1, 0, 1}, {1, 0, 1, 1, 0}, {1, 0, 1, 1, 1}, {1, 1, 0, 0, 0}, {1, 1, 0, 0, 1}, {1, 1, 0, 1, 0}, {1, 1, 0, 1, 1}, {1, 1, 1, 0, 0}, {1, 1, 1, 0, 1}, {1, 1, 1, 1, 0}, {1, 1, 1, 1, 1}}

# Python, 57 bytes

from itertools import*
lambda o,n:list(product(*([o]*n)))


repl.it

Unnamed function taking a list of objects, o and a count, n and returning a list of the combinations.

# Pure Bash, 36

printf -vv %$2s eval echo${v// /$1}  Input by command-line params - The list is a comma-separated list in braces, e.g.: ./elemcombo.sh "{0,1}" 2  Note the input list needs to be quoted so the calling shell does not expand it too early. • This doesn't seem to work. – Ipor Sircer Dec 12 '16 at 2:09 • It only repeats the input n times, doesn't print all possible combinations. – Ipor Sircer Dec 12 '16 at 3:04 • @IporSircer I clarified the required input format. Does that work for you now? – Digital Trauma Dec 12 '16 at 17:15 • bash a.sh "{0,1}" 2 --> {0,1}{0,1} (version 4.4.5(1)-release) – Ipor Sircer Dec 13 '16 at 18:08 • @IporSircer It looks like TIO is probably putting the args into an execve() or similar call. The quotes are only needed when the script is called from another shell in order to prevent the calling shell from doing expansion of the braces. I the TIO case, the list does not need quotes. tio.run/nexus/… – Digital Trauma Dec 13 '16 at 19:37 # R, 53 45 bytes function(x,n)rev(expand.grid(rep(list(x),n)))  Try it online! rev is there to the conform with the precise sort order requested (which doesn't really seem essential to the problem) and adds 5 bytes. • Just rev for 45 bytes :) – JayCe May 8 '18 at 17:46 • Had matrix on the mind and forgot the result was actually a list (data frame). – ngm May 8 '18 at 19:09 ## Racket 123 bytes (let p((s "")(l(map number->string(sort l <)))) (if(= n(string-length s))(displayln s)(for((i l))(p(string-append s i)l))))  Ungolfed: (define(f l n) (let loop ((s "") (l (map number->string (sort l <)))) (if (= n (string-length s)) (displayln s) (for ((i l)) (loop (string-append s i) l)))))  Testing: (f '(0 1) 2) (f '(0 1) 3) (f '(0 1) 5)  Output: 00 01 10 11 000 001 010 011 100 101 110 111 00000 00001 00010 00011 00100 00101 00110 00111 01000 01001 01010 01011 01100 01101 01110 01111 10000 10001 10010 10011 10100 10101 10110 10111 11000 11001 11010 11011 11100 11101 11110 11111  # PHP, 109 bytes for($o=$a=array_slice($argv,2);--$argv;$o=$c,$c=[])foreach($a as$n)foreach($o as$s)$c[]=$n.$s;print_r($o);


Takes the length as the first argument and the list as any further arguments.
Use like:

php -r "for($o=$a=array_slice($argv,2);--$argv;$o=$c,$c=[])foreach($a as$n)foreach($o as$s)$c[]=$n.$s;print_r(\$o);" 5 0 1


Will run into an "out of memory" fatal error if asked for length 0.

• You wont have to handle length 0. – LMD Dec 12 '16 at 14:43

# 05AB1E, 2 1 bytes

ã


-1 byte thanks to @Enigma.

Try it online.

Input as number\nlist, output as list of lists.

Explanation:

     # Implicit input a, b
ã    # Take the Cartesian product of list b repeated a times

• You don't need the I here. – Emigna May 8 '18 at 15:29
• @Emigna Ah of course. I had the I because I initially was trying to figure out how to have multiple inputs and had the number and list reversed. Pretty stupid to keep the I there.. Thanks! – Kevin Cruijssen May 8 '18 at 15:40