I'am trying to make a function that takes in a string as input and gives back the Nato alphabet for each corresponding letter. i've ben trying to do this for days i'am so furstrated i don't know how to treat them as an array or as a string how i imagine it would be done is taking making both alphabets elements/strings to be equal like Nato=alphabet and use a for i loop to only print out the input letters. any hints or ideas on how/what to do?
import numpy as np
def textToNato(plaintext):
plaintext=plaintext.lower()
plaintext="-".join(plaintext)
plaintext=plaintext.split()
Nato=np.array(["Alpha","Bravo","Charlie","Delta","Echo","Foxtrot",
"Golf","Kilo","Lima","Mike","November","Oscar",
"Papa","Quebec","Romeo","Sierra","Tango","Uniform"
"Victor","Whiskey","Xray","Yankee","Zulu"])
alphabet=np.array(["a","b","c","d","e","f","g","k","l","m","n","o"
,"p","q","r","s","t","u","v","w","x","y","z"])
new=plaintext
for i in range(len(Nato)): # i have no idea what i'am trying to do here
new=np.append(alphabet[i],Nato[i])
return new
CodePudding user response:
I would create a different data-structure for doing the lookup. You can make a dictionary of what word each letter in the alphabet points to. Then loop through each letter in the word, lookup the letter in the dictionary, and add the nato letter to a list.
nato_alphabet = {
'a': 'Alpha', 'b': 'Bravo', 'c': 'Charlie', 'd': 'Delta', 'e': 'Echo',
'f': 'Foxtrot', 'g': 'Golf', 'h': 'Hotel', 'i': 'India', 'j': 'Juliet',
'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'
}
def word_to_nato(word):
output = []
for letter in word:
output.append(nato_alphabet[letter.lower()])
return output
print(word_to_nato("foobar"))
['Foxtrot', 'Oscar', 'Oscar', 'Bravo', 'Alpha', 'Romeo']
CodePudding user response:
I see two problems with your code:
# ...
plaintext = plaintext.split()
# ...
# "plaintext"'s value here is overwritten by
# each element of alphabet
for plaintext in alphabet:
# ...
# Below compares each element of the alphabet
# with the alphabet list.
# You want to check each element of plaintext against
# each element of alphabet, to find the index and get the
# corresponding element of Nato
if plaintext == alphabet:
# ...
I think what you want to do is:
- loop through each element of your input, and
- loop through each letter of the alphabet to find the index, and
- use that index to get the corresponding phonetic alphabet word.
That could be done like this:
output = ''
for char1 in plaintext:
found = False
for i, char2 in enumerate(alphabet):
if char1 == char2:
output = Nato[i] ', '
found = True
break
if not found: output = 'not found'
return output
An easier and more efficient way is to use a dictionary (aka a hashmap):
nato_map = {
'a' : 'Alpha',
'b' : 'Bravo',
# ...
}
output = ''
for char in plaintext:
output = nato_map[char] ', '
That way, the lookup is in constant time, rather than needing to loop through every element of the Nato list.