C
cate
I want to see what a real jones can do with this.. IF you're bored.
Here's the interview question:
http://www.codeeval.com/public_sc/14/
Here's the kluge I came up with (they won't be talking to me!) ... a
mess. Show me the 3 liner you guys could do.
@s = (a,h,t); # sorting would give you this
@result = ();
for ($i=0; $i < @s; $i++) {
my $pa = [];
push @$pa, $s[$i];
nextone($max = 20, $i, $pa, @s);
}
print "Answer : " . join (",", @result) . "\n";
sub nextone {
my ($max,# (u) sentinel
$i, # (i) index of current character already pushed
$pa, # (u) word assembly
@s # (u) original, sorted word characters
) = @_;
if ($max-- < 1) {
print "max reached \n";
exit;
}
my $lengtharray = scalar(@s);
if ($lengtharray == 1) {
push @result, join("", @{$pa});
pop @{$pa};
return;
}
if ($i == 0 ) {
@s = @s[1 .. $lengtharray - 1];
} elsif ($i > 0) {
@s = @s[0 .. $i - 1, $i+1 .. $lengtharray - 1];
}
for (my $j = 0; $j < scalar @s; $j++) {
$l = scalar @s;
push @{$pa}, $s[$j];
nextone($max, $j, $pa, @s);
}
pop @{$pa};
}
=================================================
in case the site changes
Challenge Description
Write a program to print out all the permutations of a string in
alphabetical order.
Input
The first argument will be a text file containing an input string, one
per line. e.g.
hat
Output
Print to stdout, permutations of the string, comma separated, in
alphabetical order.
e.g.
aht,ath,hat,hta,tah,tha
Here's the interview question:
http://www.codeeval.com/public_sc/14/
Here's the kluge I came up with (they won't be talking to me!) ... a
mess. Show me the 3 liner you guys could do.
@s = (a,h,t); # sorting would give you this
@result = ();
for ($i=0; $i < @s; $i++) {
my $pa = [];
push @$pa, $s[$i];
nextone($max = 20, $i, $pa, @s);
}
print "Answer : " . join (",", @result) . "\n";
sub nextone {
my ($max,# (u) sentinel
$i, # (i) index of current character already pushed
$pa, # (u) word assembly
@s # (u) original, sorted word characters
) = @_;
if ($max-- < 1) {
print "max reached \n";
exit;
}
my $lengtharray = scalar(@s);
if ($lengtharray == 1) {
push @result, join("", @{$pa});
pop @{$pa};
return;
}
if ($i == 0 ) {
@s = @s[1 .. $lengtharray - 1];
} elsif ($i > 0) {
@s = @s[0 .. $i - 1, $i+1 .. $lengtharray - 1];
}
for (my $j = 0; $j < scalar @s; $j++) {
$l = scalar @s;
push @{$pa}, $s[$j];
nextone($max, $j, $pa, @s);
}
pop @{$pa};
}
=================================================
in case the site changes
Challenge Description
Write a program to print out all the permutations of a string in
alphabetical order.
Input
The first argument will be a text file containing an input string, one
per line. e.g.
hat
Output
Print to stdout, permutations of the string, comma separated, in
alphabetical order.
e.g.
aht,ath,hat,hta,tah,tha