Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何增加一个特殊用途的字符串?_Python_Algorithm_Data Structures - Fatal编程技术网

Python:如何增加一个特殊用途的字符串?

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

考虑长度为8的特殊用途字符串,例如“A00000XY”。该字符串有以下限制

  • 长度=8
  • 最后两个字符有特殊的含义,应该保持原样
  • A-Z和0-9仅为有效字符。因此,正则表达式“^[A-Z0-9]{6}XY$”定义了字符串
  • 我如何实现一个函数,比如increment,当被调用时,它会将字符串增加1。因此,后续调用应如下所示:

    >>> 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是整数,所以好消息是您可以将值表示为整数

    要将字符串值转换为整数,请执行以下操作:

  • intValue=0
  • 循环遍历字符串中前八个字符中的每个字符
  • 将该字符传递给返回0到35之间等效整数的函数。我们称之为charValue
  • intValue+=intValue*36+charValue
  • 要将整数转换为字符串值,请执行以下操作:

  • stringValue=特殊字符

  • curDigit=intValue%36

  • intValue=intValue/36

  • 查找与intValue(0到Z)等价的字符串

  • 附加到stringValue的前面

  • 重复此操作,直到intValue<36。任何剩余字符都将为0

    显然,您还需要构建递增和递减方法


  • 灵感来源于@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的回复有两个问题

  • ZZZZXY的处理。它应该环绕并返回000000XY。它不应该溢出来。然而,我没有在我的问题中涵盖这一部分
  • 000000XY未正确处理以返回000001XY。而是 返回1XY
  • 在以下代码中修复了这些问题,这些代码借用了@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,我刚刚在写python2
    num/36
    在Python2中是整数除法。好吧,我想我太习惯Python3:D了。不管怎样,你应该在答案中澄清这一点。答案很好,除了两个问题外,答案几乎是正确的。1.ZZZZXY的处理。它应该环绕并返回000000XY。2.000000XY未正确处理以返回000001XY。而是返回1XY。在我的答复中修正这两个问题。我仍然接受这个答复作为最好的答复。