every combination of Y/N in 5 positions
  Home FAQ Contact Sign in
comp.lang.perl.misc only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.perl.misc Profile…
 Up
every combination of Y/N in 5 positions         


Author: joemacbusiness
Date: Mar 31, 2008 13:57

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);
Show full article (1.29Kb)
33 Comments
Re: every combination of Y/N in 5 positions         


Author: smallpond
Date: Mar 31, 2008 14:06

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


Author: Sandy
Date: Mar 31, 2008 14:21

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


Author: Gunnar Hjalmarsson
Date: Mar 31, 2008 14:23

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. :)
Show full article (0.64Kb)
no comments
Re: every combination of Y/N in 5 positions         


Author: Jürgen Exner
Date: Mar 31, 2008 14:36

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


Author: joemacbusiness
Date: Mar 31, 2008 15:21

On Mar 31, 2:36 pm, Jürgen Exner hotmail.com> wrote:
> 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) {
>     $_ = sprintf "%%05b", $_;
>     s/0/N/g;
>     s/1/Y/g;
>     print "$_\n";
>
>
>
> }- Hide quoted text - ...
Show full article (1.23Kb)
no comments
Re: every combination of Y/N in 5 positions         


Author: Uri Guttman
Date: Mar 31, 2008 15:28

>>>>> "GH" == Gunnar Hjalmarsson writes:
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. :)
Show full article (1.11Kb)
no comments
Re: every combination of Y/N in 5 positions         


Author: Gunnar Hjalmarsson
Date: Apr 1, 2008 01:34

Uri Guttman wrote:
>>>>>> "GH" == Gunnar Hjalmarsson writes:
>
>> 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. :)
> ...
Show full article (1.08Kb)
no comments
Re: every combination of Y/N in 5 positions         


Author: Mirco Wahab
Date: Apr 1, 2008 04:17

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


Author: John W. Krahn
Date: Apr 1, 2008 05:30

Mirco Wahab wrote:
> 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 ;-)

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
no comments
 
1 2 3 4