SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Tony Gaddis
Chapter:
More About Strings
Exercise:
Programming Exercises
Question:12 | ISBN:9780132576376 | Edition: 2

Question

Pig Latin
Write a program that accepts a sentence as input and converts each word to “Pig Latin.” In one version, to convert a word to Pig Latin you remove the first letter and place that letter at the end of the word. Then you append the string “ay” to the word. Here is an example: 
English: I SLEPT MOST OF THE NIGHT
Pig Latin: IAY LEPTSAY OSTMAY FOAY HETAY IGHTNAY

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Pig Latin Program Code:

def pigLatinConversion(inputData):
    # Split the input sentence into individual words
    words = inputData.split()

    # Initialize an empty list to store the converted words
    convertedData = []

    # Iterate through each word in the list of words
    for word in words:
        # Remove the first letter from the word and append it to the end
        converted_word = word[1:] + word[0]

        # Append "ay" to the converted word
        converted_word += "ay"

        # Add the converted word to the list of converted words
        convertedData.append(converted_word)

    # Join the converted words back into a sentence
    converted_sentence = " ".join(convertedData)

    # Return the final converted sentence
    return converted_sentence


# Initialized the input data from the question
inputData = "I SLEPT MOST OF THE NIGHT"
#Call the method, pigLatinConversion
output_sentence = pigLatinConversion(inputData)
#Display Output
print(output_sentence)

Executed Output:

Iay LEPTSay OSTMay FOay HETay IGHTNay

 

0 0

Discussions

Post the discussion to improve the above solution.