Files

11 lines
567 B
Python

def decode_cyclic(s: str):
"""
takes as input string encoded with encode_cyclic function. Returns decoded string.
"""
# split string to groups. Each of length 3.
groups = [s[(3 * i):min((3 * i + 3), len(s))] for i in range((len(s) + 2) // 3)]
# reverse the cycle operation: shift the last character to the front
# encode does: abc -> bca (shift left)
# decode should: bca -> abc (shift right, i.e., last char to front)
groups = [(group[-1] + group[:-1]) if len(group) == 3 else group for group in groups]
return "".join(groups)