Skip to content

Instantly share code, notes, and snippets.

@Kabongosalomon
Last active January 24, 2022 11:43
Show Gist options
  • Save Kabongosalomon/35853a4381805d4ff800ecaf2c8267b4 to your computer and use it in GitHub Desktop.
Save Kabongosalomon/35853a4381805d4ff800ecaf2c8267b4 to your computer and use it in GitHub Desktop.
A Python program that replace all the vowels in the string e.g: “National Center for Supercomputing Applications” by their corresponding order number in alphabetical sequence (a with 1, e with 5, etc). Print the resulting string and the total number of consonants in the given string.
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="A Python code to replace vowel by numbers corresponding to alphabetical order ")
parser.add_argument("-string", "--string_txt", default="National Center for Supercomputing Applications")
args = parser.parse_args()
string_txt = args.string_txt
# there maybe libraries that provide these but
# I just decided to explicitly manage it myself
vowel = {'a', 'e', 'i', 'o', 'u', 'y'}
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', \
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
# this will help record the number of
# consonant seen already in a unique way
count_consonant = set()
# a dictionary that map every letter in the alphabet to
# a number.
dict_alph_number = {letter.lower(): str(number+1) for number, letter in enumerate(alphabet)}
# main code logic
transformed_input = ''
for location, caracter in enumerate(string_txt):
if caracter.lower() in vowel:
transformed_input += dict_alph_number[caracter.lower()]
else:
if caracter == " ":
transformed_input += " "
else:
count_consonant.add(caracter.lower())
transformed_input += caracter
print(f"Original string:\n`{string_txt}`")
print(f"Transformed input : \n{transformed_input}")
print(f"We have a notal of : {len(count_consonant)} consonants")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment