Sunday, January 27, 2019

codingdirectional: Translate normal character to NATO phonetic alphabet with python

Hello and welcome back, today we will create a NATO code translator program with python which will translate each character in any normal text into a NATO phonetic alphabet. This program will ignore the space within words and will not change the punctuation characters within that normal text.

Just in case you are interested in NATO phonetic alphabet, here are some facts about them.

The NATO phonetic alphabet, officially denoted as the International Radiotelephony Spelling Alphabet, and also commonly known as the ICAO phonetic alphabet, and in a variation also known officially as the ITU phonetic alphabet and figure code, is the most widely used radiotelephone spelling alphabet. Although often called “phonetic alphabets”, spelling alphabets are unrelated to phonetic transcription systems such as the International Phonetic Alphabet. Instead, the International Civil Aviation Organization (ICAO) alphabet assigned codewords acrophonically to the letters of the English alphabet, so that critical combinations of letters and numbers are most likely to be pronounced and understood by those who exchange voice messages by radio or telephone, regardless of language differences or the quality of the communication channel

http://bit.ly/10oo7a4

Here are the 26 elements within the NATO phonetic alphabet list.

pilot_alpha = ['Alfa', 'Bravo', 'Charlie', 'Delta', 'Echo', 'Foxtrot', 'Golf', 'Hotel', 'India', 'Juliett', 'Kilo', 'Lima', 'Mike', 'November', 'Oscar', 'Papa', 'Quebec', 'Romeo', 'Sierra', 'Tango', 'Uniform', 'Victor', 'Whiskey', 'Xray', 'Yankee', 'Zulu']

Below is the program which will do the translation job.

import string

def to_nato(words):

    pilot_alpha = ['Alfa', 'Bravo', 'Charlie', 'Delta', 'Echo', 'Foxtrot', 'Golf', 'Hotel', 'India', 'Juliett', 'Kilo', 'Lima', 'Mike', 'November', 'Oscar', 'Papa', 'Quebec', 'Romeo', 'Sierra', 'Tango', 'Uniform', 'Victor', 'Whiskey', 'Xray', 'Yankee', 'Zulu']
    word_list = list(words)
    str = ''
    
    for char in word_list:
        alpha_index = (ord(char.upper()) - 65) # get the location of the pilot_alpha index 
        if alpha_index in range(65):
            str += pilot_alpha[alpha_index] + " "
        elif char == " ":
            str += ""
        else:
            str += char + " "
                
    return str.strip()

If you input “Hello World!!!” into the above function you will get below outcome.

NATO phonetic alphabet

I am supposed to continue exploring codingame.com today and I actually did wrote some game code, take a look at the game below.

Hope you like this post and do subscribe to this website if you like it.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...