Learning Python in 2020 – Beginner – Pig Latin

Java solution

Problem

Goal

Simply take a word, check if it begins with a vowel or a consonant cluster and append “ay” accordingly. If the word starts with a vowel, add an “ay” to the end of the word. Else extract the consonant cluster from the word, append “ay” to the cluster and join the remaining part of the word with the “ay”ed consonant cluster.

Pseudo-code

  • Get user input
  • Check if the first character is a vowel or consonant
    • If vowel, print out user input + “ay”
    • If consonant, find get consonant cluster ( group of consonant before the first vowel in the user input)
    • Print out part of user input starting from the first vowel + consonant cluster + “ay”

Solution

Determine if the first character of the word is a vowel

import sys
def firstCharisVowel(firstcharacter:str) -> bool:
    isvowel = False
    vowels = "aeiou"
    for i in range(0, len(vowels)):
        character = vowels[i]
        if character == firstcharacter:
            isvowel = True
            break
    return isvowel

Extract consonant cluster before first vowel occurrence in string

def getConsonantCluster(word:str) ->str:
    consonantcluster = ""
    for i in range(0, len(word)):
        current_char = word[i]
        if firstCharisVowel(current_char):
            break
        else:
            consonantcluster += current_char
    return consonantcluster

Transform given word to piglatin

def wordToLatin(word:str) -> str:
    latin = ""
    isvowel = firstCharisVowel(word[0])
    if isvowel:
        latin += word + "ay"
    else:
        consonantcluster = getConsonantCluster(word)
        latin += word[word.index(consonantcluster) + len(consonantcluster):] + consonantcluster + "ay"
    return latin

Trigger Action

def main()-> None:
    if len(sys.argv) == 1:
        text = input("Provide a word or a sentence an press Enter:  ")
        piglatinized = wordToLatin(text)
        print("Your Pig Latin is : {}".format(piglatinized))
    else:
        print("Your piglatinized words are: ")
        for word in sys.argv[1:]:
            piglatin = wordToLatin(word)
            print("Word: {} ---- {}".format(word, piglatin))
    


if __name__ == "__main__":
    main()

Run Program

.....Python/Python36-32/python.exe c:/Users/ajala/python/test.py
Provide a word or a sentence an press Enter:  hello
Your Pig Latin is : ellohay
Python/Python36-32/python.exe c:/Users/ajala/python/test.py smile string stupid glove trash floor store
Your piglatinized words are: 
Word: smile ---- ilesmay
Word: string ---- ingstray
Word: stupid ---- upidstay
Word: glove ---- oveglay
Word: trash ---- ashtray
Word: floor ---- oorflay
Word: store ---- orestay

PigLatin.py

import sys
def firstCharisVowel(firstcharacter:str) -> bool:
    isvowel = False
    vowels = "aeiou"
    for i in range(0, len(vowels)):
        character = vowels[i]
        if character == firstcharacter:
            isvowel = True
            break
    return isvowel

def getConsonantCluster(word:str) ->str:
    consonantcluster = ""
    for i in range(0, len(word)):
        current_char = word[i]
        if firstCharisVowel(current_char):
            break
        else:
            consonantcluster += current_char
    return consonantcluster

def wordToLatin(word:str) -> str:
    latin = ""
    isvowel = firstCharisVowel(word[0])
    if isvowel:
        latin += word + "ay"
    else:
        consonantcluster = getConsonantCluster(word)
        latin += word[word.index(consonantcluster) + len(consonantcluster):] + consonantcluster + "ay"
    return latin 


def main()-> None:
    if len(sys.argv) == 1:
        text = input("Provide a word or a sentence an press Enter:  ")
        piglatinized = wordToLatin(text)
        print("Your Pig Latin is : {}".format(piglatinized))
    else:
        print("Your piglatinized words are: ")
        for word in sys.argv[1:]:
            piglatin = wordToLatin(word)
            print("Word: {} ---- {}".format(word, piglatin))
    


if __name__ == "__main__":
    main()

Links

  1. Python For Loops
  2. typing — Support for type hints
  3. Python String index()
  4. How to Substring a String in Python
  5. Python – Command Line Arguments
  6. Taking input in Python
  7. What does the if __name__ == “__main__”: do?