defrfencode(str, l): s = '' group = math.ceil(len(str) / l) # 组数 for i inrange(0, l): for j inrange(0, group): ind = j * l + i if ind < len(str): s += str[ind] return s
defrfdecode(str, l): s = '' oldgroup = math.ceil(len(str) / l) group = math.ceil(len(str) / oldgroup) # 组数 plus = len(str) % group length = math.ceil(len(str) / group) arr = [""] * group i = 0 j = 0 while j < group: if plus: arr[j] = str[i: i + length] i += length plus -= 1 else: arr[j] = str[i: i + length - 1] i += (length - 1) j += 1 i = 0 j = 0 while (i + j * l) < len(str): if i == group: j += 1 i = 0 continue s += arr[i % group][j % length] i += 1 return s
if __name__ == '__main__': pT = 'hellome' print(rfencode(pT, 3)) sT = 'hleeolm' print(rfdecode(sT, 3))