every combination of Y/N in 5 positions

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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

Re: every combination of Y/N in 5 positions

On Mar 31, 4:57 pm, joemacbusin...@yahoo.com wrote:

Who gets the credit for doing your homework?

Re: every combination of Y/N in 5 positions

smallpond wrote:

I do, I hope. :)

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

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

Re: every combination of Y/N in 5 positions

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

Re: every combination of Y/N in 5 positions

Uri Guttman wrote:

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

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

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

Re: every combination of Y/N in 5 positions

Much too wordy ;-)

perl -e' print "\$_\n" while glob""x5'

Regards

M.

Re: every combination of Y/N in 5 positions

Mirco Wahab wrote:

I concur.

perl -le'print for glob""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

Re: every combination of Y/N in 5 positions

_
John W. Krahn (someone@example.com) wrote on VCCCXXVII September MCMXCIII
-:  Mirco Wahab wrote:
-: >> I'm not a golfer, but it's easy to write obfuscated code using Perl...
-: >>     perl -le"dofor(0..0b11111)"
-: >
-: > Much too wordy ;-)
-:
-:  I concur.
-:
-: >  perl -e' print "\$_\n" while glob""x5'
-:
-:      perl -le'print for glob""x5'

Saving 3 chars:

perl -E'say for glob""x5'

Abigail
--
\$"=\$,;*}}=*_;
sub   _   {push         @_ => /::(.*)/s and goto &{ shift}}
sub shift {print shift; @_              and goto &}
Hack ("Just", "Perl ", " ano", "er\n", "ther "); # 20080401

Re: every combination of Y/N in 5 positions

Abigail wrote:
)                                              _
) John W. Krahn (someone@example.com) wrote on VCCCXXVII September MCMXCIII
) -:  Mirco Wahab wrote:
) -: > Gunnar Hjalmarsson wrote:
) -: >> I'm not a golfer, but it's easy to write obfuscated code using Perl...
) -: >>     perl -le"dofor(0..0b11111)"
) -: >
) -: > Much too wordy ;-)
) -:
) -:  I concur.
) -:
) -: >  perl -e' print "\$_\n" while glob""x5'
) -:
) -:      perl -le'print for glob""x5'
)
) Saving 3 chars:
)
)         perl -E'say for glob""x5'

perl -e'print glob""x5'

But this is nicer:

perl -e'\$,="\n";print glob""x5'

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Re: every combination of Y/N in 5 positions

_
Willem (willem@stack.nl) wrote on VCCCXXVII September MCMXCIII in
??  Abigail wrote:
??  )                                              _
??  ) John W. Krahn (someone@example.com) wrote on VCCCXXVII September MCMXCIII
??  ) -:  Mirco Wahab wrote:
??  ) -: > Gunnar Hjalmarsson wrote:
??  ) -: >> I'm not a golfer, but it's easy to write obfuscated code using
Perl...
??  ) -: >>     perl -le"dofor(0..0b11111)"
??  ) -: >
??  ) -: > Much too wordy ;-)
??  ) -:
??  ) -:  I concur.
??  ) -:
??  ) -: >  perl -e' print "\$_\n" while glob""x5'
??  ) -:
??  ) -:      perl -le'print for glob""x5'
??  )
??  ) Saving 3 chars:
??  )
??  )         perl -E'say for glob""x5'
??
??  perl -e'print glob""x5'

But that doesn't print newlines after each entry.

??  But this is nicer:
??
??  perl -e'\$,="\n";print glob""x5'

But this subthread involves a golfed solution, not a nice necessarely a
nice one...

Abigail
--
perl -we 'print split /(?=(.*))/s => "Just another Perl Hacker\n";'

Re: every combination of Y/N in 5 positions

Abigail wrote:
) But that doesn't print newlines after each entry.

Oh right, I missed the -l flag. Sorry.

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

Re: every combination of Y/N in 5 positions

Huh.  That doesn't look "obfuscated" to me at all.  I read "write \$_
back to itself as a 0-padded 5-position binary field, transliterate
01 to NY, print, repeat for \$_ = 0 through 31".  Outputs "NNNNN"
through "YYYYY", each on its own line.

(As a newbie Perl programmer, I don't know whether the fact that the
above one-liner doesn't seem "obfuscated" to me should make me happy
or terrified.  Perhaps a little of both.)

Maybe you're using the wrong language.  Try C for obfuscation:

