Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageOn 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?
Post Follow-up to this messageOn 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/
Post Follow-up to this messagesmallpond 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
Post Follow-up to this messagejoemacbusiness@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";
}
Post Follow-up to this messageOn 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
Post Follow-up to this message>>>>> "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 --------
-
Post Follow-up to this messageUri 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
Post Follow-up to this messageReply-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.
Post Follow-up to this messageMirco 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
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.