### (Golf) Ordered Combinations

by MeowChow (Vicar)
 on Apr 25, 2001 at 00:45 UTC Need Help??

The challenge is to write a sub that takes the paramters (\$length, @symbols) and produces an ordered list of all sequnces of length \$length that are combinations of characters from the list @symbols. The output ordering is based on the order of @symbols. Assume that @symbols is a list comprised of any quantity of one-byte ASCII chars.

My current attempt at 52 chars:

```sub c{my\$n=-1+shift;\$n?map{my\$c=\$_;map\$c.\$_,c(\$n,@_)}@_:@_}
Example:
```print join " ", c qw(2 a b c d);
print "\n";
print join " ", c qw(4 0 1);

# outputs
aa ab ac ad ba bb bc bd ca cb cc cd da db dc dd
0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101
+1110 1111
Good luck, and may your efforts yield little code :)
```   MeowChow
s aamecha.s a..a\u\$&owag.print```

Replies are listed 'Best First'.
Re: (Golf) Ordered Combinations
by sachmet (Scribe) on Apr 25, 2001 at 01:42 UTC
51 :-)
```#!/usr/bin/perl

sub c {my\$n=-1+shift;\$n?map{my\$c=\$_;map\$c.\$_,c(\$n,@_)}@_:@_};
sub d {my\$n=shift;--\$n?map{my\$d=\$_;map\$d.\$_,d(\$n,@_)}@_:@_};

print join " ", c qw(2 a b c);
print "\n";
print join " ", d qw(2 a b c);
print "\n";
print join " ", c qw(4 0 1);
print "\n";
print join " ", d qw(4 0 1);
print "\n";
Hi, i am trying following script to create 9 letter combination but not works...waiting for 8 hours but my computer keep no result. but 7 letter is still ok. Can someone help me to solve the issue? #!/usr/bin/perl sub c {my\$n=-1+shift;\$n?map{my\$c=\$_;map\$c.\$_,c(\$n,@_)}@_:@_}; print join " ", c qw(9 a b c d e f g h i); print "\n";

Howdy adolfo, welcome to the Monastery!

In general, you'll have better luck posting your question as a new top-level post in Seekers of Perl Wisdom. Also, while you're at it, please use <code> tags to format your code; see Writeup Formatting Tips for more.

Also: the particular solution to the "ordered combinations" problem here is a golfed solution, meaning that the aim was to use as few keystrokes as possible, at the expense of (among other things) readability, time and space complexity. Unless you're specifically interested in Perl golf, you're likely better off using a different solution.

Re (tilly) 1: (Golf) Ordered Combinations
by tilly (Archbishop) on Apr 25, 2001 at 06:51 UTC
If we don't have to be strict compliant...49.
```sub c {
@r='';@r=map{\$c=\$_;map\$c.\$_,@r}@_ for 1..shift;@r
}
(Note: I handle the case of \$n=0 correctly. The solution that you started with does not...)
(Note: I handle the case of \$n=0 correctly. The solution that you started with does not...)

I can't slip anything past you, can I? :-)

```   MeowChow
s aamecha.s a..a\u\$&owag.print```
Is there any way to print these results to a file?
Re: (Golf) Ordered Combinations
by satchboost (Scribe) on Apr 25, 2001 at 01:55 UTC
I know that I'm bending the rules a little, but this does get below 50.

sub c{my\$n=pop;--\$n?map{my\$c=\$_;map\$c.\$_,c(@_,\$n)}@_:@_}

Example:

```print join " ", c qw(a b c 2);
print "\n";
print join " ", c qw(0 1 4);
print "\n";

Create A New User
Node Status?
node history
Node Type: obfuscated [id://75261]
Approved by root
help
Chatterbox?
 [marto]: Eily, there's actually a lot of good discussion on the topic in the HN link choroba posted [marto]: sorry for the late response, busy busy :/ [Corion]: I think you need to load Readline in the debugger, but I don't know how [Corion]: Yay, invitation for a team-building workshop, with a team of 100 people :-/ [Eily]: marto after an additional read I started to see the light actually. And choroba's link was helpful as well :) [Corion]: I'll send my colleague who doesn't yet know the people there, and I'll do production duty instead.

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (15)
As of 2017-07-26 15:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
I came, I saw, I ...

Results (396 votes). Check out past polls.