Goat Latin in Python

PythonServer Side ProgrammingProgramming

Suppose we have a set of strings (Sentence), in that set there are few words. Each words are consists of lowercase and uppercase letters. Our task is to convert the sentence into Goat-Latin form. The Goat Latin is similar to the Pig Latin. There are some few conditions.

  • If the word starts with a vowel, then append ‘ma’ with the word
  • It the word starts with a consonant, then remove that from beginning, and append it at the end, then add ‘ma’ at the end.
  • Add one letter ‘a’ to the end of each word per its word index in the sentence, starting with 1

So if the example is like “Adam wants to go to the university”, the converted string will be “Adammaa antswmaaa otmaaaa ogmaaaaa otmaaaaaa hetmaaaaaaa universitymaaaaaaaa”

To solve this, the task is simple, take the sentence and split it into words, then for each word, check the given conditions and perform the action as mentioned above.

Example

Let us see the following implementation to get better understanding −

 Live Demo

class Solution:
   def toGoatLatin(self, S):
      """
      :type S: str
      :rtype: str
      """
      temp = S.split(" ")
      counter = 1
      result = []
      vowel = {"a","e","i","o","u"}
      for i in temp:
         if i[0].lower() in vowel:
            x = i + "ma" + ("a"*counter)
         else:
            x=i[1:]+i[0] + "ma" +("a"*counter)
         counter+=1
         result.append(x)
      return " ".join(c for c in result)
ob1 = Solution()
print(ob1.toGoatLatin("Adam wants to go to the university"))

Input

"Adam wants to go to the university"

Output

"Adammaa antswmaaa otmaaaa ogmaaaaa otmaaaaaa hetmaaaaaaa universitymaaaaaaaa"
raja
Published on 16-Jan-2020 15:50:32
Advertisements