Advertisement
Guest User

pig latin

a guest
Sep 18th, 2019
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. def pig_latin(words):
  2. vwls = ['a','e','i','o','u']
  3. count = 0
  4. wlsts = words.split()
  5. final_list = []
  6.  
  7. for n, word in enumerate(wlsts):
  8. wordlist = list(word) #forming a list out of string. strings are immutable but list are not
  9. new_word = []
  10. count = 1+ count
  11.  
  12. if wordlist[0] in vwls:
  13. new_word = wordlist[1:]
  14. new_word.append(wordlist[0])
  15.  
  16. else:
  17. new_word = wordlist
  18.  
  19. new_word.append("ni")
  20. new_word.append('j'*count)
  21. new_word = ''.join(new_word) #forming the new word
  22. final_list.append(new_word)
  23.  
  24. pig_latin = " ".join(final_list)
  25. return pig_latin
  26.  
  27. #testing...
  28. sentence = "This is just an example"
  29. new_word = pig_latin(sentence)
  30. print (new_word)
  31. Thisnij sinijj justnijjj nanijjjj xampleenijjjjj
  32.  
  33. ==========================================================
  34. #!/usr/bin/env python
  35.  
  36. def pig_latin(sentence):
  37.  
  38. vowels = 'aeiou'
  39. words = sentence.split()
  40. new_sentence = ''
  41.  
  42. cntr = 1
  43. for word in words:
  44. if len(word) > 1 and word[0] in vowels:
  45. word = word[1:] + word[0]
  46. word += 'ni' + 'j'*cntr
  47. cntr += 1
  48. new_sentence += word + ' '
  49.  
  50. return new_sentence.rstrip()
  51.  
  52. if __name__ == '__main__':
  53.  
  54. print pig_latin("This is just a small sentence")
  55.  
  56. # Testing it:
  57. > python pig_latin.py
  58.  
  59. Thisnij sinijj justnijjj anijjjj smallnijjjjj sentencenijjjjjj
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement