~ $irb --simple-prompt>> '2'.succ=> "3">> '99'.succ=> "100">> 'zzz'.succ=> "aaaa">>解决方法 以下实现是我可以通过ruby的String#succ实现得到的最接近的实现
def succ(s): if not isinstance(s,(str,string)): raise TypeError("succ works only with strings") if not s: return if max(map(ord,s)) > 127: raise TypeError("succ currently only supports ascii") #Three different character category honoured # 1. ascii lowercase Alpha # 2. ascii uppercase Alpha # 3. digits # 4. ascii nonAlpha characters (the entire ascii set) lower = string.ascii_lowercase + 'a' upper = string.ascii_uppercase + 'A' digits = string.digits + '0' nonAlpha = map(chr,range(0,256)) + [chr(0)] def incr(ch): ''' Generates the next character in sequence using the following rules 1. Incrementing a digit always results in another digit 2. Incrementing a letter results in another letter of the same case. 3. Incrementing nonAlphanumerics uses the underlying character set’s collating sequence. ''' if ch.isdigit(): return digits[ord(ch) - ord("0") + 1] if ch.islower(): return lower[ord(ch) - ord('a') + 1] if ch.isupper(): return upper[ord(ch) - ord('A') + 1] return nonAlpha[ord(ch) + 1] def last(ch): ''' Returns the last character in its catagory ''' if ch.isdigit(): return digits[-2] if ch.islower(): return lower[-2] if ch.isupper(): return upper[-2] return nonAlpha[-2] def first(ch): ''' Returns the last first in its catagory ''' if ch.isdigit(): return digits[0] if ch.islower(): return lower[0] if ch.isupper(): return upper[0] return nonAlpha[0] def islast(ch): ''' Checks if next increment would generate a carry ''' return ch == last(ch) s = List(s)[::-1] carry = True try: index = next(i for i,e in enumerate(s) if e.isalnum()) except stopiteration: index = 0 while carry: if index == len(s): #Add a character for Overflow s.append(first(s[index - 1])) break carry = True if islast(s[index]) else False s[index] = incr(s[index]) index += 1 return ''.join(s[::-1])
示例用法
succ('2') - > 3 succ('99') - > 000 succ('zzz') - > aaaa succ('') - > None succ('abcd') - > abce succ('THX1138') - > THX1139 succ('<<koala>>') - > <<koalb>> succ('1999zzz') - > 2000aaa succ('ZZZ9999') - > AAAA0000 succ('***') - > **+总结
以上是内存溢出为你收集整理的python的Ruby String#succ的对应物?全部内容,希望文章能够帮你解决python的Ruby String#succ的对应物?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)