3
\$\begingroup\$

Assuming 'lol' with a variavle number of 'o's. Print a the following phrase:

What does 1st, 2nd, 3rd, 4th ... 'o' stand for?

Examples:

lol -> What does 1st 'o' stand for?
loooool -> What does 1st, 2nd, 3rd, 4th, 5th 'o' stand for?

So far I managed to write a 128 characters solution in Perl:

$l=(<>=~tr/o//);print"What does ";print$_,$_==1?'st':$_==2?'nd':$_==3?'rd':'th',$_<$l?', ':''for(1..$l);print " 'o' stand for?";

and a 123 characters solution in Haskell:

p 1="1st";p 2="2nd";p 3="3rd";p n=show n++"th";main=putStr$"What does "++(foldr(++)"'o' stand for?"[p i++", "|i<-[1..23]]);

How short can you write it?

Johannes Kuhn
7,7685 gold badges33 silver badges45 bronze badges
asked Apr 4, 2013 at 2:36
\$\endgroup\$
3
  • \$\begingroup\$ Does it work for 'll'? I assume not \$\endgroup\$ Commented Apr 4, 2013 at 3:13
  • \$\begingroup\$ You can assume number of 'o' to be >0. \$\endgroup\$ Commented Apr 4, 2013 at 3:14
  • \$\begingroup\$ Yes. But that increases difficulty, somehow... \$\endgroup\$ Commented Apr 4, 2013 at 12:37

4 Answers 4

4
\$\begingroup\$

GolfScript, 82 characters

"What does ",2円-,{)..10/10%1=!10円%`"123"\?)*"thstndrd"2/=+}%", "*" 'o' stand for?"

Works even for >100 'o's, test it online.

answered Apr 4, 2013 at 8:00
\$\endgroup\$
2
\$\begingroup\$

Python, 112

print'What does '+', '.join(`n+1`+['th','snrtdd'[n::3]][n<3]for n in range(len(raw_input())-2)),"'o' stand for?"
answered Apr 4, 2013 at 3:10
\$\endgroup\$
2
  • \$\begingroup\$ very nice slicing trick... \$\endgroup\$ Commented Apr 4, 2013 at 6:22
  • 1
    \$\begingroup\$ Unfortunately does not work for number of 'o's greater than 20. \$\endgroup\$ Commented Apr 4, 2013 at 8:05
2
\$\begingroup\$

Perl 104 (+1) bytes

s/l?ol?/($`?', ':"What does the ").++$n.((0,st,nd,rd)[$n%(10<<($n%100<20))]||th)/ge;s/$/ 'o' stand for?/

Correctly handles 11th, 12th, 13th, 21st, 22nd, 23rd, 111th, 112th, 113th etc. Takes input from stdin, and requires a -p command line switch:

$ echo loooool| perl -p loool.pl
What does the 1st, 2nd, 3rd, 4th, 5th 'o' stand for?
$ echo looooooooooooooooooooooooooooooooooooooooool| perl -p loool.pl
What does the 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th,
15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th, 26th, 27th, 28th, 29th,
30th, 31st, 32nd, 33rd, 34th, 35th, 36th, 37th, 38th, 39th, 40th, 41st, 42nd 'o' stand for?

If it is not required that 21st, 22nd, 23rd be reported correctly (as in the example code), this can be reduced to 87 bytes:

s/l?ol?/($`?', ':"What does the ").++$n.((0,st,nd,rd)[$n]||th)/ge;s/$/ 'o' stand for?/

PHP 107 bytes

What does the <?for(;$argv[1][++$n]>l;$f=', ')echo$f,$n,date(S,~-$n%(10<<($n%100<20))*9e4)?> 'o' stand for?

Input is taken by command line argument:

$ php loool.php loooool
What does the 1st, 2nd, 3rd, 4th, 5th 'o' stand for?
$ php loool.php looooooooooooooooooooooooooooooooooooooooool
What does the 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th,
15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th, 26th, 27th, 28th, 29th,
30th, 31st, 32nd, 33rd, 34th, 35th, 36th, 37th, 38th, 39th, 40th, 41st, 42nd 'o' stand for?

Once again, if correctly handling n>20 is not required, this can be reduced to 87 bytes:

What does the <?for(;$argv[1][++$n]>l;$f=', ')echo$f,date(jS,~-$n*9e4)?> 'o' stand for?
answered Apr 4, 2013 at 4:11
\$\endgroup\$
1
\$\begingroup\$

R - 158 characters

f=function(s){n=1:(nchar(s)-2);v=c("st","nd","rd",rep("th",7));cat("What does the",paste0(n,c(v,rep("th",10),rep(v,8))[n],collapse=", "),"\"o\" stand for?")}

Works until 100 "o"s.

Usage:

f("looooooooooooooooooooooooooooooooool")
What does the 1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th, 20th, 21st, 22nd, 23rd, 24th, 25th, 26th, 27th, 28th, 29th, 30th, 31st, 32nd, 33rd, 34th "o" stands for?
answered Apr 4, 2013 at 7:14
\$\endgroup\$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.