#include <stdio.h>
main(t,_,a)char* a;\
{return!0<t?t<3?main(-79,-13,a+main(-87,1-_,\
main(-86, 0, a+1 )+a)):1,t<_?main(t+1, _, a ):3,main ( -94, -27+t, a\
)&&t == 2 ?_<13 ?main ( 2, _+1, "%s %d %d\n" ):9:16:t<0?t<-72?main(_,\
t,"@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l,+,/n{n+\
,/+#n+,/#;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l q#'+d'K#!/\
+k#;q#'r}eKK#}w'r}eKKw')rw' i;# ){n\
l]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#\
n'wk nw' iwk{KK{nl]!/w{%'l##w#' i; :'c \
;;rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;\
#'rdq#w! nr'/ ') }+}{rl#'{n' ')# }'+}##(!!/")\
:t<-50?_==*a ?putchar(a[31]):main(-65,_,a+1):main((*a == '/')+t,_,a\
+1 ):0<t?main ( 2, 2 , "%s"):*a=='/'||main(0,main(-61,*a, "!ek;dc \
i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);}

(Note: I'm not the author of that.)

Compile that with a C compiler and run it.  You'll be amazed at what
it does.  Ho, ho, ho!  Merry Christmas!  Or Merry April Fools Day,
as the case may be.

BONUS PROBLEM FOR PERL HACKERS HERE:  Translate the above C program
into Perl... and be sure you make it just as obfuscated!  (I tried,
but didn't get very far; trying to figure it out gave me a headache.
The fact that main calls itself recursively in 11 different places
is enough to fill me with horror.)

--
Cheers,
Robbie Hatley
lonewolf aatt well dott com
www dott well dott com slant user slant lonewolf slant

Re: every combination of Y/N in 5 positions

Nor to me. Except for the lack of whitespace and possibly the
reassignement to \$_, that's pretty readable.

[...]

Somebody once said that there is no Obfuscated Perl Code Contest because
it would be pointless. He was wrong on both counts: Firstly, writing
"obfus" ist a favorite pastime of some perl programmers, and there are
contests (although AFAIK there is no global, annual contest like the
OCCC), and secondly, not all (or even most) Perl code is obfuscated. It
is perfectly possible to write sane, readable Perl code. But it is also
possible to write code which is even more obfuscated in Perl than in C
(for once, Perl code can modify itself, which (portable) C can't).

For a beautiful (and rather famous) example, of a Perl obfu, see
http://www.perlmonks.org/index.pl?node=camel code

hp

Re: every combination of Y/N in 5 positions

ITYM:
http://www.perlmonks.org/index.pl?node=camel%20code

Sinan

--
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc /

Re: every combination of Y/N in 5 positions

On Mon, 31 Mar 2008 23:23:25 +0200 in comp.lang.perl.misc, Gunnar

What is *_ ?  It looks like one of those magic perl variables,  but
I don't find any documentation on it.

Re: every combination of Y/N in 5 positions

David Harmon wrote:

It is a typeglob.  It means that you want all of the _ variables to have
a local value.  See the "Typeglobs and Filehandles" section of perldata.pod.

perldoc perldata

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

Re: every combination of Y/N in 5 positions

It is a typeglob of the variables named underscore.

^^^
^^^

It is many of those magic perl variables.

It is \$_ and @_ and _ and ...

See the "Typeglobs and Filehandles" section in perldata.pod.

--
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"

Re: every combination of Y/N in 5 positions

What noone has said yet is why Gunnar used it. Due to a rather nasty bug
in perl, under certain rather obscure circumstances[0] \$_ doesn't
localise properly, so if you need to do so it is safer to localise the
whole of *_. Unfortunately, besides being ugly, this means you lose your
sub arguments (and the magic stat filehandle, of course, but that's
likely less important); personally I would always rather use a for loop
over one element

for (sprintf '%05b', \$num) {

or, with 5.10, either 'given' (like for, but gives scalar context to its
argument) or 'my \$_'. None of these suffer from the bug.

Ben

[0] If \$_ is an alias to an element of a tied hash or array, the value
of that element will be localised along with \$_. A simple example is

use Tie::Hash;

tie my %h, 'Tie::StdHash';
\$h = 1;

for (\$h) {
local \$_ = 2;
print \$h;    # 2, but should be 1
}
print \$h;        # back to 1 again

This is only really important if you call external code in the scope of
the 'local': if that code reads the hash, it will be surprised to find
the values have changed.

Re: every combination of Y/N in 5 positions

_
David Harmon (source@netcom.com) wrote on VCCCXXVIII September MCMXCIII
[]  On Mon, 31 Mar 2008 23:23:25 +0200 in comp.lang.perl.misc, Gunnar
[] >I do, I hope. :)
[] >
[] >     foreach my \$num ( 0 .. 0b11111 ) {
[] >         local *_ = \ sprintf '%05b', \$num;
[]
[]  What is *_ ?  It looks like one of those magic perl variables,  but
[]  I don't find any documentation on it.

It's a silly attempt to obfuscate the code.

It's much better written as:

foreach (0 .. 0b11111) {
my \$_ = sprintf '%05b' => \$_;
tr _01_NY_;
say "\$::_:\$_";
}

No silly typeglobs needed.

Abigail
--
map=chr}map\$=+\$]..3*\$=/2;
print "\$J\$u\$s\$t \$a\$n\$o\$t\$h\$e\$r \$P\$e\$r\$l \$H\$a\$c\$k\$e\$r\n";

Re: every combination of Y/N in 5 positions

Abigail wrote:

Nice.

You forgot

use 5.10.0;

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de /
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel