Sign up ×
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.

Write a program which accepts a sentence into stdin, and writes that sentence to stdout in pig latin.

Here are some test cases which your program should handle:

    Read your script
    ead-Ray our-yay ipt-scray

Hyphenation is mandatory. Note that multiple consonants at the start of the word must all be moved to the end. Capitalization of the output does not matter; Ead-ray or ead-ray are also valid translations of Read

    An utterance is allowed an opening vowel
    An-way utterance-way is-way allowed-way an-way opening-way owel-vay

Words starting with vowels have -way appended in their pig latin form (don't forget to handle capital opening vowels)


Words consisting entirely of consonants should have a hyphen prepended, and ay appended

Punctuation is undefined; I don't care if leading or trailing punctuation ends up inside the pig latin word.

This is code golf; the shortest program by character count wins. I've written a solution myself which handles all these cases which I shall post as a response.

share|improve this question
And what if there is a language that has a built-in piglatinify function? The Odegolfpay language springs to mind. You should add a clause that penalises the use of libraries like that. – Mr Lister Oct 24 '12 at 21:18
@MrLister I think it's better to not add a bunch of little restrictions to the puzzle unless it's actually a problem. Besides, an answer in such a language really isn't very interesting and I doubt it would get many upvotes. – Peter Olson Oct 31 '12 at 2:20
@PeterOlson O-Kay. – Mr Lister Oct 31 '12 at 7:08

9 Answers 9

up vote 3 down vote accepted

Perl (53)

s/(.*?)([aeiou].*)/$1||=w;print"$2-$1ay "/iefor split
share|improve this answer

Python, 89

import re
print re.sub('-a','-wa',re.sub('(?i)([^aeiou ]*)(\w*)',r'\2-\1ay',raw_input()))
share|improve this answer

Perl (60)

Use perl -n. The -n switch is counted (as two characters).

s/(.*?)([aeiou].*)/print"$2-$1"."w"x!$1."ay "/iefor split
share|improve this answer

Perl, 41 excluding switches

A simple modification of Timwi's answer. Runs with perl -p. Does not meet the spec for 'sky', but the original doesn't either.

share|improve this answer

K, 95

" "/:{$[&/~x in v:v,_v:"AEIOU";"-",x,"ay";("-"/:|_[;x]0,*&t),$[*t:x in v;"way";"ay"]]}'" "\:0:0
share|improve this answer

Python - 122 85 characters

import re
print re.sub("-a","-wa",re.sub("([^aeiou ]*)(\w*)",r"\2-\1ay",raw_input()))

122 -> 85: Improved use of regexes; running regex on whole sentence rather than individual words, and doing all the processing with regexes rather than a conditional expression. This ended up making it pretty similar to grc's answer

share|improve this answer
I would switch to Python 3 and shave off 2 characters :) – nneonneo Oct 25 '12 at 17:19

Cojure - 376 characters

Obviously not winning this one, but figured I'd take a shot at it.


(defn n [a b] (reduce #(and % %2) (map #(= (.indexOf a %) -1) (map string/lower-case (rest b)))))
(defn t [a] (let [vowels ["a" "e" "i" "o" "u"] x "ay"] (if (> (.indexOf vowels (string/lower-case (first a))) -1) (str a "-way") (if (n vowels a) (str a x) (str (reduce str (rest a)) "-" (first a) x)))))
(defn ay-ya [a] (reduce str (map #(str (t %) " ") (string/split a #" "))))


(defn no-vowel [a b]
    (reduce #(and % %2) (map #(= (.indexOf a %) -1) (map string/lower-case (rest b)))))

(defn transform [text]
  (let [vowels ["a" "e" "i" "o" "u"]]
    (if (> (.indexOf vowels (string/lower-case (first text))) -1)
        (str text "-way")
        (if (no-vowel vowels text)
          (str text "ay")
          (str (reduce str (rest text)) "-" (first text) "ay")))))

(defn ay-ya [text]
    (reduce str
        (map #(str (transform %) " ")  
             (clojure.string/split text #" "))))             
share|improve this answer

Clojure - 113 chars

(clojure.string/replace(read-line)#"([^aeiouAEIOU ]*)(\w*)"(fn[[a c r]](if(=""a)""(str r"-"(if(=""c)\w c)"ay"))))
share|improve this answer

Ruby 1.9, 68

puts{|w|w[/^([^aeiou]+)(.*)/i]?$2+"-"+$1+"ay":w+"-way"}*" "
share|improve this answer
I counted 68 characters ... – air_blob Nov 20 '12 at 8:47

Your Answer


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.