Click here to get back home

every combination of Y/N in 5 positions

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
every combination of Y/N in 5 positions joemacbusiness 03-31-2008
Posted by joemacbusiness on March 31, 2008, 4:57 pm
Please log in for more thread options
Hi All,

This has probably already been written but I did not see it on CPAN.
Is there a code snippent that can print every possible combination
of Y/N's in a 5 position array or string?

For example: Y Y Y Y Y becomes
N Y Y Y Y
Y N Y Y Y....
Y Y N N Y etc.

Here is my first attempt but it only handles a single field change
moving from left to right etc...

Thanks, --Joe Mac.

#!/usr/bin/perl

my @array = qw(Y Y Y Y Y);

&jumble(0);
&jumble(1);
&jumble(2);
&jumble(3);
&jumble(4);

sub jumble {
my $arg = shift;
# print "$arg\n";
$newvar = &changeIt($arg);
if ($arg == 0){
print "$newvar $array[1] $array[2] $array[3] $array[4]\n";
} elsif ($arg == 1){
print "$array[0] $newvar $array[2] $array[3] $array[4]\n";
} elsif ($arg == 2){
print "$array[0] $array[1] $newvar $array[3] $array[4]\n";
} elsif ($arg == 3){
print "$array[0] $array[1] $array[2] $newvar $array[4]\n";
} elsif ($arg == 4){
print "$array[0] $array[1] $array[2] $array[3] $newvar\n";
}
}

sub changeIt {
my $var = shift;
#print "array[$var] is $array[$var]\n";
if ($array[$var] eq "Y"){
$var = "N";
}
return("$var");
}

#
# $ perl ./jumble.pl
N Y Y Y Y
Y N Y Y Y
Y Y N Y Y
Y Y Y N Y
Y Y Y Y N

Posted by smallpond on March 31, 2008, 5:06 pm
Please log in for more thread options
On Mar 31, 4:57 pm, joemacbusin...@yahoo.com wrote:
> Hi All,
>
> This has probably already been written but I did not see it on CPAN.
> Is there a code snippent that can print every possible combination
> of Y/N's in a 5 position array or string?
>
> For example: Y Y Y Y Y becomes
> N Y Y Y Y
> Y N Y Y Y....
> Y Y N N Y etc.
>

Who gets the credit for doing your homework?


Posted by Gunnar Hjalmarsson on March 31, 2008, 5:23 pm
Please log in for more thread options
smallpond wrote:
> On Mar 31, 4:57 pm, joemacbusin...@yahoo.com wrote:
>> Hi All,
>>
>> This has probably already been written but I did not see it on CPAN.
>> Is there a code snippent that can print every possible combination
>> of Y/N's in a 5 position array or string?
>>
>> For example: Y Y Y Y Y becomes
>> N Y Y Y Y
>> Y N Y Y Y....
>> Y Y N N Y etc.
>>
>
> Who gets the credit for doing your homework?

I do, I hope. :)

foreach my $num ( 0 .. 0b11111 ) {
local *_ = \ sprintf '%05b', $num;
tr/01/NY/;
print "$_\n";
}

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Posted by Uri Guttman on March 31, 2008, 6:28 pm
Please log in for more thread options

GH> smallpond wrote:
>> On Mar 31, 4:57 pm, joemacbusin...@yahoo.com wrote:
>>> Hi All,
>>>
>>> This has probably already been written but I did not see it on CPAN.
>>> Is there a code snippent that can print every possible combination
>>> of Y/N's in a 5 position array or string?
>>>
>>> For example: Y Y Y Y Y becomes
>>> N Y Y Y Y
>>> Y N Y Y Y....
>>> Y Y N N Y etc.
>>>
>> Who gets the credit for doing your homework?

GH> I do, I hope. :)

GH> foreach my $num ( 0 .. 0b11111 ) {
GH> local *_ = \ sprintf '%05b', $num;
GH> tr/01/NY/;
GH> print "$_\n";
GH> }

ok, now make it a oneliner and golf it! we ain't had a golf thread in
ages!

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Posted by Gunnar Hjalmarsson on April 1, 2008, 4:34 am
Please log in for more thread options
Uri Guttman wrote:
>
> GH> smallpond wrote:
> >> On Mar 31, 4:57 pm, joemacbusin...@yahoo.com wrote:
> >>> Hi All,
> >>>
> >>> This has probably already been written but I did not see it on CPAN.
> >>> Is there a code snippent that can print every possible combination
> >>> of Y/N's in a 5 position array or string?
> >>>
> >>> For example: Y Y Y Y Y becomes
> >>> N Y Y Y Y
> >>> Y N Y Y Y....
> >>> Y Y N N Y etc.
> >>>
> >> Who gets the credit for doing your homework?
>
> GH> I do, I hope. :)
>
> GH> foreach my $num ( 0 .. 0b11111 ) {
> GH> local *_ = \ sprintf '%05b', $num;
> GH> tr/01/NY/;
> GH> print "$_\n";
> GH> }
>
> ok, now make it a oneliner and golf it! we ain't had a golf thread in
> ages!

I'm not a golfer, but it's easy to write obfuscated code using Perl...

perl -le"dofor(0..0b11111)"

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Similar ThreadsPosted
Looking for combination.pm June 29, 2005, 10:56 pm
Perl + Objects = Winning combination April 9, 2007, 7:55 pm
Perl Developer Positions in California March 11, 2005, 12:53 pm
htaccess rewriterule in combination with POST form January 28, 2005, 12:38 am
How can I add tokens at arbitrary positions on a line in a file? August 13, 2005, 6:36 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap