13
\$\begingroup\$

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!

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

14 Answers 14

17
\$\begingroup\$

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.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ One byte ! That solved it ! \$\endgroup\$
    – Luatic
    Dec 10, 2016 at 23:01
  • 5
    \$\begingroup\$ You know what, I'm gonna submit a ZERO byte answer! In JAVA! How do you like that, huh? :) (Seriously, though. Nice golfing.) \$\endgroup\$
    – AAM111
    Dec 11, 2016 at 0:37
9
\$\begingroup\$

Haskell, 20 bytes

(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.

\$\endgroup\$
8
\$\begingroup\$

MATL, 2 bytes

Z^

Cartesian power builtin...

Try it online!

\$\endgroup\$
6
\$\begingroup\$

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
\$\endgroup\$
2
  • \$\begingroup\$ this seems to solve it, but probably somebody else is able to do this with one byte ? \$\endgroup\$
    – Luatic
    Dec 10, 2016 at 23:00
  • \$\begingroup\$ yes, somebody else won(jelly, one byte) but nice solution, anyway \$\endgroup\$
    – Luatic
    Dec 11, 2016 at 19:26
6
\$\begingroup\$

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
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ Dec 10, 2016 at 21:30
5
\$\begingroup\$

JavaScript (Firefox 30+), 55 bytes

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

I'm 99% certain recursion is the best way to go about this in JavaScript.

\$\endgroup\$
5
\$\begingroup\$

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}}

\$\endgroup\$
4
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.

\$\endgroup\$
3
\$\begingroup\$

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.

Ideone.

\$\endgroup\$
7
  • \$\begingroup\$ This doesn't seem to work. \$\endgroup\$ Dec 12, 2016 at 2:09
  • \$\begingroup\$ It only repeats the input n times, doesn't print all possible combinations. \$\endgroup\$ Dec 12, 2016 at 3:04
  • \$\begingroup\$ @IporSircer I clarified the required input format. Does that work for you now? \$\endgroup\$ Dec 12, 2016 at 17:15
  • \$\begingroup\$ bash a.sh "{0,1}" 2 --> {0,1}{0,1} (version 4.4.5(1)-release) \$\endgroup\$ Dec 13, 2016 at 18:08
  • 1
    \$\begingroup\$ @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/… \$\endgroup\$ Dec 13, 2016 at 19:37
3
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ Just rev for 45 bytes :) \$\endgroup\$
    – JayCe
    May 8, 2018 at 17:46
  • \$\begingroup\$ Had matrix on the mind and forgot the result was actually a list (data frame). \$\endgroup\$
    – ngm
    May 8, 2018 at 19:09
2
\$\begingroup\$

05AB1E, 2 1 bytes

ã

-1 byte thanks to @Emigna.

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
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You don't need the I here. \$\endgroup\$
    – Emigna
    May 8, 2018 at 15:29
  • \$\begingroup\$ @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! \$\endgroup\$ May 8, 2018 at 15:40
1
\$\begingroup\$

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
\$\endgroup\$
1
\$\begingroup\$

PHP, 109 bytes

for($o=$a=array_slice($argv,2);--$argv[1];$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[1];$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.

\$\endgroup\$
1
  • \$\begingroup\$ You wont have to handle length 0. \$\endgroup\$
    – Luatic
    Dec 12, 2016 at 14:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.