Question

In: Computer Science

I need to write a function that takes a user-provided string like 1-3-5, and output a...

I need to write a function that takes a user-provided string like 1-3-5, and output a corresponding series of letters, where A is assigned to 1, B is assigned to 2, C is assigned to 3, etc. So in the case of 1-3-5 the output would be ACE. For 2-3-4, it should print BCD. For ?-3-4 or --3-4 it should still print BCD. **CANNOT USE LISTS, SETS, DICTS, ETC. CANNOT USE SPLIT FUNCTION. ** Here is the code I have written so far:

def num_to_let(encoded):
    result = ""
    start = 0
    for char in range(len(encoded)):
        if encoded[char] == '-':
            i = encoded.index("-")
            sub_str = encoded[start:i]
            if not sub_str.isdigit():
                result += ""
            else:
                letter = chr(64 + int(sub_str))
                if 0 < int(sub_str) < 27:
                    result += letter

                else:
                    result += ""

            start += len(sub_str) + 1
            print(start)

    return result

print(num_to_let('4-3-25'))

It only outputs "D". Please let me know how I can correct this code.

Solutions

Expert Solution

Changes in the code:

def num_to_let(encoded):
    result = ""
    start = 0
    for char in range(len(encoded)):
        if encoded[char] == '-':
            sub_str = encoded[start:char]
            # print(sub_str)
            if not sub_str.isdigit():
                result += ""
            else:
                letter = chr(64 + int(sub_str))
                if 0 < int(sub_str) < 27:
                    result += letter

                else:
                    result += ""

            start += len(sub_str) + 1
    #only need to do this part for the last part as for three numbers there will be only 2 dashes the end won't have a dash
    sub_str = encoded[start:]
    # print(sub_str)
    if not sub_str.isdigit():
        result += ""
    else:
        letter = chr(64 + int(sub_str))
        if 0 < int(sub_str) < 27:
            result += letter

        else:
            result += ""
    

    return result

print(num_to_let('4-3-25'))

There won't be need of i = encoded.index("-") as char variable will store the index of the dash. Also this variable will find the first index of - every time. So it won't be of use.

Also in the solution the - is after the number so the last part of the input won't print so we need to use the same thing inside the for loop outside of it and the sub_str = encoded[start:] which says the sub_str will be the part from the last value of start variable till the end of encoded. A better answer to the question will be to code as below:

def num_to_let(encoded):
    result = ""
    i = 0
    length = len(encoded)
    while(i < length):
        if encoded[i].isdigit(): #check if character is a digit or a dash if a digit then move in
            num = ''
            while(i < length and encoded[i].isdigit()): # this will check if the there are two digits together or not if they are then we need to combine them and increment the counter by 1 as the digit will be traversed
                num += encoded[i] #combine
                i += 1 #increment counter
            num = int(num)
            if num <= 26:
                result += chr(64 + num)
            else:
                result += ""
        i += 1

    return result

print(num_to_let('100-3-25'))

Output:


Related Solutions

Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Write a Java application with a JavaFXGUI that takes a String input by the user and...
Write a Java application with a JavaFXGUI that takes a String input by the user and shows whether the string contains all 26 letters of the (English version of the Latin) alphabet. For example, "Pack my box with five dozen liquor jugs" contains all 26 letters, but "The quick frown box jumps over the hazy log" does not contain a d. It does not matter whether one or more letters appear more than once. The GUI needs, at minimum, a...
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
Write a program that takes a string input from the user and then outputs the first...
Write a program that takes a string input from the user and then outputs the first character, then the first two, then the first three, etc until it prints the entire word. After going up to the full word, go back down to a single letter. LastNameUpDown. Input: Kean Output: K Ke Kea Kean Kea Ke K
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
PYTHON 3: must use try/except for exception handling Write a function extractInt() that takes a string...
PYTHON 3: must use try/except for exception handling Write a function extractInt() that takes a string as a parameter and returns an integer constructed out of the digits that appear in the string. The digits in the integer should appear in the same order as the digits in the string. If the string does not contain any digits or an empty string is provided as a parameter, the value 0 should be return.
Here is a Problem I need to solve: 5) Write a function to determine is a...
Here is a Problem I need to solve: 5) Write a function to determine is a given word is legal. A word is illegal if it contains no vowels. For this problem, the letter Y is considered to be a legal vowel. Pass in a word to this function and it will determine if the word is legal or not. You can make the following assumptions about the word you a passing to this function. 1) The string being passed...
Python please Write a function that takes a string as an argument checks whether it is...
Python please Write a function that takes a string as an argument checks whether it is a palindrome. A palindrome is a word that is the same spelt forwards or backwards. Use similar naming style e.g. name_pal. E.g. If we call the function as abc_pal(‘jason’) we should get FALSE and if we call it a abc_pal(‘pop’) we should get TRUE. Hint: define your function as abc_pal(str). This indicates that string will be passed. Next create two empty lists L1=[] and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT