Python | Magical Increment

Magical increment

# -*- using:utf-8 -*-

import re


def magical_increment(start):
    pattern = re.compile(r'[^0-9A-Za-z]')

    while True:
        yield start

        temp = []
        add = 1
        carry = None

        for c in reversed(list(start)):
            if pattern.match(c):
                temp.append(c)
                continue

            ch = chr(ord(c) + add)
            if c <= '9' < ch:
                ch = '0'
                carry = '1'
            elif c <= 'Z' < ch:
                ch = carry = 'A'
            elif c <= 'z' < ch:
                ch = carry = 'a'
            else:
                add = 0
                carry = None
            temp.append(ch)

        if carry:
            temp.append(carry)

        start = ''.join(reversed(temp))


if __name__ == '__main__':
    gen = magical_increment('Az-8')
    for i in range(5):
        print(next(gen))

Only alphanumeric characters are incremented. The symbol remains the same.

Az-8
Az-9
Ba-0
Ba-1
Ba-2

Magical Add

# -*- using:utf-8 -*-

import re


def magical_add(start, step):
    pattern = re.compile(r'[^0-9A-Za-z]')

    while True:
        yield start

        temp = []
        add = step
        prev = ''

        for c in reversed(list(start)):
            if pattern.match(c):
                temp.append(c)
                continue

            if c <= '9':
                value = ord(c) + add % 10
                add //= 10
                prev = '0'
                if ord('9') < value:
                    value -= 10
                    add += 1
            elif c <= 'Z':
                value = ord(c) + add % 26
                add //= 26
                prev = 'A'
                if ord('Z') < value:
                    value -= 26
                    add += 1
            else:
                value = ord(c) + add % 26
                add //= 26
                prev = 'a'
                if ord('z') < value:
                    value -= 26
                    add += 1

            temp.append(chr(value))

        if add:
            if prev == '0':
                temp.extend(reversed(list(str(add))))
            elif prev == 'A' or prev == 'a':
                while add:
                    value = ord(prev) + add % 26 - 1
                    add //= 26
                    temp.append(chr(value))

        start = ''.join(reversed(temp))


if __name__ == '__main__':
    gen = magical_add('aZ-1', 5)
    for i in range(5):
        print(next(gen))

Only alphanumeric characters are added (5 is added in this example). The symbol remains the same.

aZ-1
aZ-6
bA-1
bA-6
bB-1

Recommended Posts

Python | Magical Increment
Python increment
Python
kafka python
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python tips
python function ①
Python basics
Python memo
ufo-> python (3)
Python comprehension
install python
Python Singleton
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
atCoder 173 Python
[Python] function
Python installation
python tips
Try python
Python memo
Python iterative
Python2 + word2vec
Python functions
Python tutorial
python underscore
Python summary
Start python
[Python] Sort
Note: Python
Python basics ③
python log
Python basics
[Scraping] Python scraping
Python update (2.6-> 2.7)
python memo
Python memorandum
Python # sort
ufo-> python
Python nslookup
python learning
Hannari Python 2020
[Rpmbuild] Python 3.7.3.
Prorate Python (1)
python memorandum
Download python
python memorandum
Python memo
started python