2

How can I make this work with alpha_range(A, ZZ)?

  • right now it only work until Z

Code:

def alpha_range(start, stop):
    """ Returns chars between start char and stop char(A,D -> A,B,C,D).
    :param start: start char
    :param stop: stop char
    :return: list of chars
    """
    return [chr(x) for x in range(ord(start), ord(stop)+1)]
Alex L
  • 8,419
  • 6
  • 43
  • 51

1 Answers1

3

You can easily make a bidirectional mapping between A-ZZ and numbers. This actually is pretty similar to a numeric system with different characters to represent the digits.

BASE = ord('Z') - ord('A') + 1

def to_number(str_input):
    res = 0
    for letter in str_input:
        res = res * BASE + ord(letter) - ord('A') + 1
    return res

def to_str(int_input):
    res = ''
    while int_input > 0:
        int_input -= 1
        res = res + chr(int_input % BASE + ord('A'))
        int_input //= BASE
    return res[::-1]

Now you can replace ord and chr with this functions.

nikihub
  • 311
  • 1
  • 5