2013-10-01 1 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 

자음으로 시작하는 단어를 처리 할 수있는 번역기를 작성해야합니다.여러 자음으로 시작하는 단어에 대한 PigLatin 번역기 [파이썬]

예를 들어, 나는 "문자열"을 입력 할 때 나는 현재 얻을 출력은 다음과 같습니다

PigLatin("string") = tringsay

I 출력 할 것이다 :

PigLatin("string") = ingstray

이를 작성하는 방법을, 나는 썼다 단어를 반복하고 첫 번째 모음을 찾으려면 추가 기능이 필요하지만 그 이후에는 어떻게 진행해야할지 모르겠다. 도움이 될 것입니다.

+0

첫 번째 모음을 찾기 위해 코드를 표시 할 수 있으며 계속 진행하도록 도울 수 있습니다. =) – justhalf

+0

@justhalf 그는했다. 질문의 두 번째 기능입니다. – TerryA

+0

오, 제대로 들여 쓰지 않았으므로 기능을 비워 뒀습니다. 죄송합니다 – justhalf

답변

1

첫 번째 모음을 찾은 후 word.index(c)을 수행하여 문자열에서 해당 색인을 가져옵니다. 그런 다음 처음부터 전체 단어를 모음의 색인으로 자르십시오.

첫 번째 기능에서와 마찬가지로 단어의 끝에 단어를 넣고 'ay'을 추가하십시오.

0

자음 색인을 찾아야합니다. 이이 일을 더 웅변 방법은 아마도하지만이 내 솔루션입니다

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

:

다음은 예입니다. 희망적으로 도움이됩니다!

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