deffunc(S,K): S = list(S.upper()) first = 0 lens = len(S) offset = 0 popList = [] popoffset = 0 for i in range(lens): if(S[i]=='-'and first!=1): first = 1 offset = i elif(S[i]=='-'): popList.append(i) lens-=1 for i in popList: S.pop(i-popoffset) popoffset+=1 res = lens-(offset+1) res_char_num = K insertList = [] for i in range(offset+1,lens): if((i-offset-1)%res_char_num==0and (i-offset-1)!= 0): insertList.append(i) insertOffset = 0 for i in insertList: S.insert(i+insertOffset,'-') insertOffset+=1 return''.join(S)
deffunc(S,K): S = list(S.upper().replace('-','')) insertList = [] insertOffset = 0 n = 1 for i in range(len(S)-1,0,-1): if(n%K==0): insertList.append(i) n+=1 insertList.sort() for i in insertList: S.insert(i+insertOffset,'-') insertOffset+=1 return''.join(S) S = '5F3Z-2e-9-w' K = 4 print(func(S,K))