Question

Please use Python 3+ Write a function called let_to_num. It should take a word and encode...

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

Homework Answers

Answer #1

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:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a function called score that meets the specifications below. def score(word, f): """ word, a...
Write a function called score that meets the specifications below. def score(word, f): """ word, a string of length > 1 of alphabetical characters (upper and lowercase) f, a function that takes in two int arguments and returns an int Returns the score of word as defined by the method: 1) Score for each letter is its location in the alphabet (a=1 ... z=26) times its distance from start of word. Ex. the scores for the letters in 'adD' are...
Python #Write a function called are_anagrams. The function should #have two parameters, a pair of strings....
Python #Write a function called are_anagrams. The function should #have two parameters, a pair of strings. The function should #return True if the strings are anagrams of one another, #False if they are not. # #Two strings are considered anagrams if they have only the #same letters, as well as the same count of each letter. For #this problem, you should ignore spaces and capitalization. # #So, for us: "Elvis" and "Lives" would be considered #anagrams. So would "Eleven plus...
Create a function called, convert. This function receives a string parameter called word which only contains...
Create a function called, convert. This function receives a string parameter called word which only contains digits (the string represents a positive number) and returns a list of numbers. This is how the function works: - This function calculates the number of times each digit has repeated in the input string and then generates a number based on that using the following formula and adds it to a list. For instance, if the digit x has been repeated n times,...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class...
0. Introduction. In this laboratory assignment, you will write a Python class called Zillion. The class Zillion implements a decimal counter that allows numbers with an effectively infinite number of digits. Of course the number of digits isn’t really infinite, since it is bounded by the amount of memory in your computer, but it can be very large. 1. Examples. Here are some examples of how your class Zillion must work. I’ll first create an instance of Zillion. The string...
Problem 2: Python 3 Implement a function called gee_whiz that does the following: given argument n,...
Problem 2: Python 3 Implement a function called gee_whiz that does the following: given argument n, a positive integer, it returns a list of n tuples corresponding to the numbers 1 through n (both inclusive): the tuple for the number k consists of k as the first component, and exactly one of the following strings as the second: • the string 'two!' if k is divisible by 2 • the string 'three!' if k is divisible by 3 • the...
3. Write function, leastChar(inputString) that takes as input a string of one or more letters (and...
3. Write function, leastChar(inputString) that takes as input a string of one or more letters (and no other characters) and prints 1) the "least" character in the string, where one character is less than another if it occurs earlier in the alphabet (thus 'a' is less than 'C' and both are less than 'y') and 2) the index of the first occurrence of that character. When comparing letters in this problem, ignore case - i.e. 'e' and 'E' should be...
Using C programming Create a function called printMenu( ) with the following properties: Has no function...
Using C programming Create a function called printMenu( ) with the following properties: Has no function inputs or output. Prints the following menu to the screen: 1. Enter user name. 2. Enter scores. 3. Display average score. 4. Display summary. 5. Quit Create a function called printLine( ) with the following properties: Takes as input a char Takes as input an integer corresponding to the number of times to print the character Has no function output. For example, if we...
This is an intro to Python question: #Write a function called linear() that takes two parameters...
This is an intro to Python question: #Write a function called linear() that takes two parameters #- a list of strings and a string. Write this function so #that it returns the first index at which the string is #found within the list if the string is found, or False if #it is not found. You do not need to worry about searching #for the search string inside the individual strings within #the list: for example, linear(["bobby", "fred"], "bob") #should...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in the file contains seven numbers,which are the sales number for one week. The numbers are separated by comma.The following line is an example from the file 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36. The program should display the following: . The total sales for each week . The average daily sales for each week . The total sales for all of the weeks .The average weekly sales .The week number...
Using python language 1- Write a function for checking the speed of drivers with 3 different...
Using python language 1- Write a function for checking the speed of drivers with 3 different speeds: 55, 90, 160. If the return result is 0, it should print “Good”. If the return is not zero but less then 12, it should print: ‘Too fast“ if the return is  greater than 70, then print “Your license is suspended!” 2- Write a function that takes a series of numbers as parameters to check if the driver is speeding up or slowing down....