logo CodeStepByStep logo

pig_latin

Language/Type: Python file input
Author: Nick Troccoli (on 2017/07/08)

Write a function named pig_latin that accepts as a parameter a string representing an input file name. Your function should, preserving line breaks, prout the input file's text in a simplified version of Pig Latin, a silly English variant where the first letter of each word is moved to the end. The rules for translating a word to Pig Latin are as follows:

  • If the word starts with a vowel (aeiou), simply append "yay". For example, "elephant" becomes "elephantyay".
  • If the word starts with a consonant, move the consonant to the end, and append "ay". For example, "welcome" becomes "elcomeway".

As an overall example, if the input file lincoln.txt contains the following text:

four score and
seven years ago our
fathers brought forth on this continent a new nation

The call of pig_latin("lincoln.txt") should produce the following output:

ourfay coresay andyay
evensay earsyay agoyay ouryay
athersfay roughtbay orthfay onyay histay ontinentcay ayay ewnay ationnay
Function: Write a Python function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.