In: Computer Science
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.
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:
