python pig latin

This commit is contained in:
Xevion
2020-11-15 09:24:39 -06:00
parent 6577acde42
commit 54e9dba5fc
4 changed files with 166 additions and 0 deletions

View File

@@ -0,0 +1 @@
{"track":"python","exercise":"pig-latin","id":"4a3eab6787d64b43af2d7c777df7cf17","url":"https://exercism.io/my/solutions/4a3eab6787d64b43af2d7c777df7cf17","handle":"Xevion","is_requester":true,"auto_approve":false}

View File

@@ -0,0 +1,67 @@
# Pig Latin
Implement a program that translates from English to Pig Latin.
Pig Latin is a made-up children's language that's intended to be
confusing. It obeys a few simple rules (below), but when it's spoken
quickly it's really difficult for non-children (and non-native speakers)
to understand.
- **Rule 1**: If a word begins with a vowel sound, add an "ay" sound to the end of the word. Please note that "xr" and "yt" at the beginning of a word make vowel sounds (e.g. "xray" -> "xrayay", "yttria" -> "yttriaay").
- **Rule 2**: If a word begins with a consonant sound, move it to the end of the word and then add an "ay" sound to the end of the word. Consonant sounds can be made up of multiple consonants, a.k.a. a consonant cluster (e.g. "chair" -> "airchay").
- **Rule 3**: If a word starts with a consonant sound followed by "qu", move it to the end of the word, and then add an "ay" sound to the end of the word (e.g. "square" -> "aresquay").
- **Rule 4**: If a word contains a "y" after a consonant cluster or as the second letter in a two letter word it makes a vowel sound (e.g. "rhythm" -> "ythmrhay", "my" -> "ymay").
There are a few more rules for edge cases, and there are regional
variants too.
See <http://en.wikipedia.org/wiki/Pig_latin> for more details.
## Exception messages
Sometimes it is necessary to raise an exception. When you do this, you should include a meaningful error message to
indicate what the source of the error is. This makes your code more readable and helps significantly with debugging. Not
every exercise will require you to raise an exception, but for those that do, the tests will only pass if you include
a message.
To raise a message with an exception, just write it as an argument to the exception type. For example, instead of
`raise Exception`, you should write:
```python
raise Exception("Meaningful message indicating the source of the error")
```
## Running the tests
To run the tests, run the appropriate command below ([why they are different](https://github.com/pytest-dev/pytest/issues/1629#issue-161422224)):
- Python 2.7: `py.test pig_latin_test.py`
- Python 3.4+: `pytest pig_latin_test.py`
Alternatively, you can tell Python to run the pytest module (allowing the same command to be used regardless of Python version):
`python -m pytest pig_latin_test.py`
### Common `pytest` options
- `-v` : enable verbose output
- `-x` : stop running tests on first failure
- `--ff` : run failures from previous test before running other test cases
For other options, see `python -m pytest -h`
## Submitting Exercises
Note that, when trying to submit an exercise, make sure the solution is in the `$EXERCISM_WORKSPACE/python/pig-latin` directory.
You can find your Exercism workspace by running `exercism debug` and looking for the line that starts with `Workspace`.
For more detailed information about running tests, code style and linting,
please see [Running the Tests](http://exercism.io/tracks/python/tests).
## Source
The Pig Latin exercise at Test First Teaching by Ultrasaurus [https://github.com/ultrasaurus/test-first-teaching/blob/master/learn_ruby/pig_latin/](https://github.com/ultrasaurus/test-first-teaching/blob/master/learn_ruby/pig_latin/)
## Submitting Incomplete Solutions
It's possible to submit an incomplete solution so you can see how others have completed the exercise.

View File

@@ -0,0 +1,21 @@
def translate(text):
return ' '.join(list(map(translate_word, text.split())))
vowels = "aeiou"
def translate_word(word):
# Rule 1
if word[0] in vowels or word[0:2] in ["xr", "yt"]:
return word + "ay"
# Rule 2
else:
i = [word.find(v) for v in vowels if word.find(v) != -1]
i = min(i) if i else 999
# Rule 4
if 'y' in word[1:]:
temp = word[1:].find('y')
if temp != -1 and temp < i: i = temp + 1
# Rule 3
if 'qu' in word:
temp = word.find('qu')
if temp != -1: i = temp + 2
return word[i:] + word[:i] + "ay"

View File

@@ -0,0 +1,77 @@
import unittest
from pig_latin import translate
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.2.0
class PigLatinTest(unittest.TestCase):
def test_word_beginning_with_a(self):
self.assertEqual(translate("apple"), "appleay")
def test_word_beginning_with_e(self):
self.assertEqual(translate("ear"), "earay")
def test_word_beginning_with_i(self):
self.assertEqual(translate("igloo"), "iglooay")
def test_word_beginning_with_o(self):
self.assertEqual(translate("object"), "objectay")
def test_word_beginning_with_u(self):
self.assertEqual(translate("under"), "underay")
def test_word_beginning_with_a_vowel_and_followed_by_a_qu(self):
self.assertEqual(translate("equal"), "equalay")
def test_word_beginning_with_p(self):
self.assertEqual(translate("pig"), "igpay")
def test_word_beginning_with_k(self):
self.assertEqual(translate("koala"), "oalakay")
def test_word_beginning_with_x(self):
self.assertEqual(translate("xenon"), "enonxay")
def test_word_beginning_with_q_without_a_following_u(self):
self.assertEqual(translate("qat"), "atqay")
def test_word_beginning_with_ch(self):
self.assertEqual(translate("chair"), "airchay")
def test_word_beginning_with_qu(self):
self.assertEqual(translate("queen"), "eenquay")
def test_word_beginning_with_qu_and_a_preceding_consonant(self):
self.assertEqual(translate("square"), "aresquay")
def test_word_beginning_with_th(self):
self.assertEqual(translate("therapy"), "erapythay")
def test_word_beginning_with_thr(self):
self.assertEqual(translate("thrush"), "ushthray")
def test_word_beginning_with_sch(self):
self.assertEqual(translate("school"), "oolschay")
def test_word_beginning_with_yt(self):
self.assertEqual(translate("yttria"), "yttriaay")
def test_word_beginning_with_xr(self):
self.assertEqual(translate("xray"), "xrayay")
def test_y_is_treated_like_a_consonant_at_the_beginning_of_a_word(self):
self.assertEqual(translate("yellow"), "ellowyay")
def test_y_is_treated_like_a_vowel_at_the_end_of_a_consonant_cluster(self):
self.assertEqual(translate("rhythm"), "ythmrhay")
def test_y_as_second_letter_in_two_letter_word(self):
self.assertEqual(translate("my"), "ymay")
def test_a_whole_phrase(self):
self.assertEqual(translate("quick fast run"), "ickquay astfay unray")
if __name__ == '__main__':
unittest.main()