1
\$\begingroup\$

The challenge:

Write a program or function in any language that takes as an argument (command-line or function) a single English word. It returns or outputs the pluralized form of the word.

(To clarify, the program or function will be executed many times on different words, but separately - the list of words won't be passed all at once.)

The scoring:

The score of a program or function is -(characters in program + 1) * number of wrong answers. Highest score wins. Joke answers will likely lose by a huge margin (I'm planning on putting in maybe 1,000 words) and if you're amazing you get the highest score of 0.

Example test data:

woman
turn
fly
court
finger
blitz
pie
injury
vertex
alleyway
alloy
ox
goose
mouse
house
louse
waffle
right
bacterium
virus
cherub
chaetognath
boy
man
shaman
figure
prefix
etc.

Those are just some that I picked off the top of my head. The real test cases will have even stranger plurals, but the distribution between types of suffixes will be more even.

Enjoy!

Additional Rule

No Mathematica. Sorry.

\$\endgroup\$
  • 9
    \$\begingroup\$ The problem with your proposed scoring is that a program which gets everything right can be arbitrarily big, it will always have score 0. That's hardly code-golf-ish, you'd simply need to include an entire dictionary! I'd suggest (characters_in_program + 1) * (wrong_answers + 1) instead. \$\endgroup\$ – ceased to turn counterclockwis Feb 25 '12 at 21:31
  • \$\begingroup\$ @leftaroundabout: I'll throw in a plural that people are unlikely to have ever heard of. If it includes that, well... it deserves the highest score! :) \$\endgroup\$ – Ry- Feb 25 '12 at 21:33
  • \$\begingroup\$ Also, you should perhaps specify whether answers like this will be accepted. \$\endgroup\$ – ceased to turn counterclockwis Feb 25 '12 at 21:43
  • \$\begingroup\$ @leftaroundabout: Ah, yes. I was meaning to add that. And the answer is NO! But for the other possible "loopholes" and rule-bending, they're still allowed. \$\endgroup\$ – Ry- Feb 25 '12 at 21:45
  • \$\begingroup\$ By the way, something which hadn't occurred to me earlier: how does your test script handle cases where there's more than one correct answer? \$\endgroup\$ – Peter Taylor Feb 26 '12 at 8:46
3
\$\begingroup\$

sed - 155 (not including #! line)

#!/bin/sed -Ef
s/man$/men!/
s/([aeiou](x|z))$/\1es!/
s/ium$/ia!/
s/([^!aeiou])y$/\1ies!/
s/((r|gh|l|s)[^!aeiou])$/\1s!/
s/([^!aeiou]{2}|.s)$/\1es!/
s/([^!])$/\1s/
s/!$//

This will read from stdin, and I think it will do an okay job for most input. The basic idea is that it tries each regex line by line. Whenever it matches it makes a replacement that appends ! to make sure that no other regexes will match, then it drops the ! on the last line.

I tried to deal with most special cases, but it will definitely fail on some (goose and mouse for example)

Sample invocation:

chmod a+x GordonBailey.sed
echo $word | ./GordonBailey.sed
\$\endgroup\$
  • \$\begingroup\$ Sorry, how do I run it? echo "test" > sed -f GordonBailey.sed doesn't output anything. \$\endgroup\$ – Ry- Feb 25 '12 at 22:18
  • \$\begingroup\$ woops, added a sample invocation \$\endgroup\$ – Gordon Bailey Feb 25 '12 at 22:26
  • 3
    \$\begingroup\$ Really? If it makes it easier, since it's sed, it will read from a file line by line, so you could put all the words into a file and run it on the whole file: cat words.txt | ./GordonBailey.sed > output.txt \$\endgroup\$ – Gordon Bailey Feb 25 '12 at 23:33
  • 6
    \$\begingroup\$ I don't know @minitech...I want to respect you, but I'm beginning to think you're not a unix user. ::gasps and shocked silence from the audience:: Whatever is the world coming to? \$\endgroup\$ – dmckee Feb 26 '12 at 2:22
  • 1
    \$\begingroup\$ @dmckee: Isn't unix that thing that webservers use? (No, this is not serious.) \$\endgroup\$ – Ry- Feb 26 '12 at 14:44
5
\$\begingroup\$

sed, 6 chars

Someone should give this a try...

s/$/s/

Invoked with sed -f myscript.sed < words.txt > output.txt
I can afford 22 times more mistakes, compared to Gordon Bailey's solution, and still win.

With the sample input I do win, because he misses "mice" and "lice" (and maybe more, there are some I need to look up), and this sample is too short for me to have 44 mistakes...

\$\endgroup\$
  • \$\begingroup\$ Nice. I'll be curious to see the final stats with a longer input list, this may well be the best approach. \$\endgroup\$ – Gordon Bailey Feb 27 '12 at 23:51
4
\$\begingroup\$
require 'open-uri'

def p(w)
  open("http://en.wiktionary.org/wiki/#{w}"){|f| f.read}.match(/plural-form-of.*?title=\"(.*?)\"/)[1]
end

ruby at 135. It got all of the sample input (though it will eventually begin returning 403s if you type quickly enough).

\$\endgroup\$
  • \$\begingroup\$ Nicely done. If it does return 403s, though... (automated testing, so typing speed is not an issue.) \$\endgroup\$ – Ry- Feb 27 '12 at 20:34
0
\$\begingroup\$
perl -mLingua::EN::Inflect=PL -pe's/.*/PL$&/e'
\$\endgroup\$
0
\$\begingroup\$

Extended BrainFuck: 9

,[.,]||s|

Compiling:

beef ebf.bf < plural.ebf > plural.bf

Turns into the following BrainFuck code: (39)

,[.,]>++++++++++[-<+++++++++++>]<+++++.

Sample invocation:

$ echo -n test | beef plural.bf

Any BrainFuck interpreter can be used instead of beef. It takes one word in stdin and ends it with EOF. Alternatively you can use echo test | bf -n plural.bf where in this particular interpreter-n assumes EOF on first linefeed.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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