Python:如何增加一个特殊用途的字符串?
考虑长度为8的特殊用途字符串,例如“A00000XY”。该字符串有以下限制Python:如何增加一个特殊用途的字符串?,python,algorithm,data-structures,Python,Algorithm,Data Structures,考虑长度为8的特殊用途字符串,例如“A00000XY”。该字符串有以下限制 长度=8 最后两个字符有特殊的含义,应该保持原样 A-Z和0-9仅为有效字符。因此,正则表达式“^[A-Z0-9]{6}XY$”定义了字符串 我如何实现一个函数,比如increment,当被调用时,它会将字符串增加1。因此,后续调用应如下所示: >>> A = "A00000XY" >>> print increment(A) "A00000XY" >>> print
>>> A = "A00000XY"
>>> print increment(A)
"A00000XY"
>>> print increment(A)
"A00001XY"
>>> print increment(A)
"A00002XY"
...
>>> print increment(A)
"A00009XY"
>>> print increment(A)
"A0000AXY"
>>> print increment(A)
"A0000BXY"
...
>>> print increment(A)
"A0000YXY"
>>> print increment(A)
"A0000ZXY"
>>> print increment(A)
"A00010XY"
>>> print increment(A)
"A00011XY"
...
>>> print increment(A)
"ZZZZZZXY"
诱惑太大了。不过,作为家庭作业答案恐怕不是很合适,D
更新:是Python 2。在Python3中,除法应该是
num//36
,所以您真正想要的是一个基数为36的数字。您将需要构建一个类似于十六进制到十进制和十进制到十六进制转换工作方式的类。如果限制为8个字符,则值的范围为0到36^8-1,即282110907455。
9223372036854775807是整数,所以好消息是您可以将值表示为整数
要将字符串值转换为整数,请执行以下操作:
灵感来源于@avysk的回应
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def digit_helper(num):
while num > 0:
yield Tape.digits[num % 36]
num = num / 36
# This function, using the utility digit_helper, increments the string by 1.
# It produces string in following order:
# "A00000XY", "A00000XY", "A00001XY", "A00002XY", ...,
# "A00009XY", "A0000AXY", "A0000BXY", ...,
# "A0000YXY", "A0000ZXY", "A00010XY", "A00011XY", ...,
# "ZZZZZZXY", "000000XY", "000001XY", ...
# Logic:
# 1. Strip the string of last two chars.
# 2. Convert to base 36 equivalent integer and increment by one.
# 3. Convert back to Base 36 representation of incremented value.
# 3.1. [0:6] handles the overflow. Overflow happens after "ZZZZZZXY" and produces "1000000XY".
# 3.2. [::-1] reverses the string.
# 3.3. zfill(6) handles underflow, like converting integer 1 to "000001XY".
def increment(string):
incremented = int(string[:-2], base=36) + 1
return "".join(Tape.digit_helper(incremented))[0:6][::-1].zfill(6) + string[-2:]
@avysk的回复有两个问题
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def digit_helper(num):
while num > 0:
yield Tape.digits[num % 36]
num = num / 36
# This function, using the utility digit_helper, increments the string by 1.
# It produces string in following order:
# "A00000XY", "A00000XY", "A00001XY", "A00002XY", ...,
# "A00009XY", "A0000AXY", "A0000BXY", ...,
# "A0000YXY", "A0000ZXY", "A00010XY", "A00011XY", ...,
# "ZZZZZZXY", "000000XY", "000001XY", ...
# Logic:
# 1. Strip the string of last two chars.
# 2. Convert to base 36 equivalent integer and increment by one.
# 3. Convert back to Base 36 representation of incremented value.
# 3.1. [0:6] handles the overflow. Overflow happens after "ZZZZZZXY" and produces "1000000XY".
# 3.2. [::-1] reverses the string.
# 3.3. zfill(6) handles underflow, like converting integer 1 to "000001XY".
def increment(string):
incremented = int(string[:-2], base=36) + 1
return "".join(Tape.digit_helper(incremented))[0:6][::-1].zfill(6) + string[-2:]
一些提示:使用
ord
和chr
。检查ASCII表格中的数字(48-57)和大写字母(65-90)。有一个要递增的列表。当你用数字递增时,从右到左对每一个数字都这样做。如果右侧溢出,则增加邻居。现在玩一下,看看你能走多远,当你付出更多努力时,再来问一些具体的问题。我如何实现一个函数,…?
-慢慢来-最终你将掌握可用的工具,并开始获得想法-尝试一些想法。你真的需要增加特定字符串,还是需要一个按顺序迭代所有此类字符串的算法?在前一种情况下,您可以尝试使用itertools.product
。您可以使用带基数36的a将字符串转换为数字并返回。num/36
是浮点除法。你可能是指整数除数/
@Paul Nope,我刚刚在写python2num/36
在Python2中是整数除法。好吧,我想我太习惯Python3:D了。不管怎样,你应该在答案中澄清这一点。答案很好,除了两个问题外,答案几乎是正确的。1.ZZZZXY的处理。它应该环绕并返回000000XY。2.000000XY未正确处理以返回000001XY。而是返回1XY。在我的答复中修正这两个问题。我仍然接受这个答复作为最好的答复。