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

10 little indians in Perl?

2 views
Skip to first unread message

Grant Robinson

unread,
Jul 23, 1993, 5:32:58 PM7/23/93
to
I never followed the thread in comp.lang.c long enough to see if anyone
worked up a solution in Perl.

By way of exercise, are there any cool tricks for getting it down below
68 chars?

for(;$i++<10;){print"\n$i little indian",$i%3?' ':'s';}print"boys";

--
Grant Robinson -- ClariNet Communications Corp, Sunnyvale, CA

Hal Pomeranz

unread,
Jul 23, 1993, 6:53:39 PM7/23/93
to
In article <1993Jul23....@clarinet.com> Grant Robinson writes:
>
> for(;$i++<10;){print"\n$i little indian",$i%3?' ':'s';}print"boys";
>

Well, if you're willing to produce incorrect output, you can make
the program arbitrarily small. Grant's version does

cockroach% perl -e 'for(;$i++<10;){print"\n$i little indian",$i%3?" ":"s";}print"boys";'

1 little indian
2 little indian
3 little indians
4 little indian
5 little indian
6 little indians
7 little indian
8 little indian
9 little indians
10 little indian boyscockroach%

There's an extra leading newline, and no trailing one, plus the words
are incorrect. *pew*

Here's the shortest one I could come up with:

for(;$i++<10;){print"$i little",$i%3?"":" indians",$i<10?"\n":" indian boys\n"}

*phew*, just under 80 chars!

--
==============================================================================
Hal Pomeranz pome...@imagen.com pome...@cs.swarthmore.edu
System/Network Manager "We are islands to each other, building
QMS Imagen Division hopeful bridges on a troubled sea." --Rush

Hal Pomeranz

unread,
Jul 23, 1993, 7:32:37 PM7/23/93
to

Hey, using printf() to save two characters:

for(1..10){printf"$_ little %s\n",$_%3?$_<10?"":"indian boys":"indians"}

Getting more than a little obfuscated now...

Hal Pomeranz

unread,
Jul 23, 1993, 7:16:57 PM7/23/93
to
In article <1993Jul23.2...@imagen.com> Hal Pomeranz writes:
>
> for(;$i++<10;){print"$i little",$i%3?"":" indians",\

> $i<10?"\n":" indian boys\n"}
>

Hate to follow up my own post, but you always think of these things
afterwards... *sigh*

for(1..10){print"$_ little",$_%3?"":" indians",$_<10?"\n":" indian boys\n"}

I should also note that this won't work before pl35 since I'm dropping
the trailing semi inside the block to save chars.

Much too much time on my hands...

Grant Robinson

unread,
Jul 23, 1993, 8:55:51 PM7/23/93
to
In article <1993Jul23.2...@imagen.com> pome...@imagen.com (Hal Pomeranz) writes:

>1 little indian
>2 little indian
>3 little indians
>4 little indian
>5 little indian
>6 little indians
>7 little indian
>8 little indian
>9 little indians
>10 little indian boyscockroach%
>
>There's an extra leading newline, and no trailing one, plus the words
>are incorrect. *pew*

Whoops.

I thought the problem seemed too simple to merit much attention! It did
solve the problem I though it was trying to solve (to which I plead memory
fault, the "little indians" song never was a big hit where I come from!)

>Here's the shortest one I could come up with:
>
>for(;$i++<10;){print"$i little",$i%3?"":" indians",$i<10?"\n":" indian boys\n"}
>
>*phew*, just under 80 chars!

Congrats.

Norman Richards

unread,
Jul 24, 1993, 1:03:54 AM7/24/93
to
In article <1993Jul23.2...@imagen.com> pome...@imagen.com (Hal Pomeranz) writes:
>Hey, using printf() to save two characters:
>for(1..10){printf"$_ little %s\n",$_%3?$_<10?"":"indian boys":"indians"}
>Getting more than a little obfuscated now...

reverse one of the ? tests and we chop off another one:
for(1..10){printf"$_ little %s\n",$_%3?$_>9?"indian boys":"":"indians"}


______________________________________________________________________________
o...@ccwf.cc.utexas.edu "Two roads diverged in a wood, and I -
I chose to climb the nearest tree.
And that has made all the difference."

Roland Schemers

unread,
Jul 24, 1993, 2:22:51 AM7/24/93
to
o...@bashful.cc.utexas.edu (Norman Richards) writes:
>reverse one of the ? tests and we chop off another one:
>for(1..10){printf"$_ little %s\n",$_%3?$_>9?"indian boys":"":"indians"}
>

two more:

for(1..10){printf"$_ little %s\n",$_%3?$_>9?"indian boys":"":indians}

Of course if Perl 5 has a function called indians then... :-)

Roland

--
Roland J. Schemers III | Networking Systems
Systems Programmer | 414 Sweet Hall +1 (415) 723-6740
Distributed Systems Operations | Stanford, CA 94305-3090
Stanford University | sche...@Slapshot.Stanford.EDU

Robert Withrow

unread,
Jul 24, 1993, 10:53:40 AM7/24/93
to
Uhh.. I thought it was ``1 little 2 little 3 little indians...10 little
indian boys''!!??

--
Robert Withrow, Tel: +1 617 598 4480, Fax: +1 617 598 4430, Net: wi...@rwwa.COM
R.W. Withrow Associates, 21 Railroad Ave, Swampscott MA 01907-1821 USA

Paul Fernquist

unread,
Jul 25, 1993, 3:13:00 AM7/25/93
to
In article <1993Jul24.0...@leland.stanford.edu> sche...@leland.Stanford.EDU (Roland Schemers) writes:
>o...@bashful.cc.utexas.edu (Norman Richards) writes:
>
>two more:

