python - Addition of chars adding one character in front -
what i'm trying implement function increments string 1 character, example:
'aaa' + 1 = 'aab' 'aaz' + 1 = 'aba' 'zzz' + 1 = 'aaaa'
i've implemented function first 2 cases, can't think of solution third case.
here's code :
def new_sku(s): s = s[::-1] already_added = false new_sku = str() in s: if not already_added: if (i < 'z'): already_added = true new_sku += chr((ord(i)+1)%65%26 + 65) else: new_sku += return new_sku[::-1]
any suggestions ?
how ?
def new_sku(s): s = s[::-1] already_added = false new_sku = str() in s: if not already_added: if (i < 'z'): already_added = true new_sku += chr((ord(i)+1)%65%26 + 65) else: new_sku += if not already_added: # carry still left? new_sku += 'a' return new_sku[::-1]
sample run :-
$ python sku.py z aa $ python sku.py zzz aaaa $ python sku.py aaa aab $ python sku.py aaz aba
Comments
Post a Comment