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 Gunnar Hjalmarsson on April 3, 2008, 3:06 am
Please log in for more thread options
David Harmon wrote:
> On Wed, 02 Apr 2008 22:20:44 +0200 in comp.lang.perl.misc, Gunnar
>> As regards applicable docs on _the reason_ why I didn't just say
>>
>> local $_ = sprintf ...
>>
>> please see the "Localization of globs" section in "perldoc perlsub".
>
> What I'm wondering is why you didn't use the much simpler (from my
> point of view):
>
> foreach my $num ( 0 .. 0b11111 ) {
> my $yn = sprintf '%05b', $num;
> $yn =~ tr/01/NY/;
> print "$yn\n";
> }

Because ... I was in $_ mode. ;-)

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

Posted by Sandy on March 31, 2008, 5:21 pm
Please log in for more thread options
On Mar 31, 1:57 pm, joemacbusin...@yahoo.com wrote:
>
> 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?

If I understand you correctly, it sounds like a 5-bit binary with N
and Y instead of 0 and 1. Try looping through numbers 0..31 (max 5-bit
number) and call some transforming function (let's call it to_string),
which would loop trough the bits and produce a string of Ys and Ns
accordingly. I intentionally give you just an idea, so you could be
proud of yourself implementing it :) It will take some 15 lines of
code.

/sandy
http://myperlquiz.com/

Posted by Jürgen Exner on March 31, 2008, 5:36 pm
Please log in for more thread options
joemacbusiness@yahoo.com wrote:
>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?

I am sure it has been written many times. Search for permutation with
repetitions.

However, just for the fun of it, here's yet another implementation:

for (0..2**5-1) {
$_ = sprintf "%05b", $_;
s/0/N/g;
s/1/Y/g;
print "$_\n";
}

Posted by joemacbusiness on March 31, 2008, 6:21 pm
Please log in for more thread options
> joemacbusin...@yahoo.com wrote:
> >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?
>
> I am sure it has been written many times. Search for permutation with
> repetitions.
>
> However, just for the fun of it, here's yet another implementation:
>
> for (0..2**5-1) {
> =A0 =A0 $_ =3D sprintf "%05b", $_;
> =A0 =A0 s/0/N/g;
> =A0 =A0 s/1/Y/g;
> =A0 =A0 print "$_\n";
>
>
>
> }- Hide quoted text -
>
> - Show quoted text -
Hi All,

Got it!
Seriously - this was not an HW assignment. I want exception
reports for a report similar to the following:

Project Loc1 Loc2 Loc3 Loc4 Loc5
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
prod_1 Y Y Y Y Y
prod_2 Y Y N N N
prod_3 Y Y N N N
prod_4 Y Y Y Y Y
prod_5 Y Y Y Y Y
prod_6 Y Y Y Y Y
=2E..
stuff deleted.

So .... show all YYYYN's YNYNY's etc....

Very helpful, thanks again!

--JoeMac



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