Interesting Pig Latin Words

Pig Latin is a word game where words are modified according to some rules:

  1. If the original word has no vowel, the Pig Latin is just the same word.
  2. If the original word begins with a vowel, "ay" is suffixed to the word. E.g., "eat" becomes "eatay".
  3. Otherwise, the consonant cluster before the first vowel is moved to the end of the word, and "ay" is added. E.g., "smile" becomes "ilesmay".

Now, call a word interesting if its Pig Latin form is still an English word; for example, the Pig Latin of "trash" is "ashtray". It is easy to see that words with no vowels are trivially interesting. We do not want to take such words into account.

Here is a list of about 45 thousand words. Assuming that these are all the valid English words, find the number of non-trivial , interesting words.


The answer is 8.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

1 solution

Bryan Bischof
Apr 22, 2018

A simple function to check this property is easy to write:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
def piggy(word):
    try:
        return "".join(
            sorted(
                [
                    (word[:i], word[i:]) for i in [
                        word.find(v) for v in vowels
                    ] if i != -1
                ], key=lambda tup: len(tup[0])
            )[0][::-1]
        )+'ay'
    except:
        return word

Which can be improved my making it a generator, or using a regex potentially. The real challenge is that you need to check if the output is also a word. Since the lists are so long, a worthwhile effort is to optimize this part of the problem. The traditional approach is to use a Trie.

Thus, encode the list of words in a trie, and then just loop over keys in the trie with the above function, and test if they're in the trie, and not trivial.

As with many of these kinds of problems, this depends a lot on the dictionary. I like a larger dictionary, so in addition to the svelte one provided, I ran the program on a bigger dictionary. The result is more fun: [('ab', 'abay'), ('abac', 'abacay'), ('abr', 'abray'), ('ad', 'aday'), ('al', 'alay'), ('ald', 'alday'), ('all', 'allay'), ('alw', 'alway'), ('am', 'amay'), ('amb', 'ambay'), ('amir', 'amiray'), ('an', 'anay'), ('ap', 'apay'), ('app', 'appay'), ('arr', 'array'), ('ass', 'assay'), ('ast', 'astay'), ('astr', 'astray'), ('aw', 'away'), ('ba', 'abay'), ('bam', 'ambay'), ('bes', 'esbay'), ('bra', 'abray'), ('bun', 'unbay'), ('caba', 'abacay'), ('da', 'aday'), ('dal', 'alday'), ('enl', 'enlay'), ('ess', 'essay'), ('fo', 'ofay'), ('fra', 'afray'), ('isl', 'islay'), ('ko', 'okay'), ('la', 'alay'), ('lad', 'adlay'), ('lavant', 'avantlay'), ('len', 'enlay'), ('lin', 'inlay'), ('linter', 'interlay'), ('lis', 'islay'), ('lout', 'outlay'), ('lover', 'overlay'), ('ma', 'amay'), ('na', 'anay'), ('oath', 'oathay'), ('of', 'ofay'), ('ok', 'okay'), ('outr', 'outray'), ('outs', 'outsay'), ('overs', 'oversay'), ('pa', 'apay'), ('pap', 'appay'), ('plout', 'outplay'), ('plover', 'overplay'), ('plu', 'uplay'), ('plunder', 'underplay'), ('pout', 'outpay'), ('ppa', 'appay'), ('prover', 'overpray'), ('pun', 'unpay'), ('rab', 'abray'), ('raff', 'affray'), ('rami', 'amiray'), ('ren', 'enray'), ('rest', 'estray'), ('riv', 'ivray'), ('rout', 'outray'), ('run', 'unray'), ('sta', 'astay'), ('stout', 'outstay'), ('stover', 'overstay'), ('stra', 'astray'), ('stre', 'estray'), ('sun', 'unsay'), ('sunder', 'undersay'), ('swa', 'asway'), ('tas', 'astay'), ('touts', 'outstay'), ('trash', 'ashtray'), ('tres', 'estray'), ('tups', 'upstay'), ('unb', 'unbay'), ('unl', 'unlay'), ('unp', 'unpay'), ('uns', 'unsay'), ('wa', 'away'), ('wair', 'airway'), ('wany', 'anyway'), ('warch', 'archway'), ('was', 'asway'), ('wedge', 'edgeway'), ('wup', 'upway')]

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...