给定包含十进制数的字符串:
teststring134this 123test string54 100
将此字符串中的每个数字递增一个,以给出新的字符串。
teststring135this 124test string55 101。
字符串可以如下所示:
覆盖所有可能的职位:
123test►124testtest123►test124te123st►te124sttest 123 test►test 124 test下面是Python中的一种非高尔夫解决方案:
NUMBERS = '0123456789'
def increment(s):
out = ''
number = ''
for c in s:
if c in NUMBERS:
number += c
else:
if number != '':
out += str(int(number) + 1)
number = ''
out += c
if number != '':
out += str(int(number) + 1)
number = ''
return out
print "\"%s\"" % (increment('teststring134this 123test string54 100'))这是一个code-golf问题,最短代码获胜。
发布于 2014-09-21 14:07:47
H=k=>k.replace(/\d+/g,k=>++k)通过使用H("test 123 234t")运行。
发布于 2014-09-22 03:24:51
0qqqqq^Al@qq@q期望输入为当前行。
或对于8+ ceil(log(n))中的有限多个数字(例如999),击键:
0qq^Alq999@q发布于 2014-09-21 11:20:16
将字符串作为变量n提供
import re;print re.sub('\d+',lambda x:`int(x.group())+1`,n)https://codegolf.stackexchange.com/questions/38033
复制相似问题