Last active
August 29, 2015 14:15
-
-
Save EnriqueSoria/70a56efb2c7be499c9ff to your computer and use it in GitHub Desktop.
Pig latin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
def pig(word): | |
translated = '' | |
punct = '' | |
''' Nos guardamos si tiene algún signo de puntuación ''' | |
if word[-1] in punctuation: | |
punct = word[-1] | |
word = word[:-1] | |
''' Si una palabra no comienza con una letra se deja igual ''' | |
if not word[0].isalpha(): | |
return word | |
''' Palabras que comienzan con vocal (incluyendo la y), simplemente se agrega "yay" al final de la palabra. ''' | |
if word[0] in vowels: | |
translated = '%syay' % word | |
''' Si comienza con consonantes, se mueven todas las consonantes antes de la primera vocal al final y se agrega la sílaba "ay". ''' | |
if word[0] not in vowels: | |
# Tenemos que coger el prefijo de consonantes más largo | |
pref_count = 0 | |
for char in word: | |
if char not in vowels: | |
pref_count += 1 | |
else: | |
translated = '%s%say' % (word[pref_count:], word[:pref_count]) | |
break | |
''' Añadimos (si hay) signos de puntuación ''' | |
translated += punct | |
''' Vamos a respetar las mayúsculas ''' | |
if word.isupper(): | |
return translated.upper() | |
if word[0].isupper(): | |
return translated.capitalize() | |
else: | |
return translated | |
def main(): | |
# Traducimos | |
inp = raw_input('Introduce palabras:\n') | |
while inp: | |
# Separamos las palabras | |
for word in inp.split(' '): | |
print pig(word), | |
print '' | |
# Preguntamos por más palabras | |
inp = raw_input('') | |
if __name__=='__main__': | |
# Vocales | |
vowels = 'aeiouyAEIOUY' | |
punctuation = '.,:;?!' | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment