How to shift letters in a string x times by giving a list of shifts in Python

1 Answer

0 votes
'''

string = "aaa"
After Shifting the first 1 letter by 1 = "baa"
After shifting the first 2 letters by 2 = "dca"
After shifting the first 3 letters 3 = "gfd"
result = "gfd"

'''

def shifLetters(s,  shifts) :
    size = len(shifts)
    lst = []

    i = size - 1
    while (i >= 0) :
        if (i + 1 < size) :
            shifts[i] += shifts[i + 1]
        shifts[i] = shifts[i] % 26
        asciicode = ord(s[i]) - ord('a')
        asciicode = asciicode + shifts[i]
        if (asciicode > 25) :
            asciicode = asciicode - 26
        lst.append(chr(ord('a') + asciicode))
        i -= 1
    
    lst.reverse()
    
    return ''.join(lst)
    
    
s = "aaa"
shifts = [1, 2, 3]

s = shifLetters(s, shifts)

print(s)


'''
run:

gfd

'''

 



answered Feb 28, 2024 by avibootz

Related questions

...