Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

every combination of Y/N in 5 positions
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

Report this thread to moderator Post Follow-up to this message
Old Post
joemacbusiness@yahoo.com
04-01-08 02:11 AM


Re: every combination of Y/N in 5 positions
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?


Report this thread to moderator Post Follow-up to this message
Old Post
smallpond
04-01-08 02:12 AM


Re: every combination of Y/N in 5 positions
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/

Report this thread to moderator Post Follow-up to this message
Old Post
Sandy
04-01-08 02:13 AM


Re: every combination of Y/N in 5 positions
smallpond wrote:
> On Mar 31, 4:57 pm, joemacbusin...@yahoo.com wrote: 
>
> 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

Report this thread to moderator Post Follow-up to this message
Old Post
Gunnar Hjalmarsson
04-01-08 02:13 AM


Re: every combination of Y/N in 5 positions
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";
}

Report this thread to moderator Post Follow-up to this message
Old Post
Jürgen Exner
04-01-08 02:14 AM


Re: every combination of Y/N in 5 positions
On Mar 31, 2:36=A0pm, J=FCrgen Exner <jurge...@hotmail.com> wrote:
> joemacbusin...@yahoo.com wrote: 
>
> 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



Report this thread to moderator Post Follow-up to this message
Old Post
joemacbusiness@yahoo.com
04-01-08 02:14 AM


Re: every combination of Y/N in 5 positions
>>>>> "GH" == Gunnar Hjalmarsson <noreply@gunnar.cc> writes:

GH> smallpond wrote: 

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

Report this thread to moderator Post Follow-up to this message
Old Post
Uri Guttman
04-01-08 02:16 AM


Re: every combination of Y/N in 5 positions
Uri Guttman wrote: 
>
>   GH> smallpond wrote: 
>
>   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"do{$_=sprintf'%05b',$_;y/01/NY/;print}for(0..0b11111)"

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

Report this thread to moderator Post Follow-up to this message
Old Post
Gunnar Hjalmarsson
04-01-08 09:31 AM


Re: every combination of Y/N in 5 positions
Reply-To: wahab-mail@gmx.de
NNTP-Posting-Host: krull.phych.tu-freiberg.de
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: nserver.hrz.tu-freiberg.de 1207048678 45872 139.20.37.88 (1 Apr 200
8 11:17:58 GMT)
X-Complaints-To: usenet@nserver.hrz.tu-freiberg.de
NNTP-Posting-Date: Tue, 1 Apr 2008 11:17:58 +0000 (UTC)
User-Agent: Thunderbird 2.0.0.12 (Windows/20080213)
In-Reply-To: <65eah7F2fa13jU1@mid.individual.net>
Bytes: 1514
Xref: number1.nntp.dca.giganews.com comp.lang.perl.misc:647711

Gunnar Hjalmarsson wrote:
> I'm not a golfer, but it's easy to write obfuscated code using Perl...
>     perl -le"do{$_=sprintf'%05b',$_;y/01/NY/;print}for(0..0b11111)"

Much too wordy ;-)

perl -e' print "$_\n" while glob"{Y,N}"x5'


Regards

M.

Report this thread to moderator Post Follow-up to this message
Old Post
Mirco Wahab
04-01-08 01:09 PM


Re: every combination of Y/N in 5 positions
Mirco Wahab wrote:
> Gunnar Hjalmarsson wrote: 
>
> Much too wordy ;-)

I concur.

>  perl -e' print "$_\n" while glob"{Y,N}"x5'

perl -le'print for glob"{Y,N}"x5'



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

Report this thread to moderator Post Follow-up to this message
Old Post
John W. Krahn
04-01-08 01:09 PM


Sponsored Links




Last Thread Next Thread Next
Pages (3): [1] 2 3 »
Search this forum -> 
Post New Thread

PERL Miscellaneous archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 09:46 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.