Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

YN golf

3 views
Skip to first unread message

Uri Guttman

unread,
Mar 31, 2008, 6:50:24 PM3/31/08
to Fun with Perl

someone posted (and it wasn't homework) for an easy way to get all
possible combos of YN (5 chars so 32 answers). he got some basic
multiline answers but i think it makes for a great golf problem.

here is my first pass which i am sure can be easily bested. i haven't
even squeezed out the white spaces but it does work.

golf away!

uri

perl -le 'print join "\n", map {tr/01/NY/; $_} map unpack( "b5", chr), 0 .. 31'

--
Uri Guttman ------ u...@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 ---------

Rick Klement

unread,
Mar 31, 2008, 7:34:58 PM3/31/08
to Fun With Perl

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

--
Rick Klement

Philippe Bruhat

unread,
Mar 31, 2008, 7:54:40 PM3/31/08
to Fun With Perl
On Mon, Mar 31, 2008 at 04:34:58PM -0700, Rick Klement wrote:
>
> perl -le 'print for glob"{Y,N}"x5'
>

Of course you have to run this in a directory that doesn't contain
any file matching /^[YN]{5}$/.

--
Philippe Bruhat (BooK)

The learned man makes a mistake but once... but the truly stupid keep
practicing until they get it right.
(Moral from Groo The Wanderer #75 (Epic))

Zed Lopez

unread,
Mar 31, 2008, 7:22:08 PM3/31/08
to Fun with Perl
On Mon, Mar 31, 2008 at 05:50:24PM -0500, Uri Guttman wrote:
> perl -le 'print join "\n", map {tr/01/NY/; $_} map unpack( "b5", chr), 0 .. 31'

Also, I'm sure, easily bested:

perl -e 'for(0..31){$_=sprintf"%05b\n",$_;y/01/YN/;print}

marginally shorter not-to-hoyle hybrid:
seq 0 31|perl -pe '$_=sprintf"%05b\n",$_;y/01/YN/'

--
zed at-sign apricot dot com http://www.MemeMachineGo.com/
Zed Lopez PO Box 12546 Berkeley CA 94712

Josh Goldberg

unread,
Mar 31, 2008, 7:12:12 PM3/31/08
to Uri Guttman, Fun with Perl
Knocking a few chars off yours for perl -le 'print
join$/,map{y/01/NY/;$_}map unpack("b5",chr),0..31'

and shrunk a few more by removing the join and using sprintf:

perl -le 'print map{y/10 /YN/;$_}map{sprintf"%5b$/",$_}0..31'

Chris Dolan

unread,
Apr 1, 2008, 12:37:05 AM4/1/08
to Philippe Bruhat, Fun With Perl
On Mar 31, 2008, at 6:54 PM, Philippe Bruhat (BooK) wrote:
> On Mon, Mar 31, 2008 at 04:34:58PM -0700, Rick Klement wrote:
>>
>> perl -le 'print for glob"{Y,N}"x5'
>>
>
> Of course you have to run this in a directory that doesn't contain
> any file matching /^[YN]{5}$/.

Not true. The {} notation doesn't care whether files of that name
actual exist. I tested like so on Mac:

% touch YYYYY
% perl -le 'print for glob"{Y,N}"x5'
YYYYY
YYYYN
YYYNY
YYYNN
YYNYY
YYNYN
YYNNY
YYNNN
YNYYY
YNYYN
YNYNY
YNYNN
YNNYY
YNNYN
YNNNY
YNNNN
NYYYY
NYYYN
NYYNY
NYYNN
NYNYY
NYNYN
NYNNY
NYNNN
NNYYY
NNYYN
NNYNY
NNYNN
NNNYY
NNNYN
NNNNY
NNNNN

Philippe Bruhat

unread,
Apr 1, 2008, 3:10:27 AM4/1/08
to Fun With Perl
On Mon, Mar 31, 2008 at 11:37:05PM -0500, Chris Dolan wrote:
> On Mar 31, 2008, at 6:54 PM, Philippe Bruhat (BooK) wrote:
>> On Mon, Mar 31, 2008 at 04:34:58PM -0700, Rick Klement wrote:
>>>
>>> perl -le 'print for glob"{Y,N}"x5'
>>>
>>
>> Of course you have to run this in a directory that doesn't contain
>> any file matching /^[YN]{5}$/.
>
> Not true. The {} notation doesn't care whether files of that name
> actual exist. I tested like so on Mac:

I was pretty sure I tried the same manipulation to obtain all permutations
of some series of strings, and that it didn't work as expected when one of
the permutations actually existed as a file in the current directory.

After a quick check, you are right and I was too. :-)
What I tried was:

$ touch YYYYY
$ perl -le 'print for glob"[YN]"x5'
YYYYY

I computed my permutations with square brackets (which is one character
shorter, but more fragile).

--
Philippe Bruhat (BooK)

To flaunt your strength is to make it your weakness.
(Moral from Groo The Wanderer #25 (Epic))

John W. Krahn

unread,
Apr 1, 2008, 2:17:07 AM4/1/08
to f...@perl.org
Uri Guttman wrote:
> someone posted (and it wasn't homework) for an easy way to get all
> possible combos of YN (5 chars so 32 answers). he got some basic
> multiline answers but i think it makes for a great golf problem.
>
> here is my first pass which i am sure can be easily bested. i haven't
> even squeezed out the white spaces but it does work.
>
> golf away!
>
> uri
>
> perl -le 'print join "\n", map {tr/01/NY/; $_} map unpack( "b5", chr), 0 .. 31'

perl -le'y/01/NY/&&print,for+map+unpack(b5,chr),0..31'

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

Greg Allen

unread,
Apr 1, 2008, 6:37:02 AM4/1/08
to John W. Krahn, f...@perl.org

Magoo!

perl -le'y/01/NY/&&print,for+map+unpack(b5,chr),0..31'

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

This message is intended only for the personal and confidential use of the designated recipient(s) named above. If you are not the intended recipient of this message you are hereby notified that any review, dissemination, distribution or copying of this message is strictly prohibited. This communication is for information purposes only and should not be regarded as an offer to sell or as a solicitation of an offer to buy any financial product, an official confirmation of any transaction, or as an official statement of Lehman Brothers. Email transmission cannot be guaranteed to be secure or error-free. Therefore, we do not represent that this information is complete or accurate and it should not be relied upon as such. All information is subject to change without notice.

Shmem

unread,
Apr 11, 2008, 5:25:54 AM4/11/08
to Uri Guttman, Fun with Perl
From the keyboard of Uri Guttman [31.03.08,17:50]:

>
> someone posted (and it wasn't homework) for an easy way to get all
> possible combos of YN (5 chars so 32 answers). he got some basic
> multiline answers but i think it makes for a great golf problem.
>
> here is my first pass which i am sure can be easily bested. i haven't
> even squeezed out the white spaces but it does work.
>
> golf away!
>
> uri
>
> perl -le 'print join "\n", map {tr/01/NY/; $_} map unpack( "b5", chr), 0 .. 31'

from mtve:

perl -le 'y/12/NY/-5||print for 0..2x5'

(can't do any better...)

0--gg-

--
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

0 new messages