This post originated from an RSS feed registered with Python Buzz
by Simon Brunning.
Original Post: Incrementing strings
Feed Title: Small Values of Cool
Feed URL: http://www.brunningonline.net/simon/blog/index-PnJ.xml
Feed Description: Simon Brunning - stuff that I find interesting
While I'm posting silly little Python scripts, here's one I prepared earlier. def inc_string(string, allowGrowth=True): '''Increment a string.''' CHAR_RANGES = [ Bunch(from_char=48, to_char=57), # digits Bunch(from_char=65, to_char=90), # upper case Bunch(from_char=97, to_char=122), # lower case ] string_chars = list(string) string_chars[-1] = chr(ord(string_chars[-1]) + 1) for index in range(-1, -len(string_chars), -1): for char_range in CHAR_RANGES: if ord(string_chars[index]) == char_range.to_char + 1: string_chars[index] = chr(char_range.from_char) string_chars[index-1] = chr(ord(string_chars[index-1]) + 1) for char_range in CHAR_RANGES: if ord(string_chars[0]) == char_range.to_char + 1: if allowGrowth: string_chars[0] = chr(char_range.from_char)...