Tell me more ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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.

share|improve this question
5  
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. – leftaroundabout Feb 25 '12 at 21:31
@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! :) – rynah Feb 25 '12 at 21:33
Also, you should perhaps specify whether answers like this will be accepted. – leftaroundabout Feb 25 '12 at 21:43
@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. – rynah Feb 25 '12 at 21:45
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? – Peter Taylor Feb 26 '12 at 8:46
show 3 more comments

4 Answers

up vote 3 down vote accepted

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
share|improve this answer
Sorry, how do I run it? echo "test" > sed -f GordonBailey.sed doesn't output anything. – rynah Feb 25 '12 at 22:18
woops, added a sample invocation – Gordon Bailey Feb 25 '12 at 22:26
2  
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 – Gordon Bailey Feb 25 '12 at 23:33
4  
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? – dmckee Feb 26 '12 at 2:22
1  
@dmckee: Isn't unix that thing that webservers use? (No, this is not serious.) – rynah Feb 26 '12 at 14:44
show 9 more comments

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...

share|improve this answer
Nice. I'll be curious to see the final stats with a longer input list, this may well be the best approach. – Gordon Bailey Feb 27 '12 at 23:51
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).

share|improve this answer
Nicely done. If it does return 403s, though... (automated testing, so typing speed is not an issue.) – rynah Feb 27 '12 at 20:34
perl -mLingua::EN::Inflect=PL -pe's/.*/PL$&/e'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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