---
id: 6a2037a68a0bc2aef0006002
title: "Challenge 340: Pig Latin Converter"
challengeType: 29
dashedName: challenge-340
---

# --description--

Given a string, convert it to Pig Latin using the following rules:

- If a word begins with a vowel (`"a"`, `"e"`, `"i"`, `"o"`, or `"u"`), add `"way"` to the end. For example, `"universe"` converts to `"universeway"`.
- If a word begins with one or more consonants, move them to the end and add `"ay"`. For example, `"hello"` converts to `"ellohay"`.
- Preserve the case of the first letter. For example, `"Hello"` converts to `"Ellohay"`.

# --hints--

`pig_latin("universe")` should return `"universeway"`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(pig_latin("universe"), "universeway")`)
}})
```

`pig_latin("hello")` should return `"ellohay"`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(pig_latin("hello"), "ellohay")`)
}})
```

`pig_latin("hello universe")` should return `"ellohay universeway"`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(pig_latin("hello universe"), "ellohay universeway")`)
}})
```

`pig_latin("Hello universe")` should return `"Ellohay universeway"`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(pig_latin("Hello universe"), "Ellohay universeway")`)
}})
```

`pig_latin("Pig Latin is fun")` should return `"Igpay Atinlay isway unfay"`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(pig_latin("Pig Latin is fun"), "Igpay Atinlay isway unfay")`)
}})
```

`pig_latin("The quick brown fox jumped over the lazy dog")` should return `"Ethay uickqay ownbray oxfay umpedjay overway ethay azylay ogday"`.

```js
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(pig_latin("The quick brown fox jumped over the lazy dog"), "Ethay uickqay ownbray oxfay umpedjay overway ethay azylay ogday")`)
}})
```

# --seed--

## --seed-contents--

```py
def pig_latin(s):

    return s
```

# --solutions--

```py
def pig_latin(s):
    vowels = "aeiou"

    def convert_word(word):
        lower_word = word.lower()
        if lower_word[0] in vowels:
            return word + "way"
        i = 0
        while i < len(word) and lower_word[i] not in vowels:
            i += 1
        consonants = word[:i]
        rest = word[i:]
        result = rest + consonants + "ay"
        if word[0].isupper():
            return result[0].upper() + result[1:].lower()
        return result

    return " ".join(convert_word(word) for word in s.split(" "))
```
