Skip to content

Instantly share code, notes, and snippets.

@axymthr
Created January 8, 2022 09:53
Show Gist options
  • Select an option

  • Save axymthr/bf35c041a226380c8e84985dc01d7319 to your computer and use it in GitHub Desktop.

Select an option

Save axymthr/bf35c041a226380c8e84985dc01d7319 to your computer and use it in GitHub Desktop.
"""
Conversion functions for the NATO Phonetic Alphabet.
"""
# To save a lot of typing the code words are presented here
# as a dict, but feel free to change this if you'd like.
ALPHANUM_TO_NATO = {
"A": "ALFA",
"B": "BRAVO",
"C": "CHARLIE",
"D": "DELTA",
"E": "ECHO",
"F": "FOXTROT",
"G": "GOLF",
"H": "HOTEL",
"I": "INDIA",
"J": "JULIETT",
"K": "KILO",
"L": "LIMA",
"M": "MIKE",
"N": "NOVEMBER",
"O": "OSCAR",
"P": "PAPA",
"Q": "QUEBEC",
"R": "ROMEO",
"S": "SIERRA",
"T": "TANGO",
"U": "UNIFORM",
"V": "VICTOR",
"W": "WHISKEY",
"X": "XRAY",
"Y": "YANKEE",
"Z": "ZULU",
"0": "ZERO",
"1": "ONE",
"2": "TWO",
"3": "TREE",
"4": "FOUR",
"5": "FIVE",
"6": "SIX",
"7": "SEVEN",
"8": "EIGHT",
"9": "NINER",
}
def transmit(message: str) -> str:
"""
Convert a message to a NATO code word transmission.
"""
# code_words = []
natos = [ALPHANUM_TO_NATO.get(str(char)) for char in message.upper()]
code_words = filter(lambda x: x is not None, natos)
return " ".join(code_words)
def receive(transmission: str) -> str:
"""
Convert a NATO code word transmission to a message.
"""
words = transmission.split(" ")
message = []
for word in words:
for k,v in ALPHANUM_TO_NATO.items():
if v == word:
message.append(k)
return "".join(message)
print(receive("NOVEMBER CHARLIE CHARLIE ONE SEVEN ZERO ONE DELTA"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment