Please use Python 3+ Write a function called let_to_num. It should take a word and encode it into a series of numbers and dashes, with the numbers corresponding to the position of the letter in the alphabet (regardless of casing). If the character is not a letter, then ignore it.
Examples of outputs: print(let_to_num('AZ')) # -> 1-26 print(let_to_num('')) # -> (empty string) print(let_to_num('A?Z')) # -> 1-26 print(let_to_num('AZ?')) # -> 1-26 print(let_to_num('AbzC')) # -> 1-2-26-3
Python code:
#defining let_to_num function
def let_to_num(word):
#initializing num as an empty string
num=""
#initializing flag as False
flag=False
#looping each character in the word
for i in word:
#checking if it is a
alphabet
if(i.isalpha()):
#adding it encoded number to num
num+=str(ord(i.upper())-64)+"-"
#setting flag as True
flag=True
#checking if flag is True
if(flag):
#returning the encoded
number
return num[:-1]
#returning empty string
return num
#calling let_to_num function and printing result
print(let_to_num('AZ'))
#calling let_to_num function and printing result
print(let_to_num(''))
#calling let_to_num function and printing result
print(let_to_num('A?Z'))
#calling let_to_num function and printing result
print(let_to_num('AZ?'))
#calling let_to_num function and printing result
print(let_to_num('AbzC'))
Screenshot:
Output:
Get Answers For Free
Most questions answered within 1 hours.