3
\$\begingroup\$

This question already has an answer here:

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?

\$\endgroup\$

marked as duplicate by Peter Taylor, grc, primo, ugoren, manatwork Apr 4 '13 at 14:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ Does it work for 'll'? I assume not \$\endgroup\$ – jamylak Apr 4 '13 at 3:13
  • \$\begingroup\$ You can assume number of 'o' to be >0. \$\endgroup\$ – Maciej Goszczycki Apr 4 '13 at 3:14
  • \$\begingroup\$ Yes. But that increases difficulty, somehow... \$\endgroup\$ – Maciej Goszczycki Apr 4 '13 at 12:37
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.

\$\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?"
\$\endgroup\$
  • \$\begingroup\$ very nice slicing trick... \$\endgroup\$ – boothby Apr 4 '13 at 6:22
  • 1
    \$\begingroup\$ Unfortunately does not work for number of 'o's greater than 20. \$\endgroup\$ – Howard Apr 4 '13 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?
\$\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?
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.