>
>for(1..10){printf"$_ little %s\n",$_%3?$_>9?"indian boys":"":indians}

one more:

for(1..10){printf"$_ little %s\n",$_%3?/0/?"indian boys":"":indians}

-----
Paul Fernquist pf...@technix.mn.org

--
Paul Fernquist
uunet: pf...@technix.mn.org
email: ...!uunet!cs.umn.edu!kksys!edgar!technix!pfern

Jim Barnett

unread,
Jul 25, 1993, 6:41:12 PM7/25/93
to

I may be from a different dialect, but isn't the verse different? I
thought it was: "One little, two little, three little indians, four
little, five little..." So the expression ought to be:

for(1..10){printf"$_ little%s\n",$_%3?/0/&&" indian boys":" indians"}

Of course, this may be compressible too.

- Jim

Michael Graff

unread,
Jul 25, 1993, 5:46:49 PM7/25/93
to
In <JSC.93Ju...@monolith.mit.edu> j...@monolith.mit.edu (Jin S Choi) writes:

> Jin> for(1..10){printf"$_ little indian%s\n",$_%3?/0/?" boys":"":"s"}

>And this can be shortened by two more characters by using a
>short-circuit && instead of ?: for a total of 62 characters.

>for(1..10){printf"$_ little indian%s\n",$_%3?/0/&&" boys":"s"}


Both of these are wrong though I believe...

1 little indian
2 little indian
3 little indians
4 little indian
5 little indian
6 little indians
7 little indian
8 little indian
9 little indians
10 little indian boys

It should be:

1 little
2 little
3 little indians

4 little
5 little

6 little indians
7 little

8 little

9 little indians
10 little indian boys

like Paul (and other's) have coded.

--Michael
--
Michael Graff <expl...@iastate.edu> Speaking for myself, not
Project Vincent Voice: (515)294-4994 for ISU or the ISUCC
Iowa State Univ Comp Center Fax: (515)294-1717
Ames, IA 50011 -=*> PGP key on pgp-pub...@pgp.iastate.edu <*=-

Jin S Choi

unread,
Jul 25, 1993, 6:50:57 PM7/25/93
to
>>>>> On Sun, 25 Jul 1993 21:46:49 GMT, expl...@iastate.edu (Michael Graff) said:

Michael> In <JSC.93Ju...@monolith.mit.edu> j...@monolith.mit.edu (Jin S Choi) writes:

> Jin> for(1..10){printf"$_ little indian%s\n",$_%3?/0/?" boys":"":"s"}

>And this can be shortened by two more characters by using a
>short-circuit && instead of ?: for a total of 62 characters.

>for(1..10){printf"$_ little indian%s\n",$_%3?/0/&&" boys":"s"}


Michael> Both of these are wrong though I believe...

My goof. I realized this just after I posted it, and issued a cancel,
but it seems not to have gotten out in time. How embarassing.

--
Jin Choi
j...@athena.mit.edu

silver Harloe

unread,
Jul 26, 1993, 1:01:14 PM7/26/93
to
In article <BARNETT.93...@neutron.mcc.com> bar...@neutron.mcc.com
(Jim Barnett) writes:
> for(1..10){printf"$_ little%s\n",$_%3?/0/&&" indian boys":" indians"}
> Of course, this may be compressible too.

Well, I could only squeeze two more characters out of this solution, down
to 66:

for(1..10){printf"$_ little %s\n",$_%3?/0/&&"indian boys":indians}

or

for(1..10){print"$_ little ",$_%3?/0/&&"indian boys":indians,"\n"}

--silver--
sil...@metonymy.ots.utexas.edu

Tim Cook

unread,
Jul 27, 1993, 12:21:04 AM7/27/93
to
bar...@neutron.mcc.com (Jim Barnett) writes:
>
> I may be from a different dialect, but isn't the verse different? I
> thought it was: "One little, two little, three little indians, four
> little, five little..."

That's what I thought.

> So the expression ought to be:
>
> for(1..10){printf"$_ little%s\n",$_%3?/0/&&" indian boys":" indians"}

How about:

for(1..10){print"$_ little ",$_%3?/0/&&"indian boys\n":"indians\n"}

This gives a format that denotes the rhythym better, and includes a
further optimisation.

Can anybody beat 67?

--
for(1..10){print"$_ little ",$_%3?/0/&&"Perl hackers \n":"hackers\n"}

William M. Eldridge

unread,
Jul 27, 1993, 1:20:21 PM7/27/93
to
And at the second coming, earthlings basking in the warm
glory, lovingly awaiting the idyllic future, picked up in
rapturous delight, the overlord proclaims:

"You know this was going to be my last time, but you idiots
were working on the wrong problem. Now get back down there
and do something real."
--
Bill Eldridge bi...@cognet.ucla.edu 310-206-3960 (3987 fax)
More! Better! Faster! (80's motto) x x x x
Hot, Toxic, and Shallow! (90's motto) x xx x x
Free Rorschach Sig! (interpretation extra) x xx xx x

William M. Eldridge

unread,
Aug 1, 1993, 11:17:04 PM8/1/93
to
ke...@cruise.cc.rochester.edu (Kenneth C. Rich) writes:

>this ain't the shortest but hey!

>for(1..10){s/$/ little /;/[3690]/&&s/ $/ whiteys,\n/;/0/&&s/s,/ boys. ;)/;print}


The first time I tried running this, it bombed out, but
the I changed the flags to:

#!/usr/bin/perl -pc

and it ran perfectly. Even too perfectly. Kind of eery.

Bill

0 new messages