2013-10-01 5 views
1
VOWELS = ('a', 'e', 'i', 'o', 'u') 


def pigLatin(word): 
    first_letter = word[0] 
    if first_letter in VOWELS: # if word starts with a vowel... 
     return word + "hay"  # then keep it as it is and add hay to the end 
    else: 
     return word[1:] + word[0] + "ay" 


def findFirstVowel(word): 
    novowel = False 
    if novowel == False: 
     for c in word: 
      if c in VOWELS: 
       return c 

Je dois écrire un traducteur de piglatine qui peut gérer des mots commençant par plusieurs consonnes.Traducteur PigLatin pour les mots commençant par plusieurs consonnes [Python]

Par exemple, la sortie que je reçois actuellement quand je rentre "string" est:

PigLatin("string") = tringsay

je ne voudrais la sortie:

PigLatin("string") = ingstray

Pour écrire cela, je l'ai écrit une fonction supplémentaire pour itérer à travers le mot et trouver la première voyelle, mais après cela, je ne suis pas sûr de savoir comment procéder. Toute aide serait appréciée.

+0

Vous pouvez nous montrer votre code pour trouver la première voyelle, et nous pouvons vous aider à continuer. =) – justhalf

+0

@justhalf Il l'a fait. C'est la deuxième fonction dans la question – TerryA

+0

Oh, Parce que ce n'est pas correctement indenté, je pensais qu'il laissait la fonction vide. Désolé – justhalf

Répondre

1

Après avoir trouvé la première voyelle, obtenir son index dans la chaîne en faisant word.index(c). Ensuite, découpez le mot entier du début à l'index de la voyelle

Avec cette tranche, placez-le à la fin du mot et ajoutez 'ay', comme vous l'avez fait dans votre première fonction.

0

Vous devez trouver l'index d'une consonne et tranche sur elle.

Voici un exemple:

def isvowel(letter): return letter.lower() in "aeiou" 

def pigLatin(word): 
    if isvowel(word[0]):  # if word starts with a vowel... 
     return word + "hay" # then keep it as it is and add hay to the end 
    else: 
     first_vowel_position = get_first_vowel_position(word) 
     return word[first_vowel_position:] + word[:first_vowel_position] + "ay" 

def get_first_vowel_position(word): 
    for position, letter in enumerate(word): 
     if isvowel(letter): 
      return position 
    return -1 
0

Il y a probablement des moyens plus éloquents de le faire, mais cela est ma solution. J'espère que ça aide!

def pigLatin(aString): 
    index = 0 
    stringLength = len(aString) 
    consonants = '' 

    # if aString starts with a vowel then just add 'way' to the end 
    if isVowel(aString[index]): 
     return aString + 'way' 
    else: 
    # the first letter is added to the list of consonants 
     consonants += aString[index] 
     index += 1 

     # if the next character of aString is a vowel, then from the index 
     # of the vowel onwards + any consonants + 'ay' is returned 
     while index < stringLength: 
      if isVowel(aString[index]): 
       return aString[index:stringLength] + consonants + 'ay' 
      else: 
       consonants += aString[index] 
       index += 1 
     return 'This word does contain any vowels.' 

def isVowel(character): 
    vowels = 'aeiou' 
    return character in vowels 
Questions connexes