Skip to content

Latest commit

 

History

History
45 lines (32 loc) · 1.7 KB

introduction.md

File metadata and controls

45 lines (32 loc) · 1.7 KB

Introduction

There are various ways to solve Pig Latin. One way is to use regular expressions (also known as regex) for processing the input. Solutions using regex can be very succinct, but require familiarity with regex patterns, which are like another language. Another way is to use a series of conditional statements to test which of several rules the input matches. Another approach is to use sets for look-up and then slice the input to return the correct value.

General guidance

At the time of writing only four rules need to be handled, but if they have similar output, they don't need to be handled completely separately.

Approach: Sets and slices

VOWELS = {"a", "e", "i", "o", "u"}
VOWELS_Y = {"a", "e", "i", "o", "u", "y"}
SPECIALS = {"xr", "yt"}


def translate(text):
    piggyfied = []

    for word in text.split():
        if word[0] in VOWELS or word[0:2] in SPECIALS:
            piggyfied.append(word + "ay")
            continue

        for pos in range(1, len(word)):
            if word[pos] in VOWELS_Y:
                pos += 1 if word[pos] == 'u' and word[pos - 1] == "q" else 0
                piggyfied.append(word[pos:] + word[:pos] + "ay")
                break

    return " ".join(piggyfied)

For more information, check the sets and slices approach.