gpt4 book ai didi

python - 如何在python中创建一个4位数的密码?

转载 作者:行者123 更新时间:2023-12-04 10:36:00 24 4
gpt4 key购买 nike

我正在设计一个 4 位字母数字键,其中字符从 A001、A002 到 Z999。在 Z999 之后它转到 AA01 到 AA99。在 AA99 之后,它转到 AB00 或 AB01。我面临的问题是当我增加范围时,在 AA99 之后它不会到达 AB01。它再次以 AA01 开始,以 AA99 结束,同样的事情不断重复。任何有关这方面的帮助将不胜感激。

非常感谢!

我试过的 -

    def excel_format(num):
res = ""
while num:
mod = (num - 1) % 26
res = chr(65 + mod) + res
num = (num - mod) // 26
return res

def full_format(num, d=3):
chars = num // (10**d-1) + 1 # this becomes A..ZZZ
c = excel_format(chars)
digit = num % (10**d-1) + 1 # this becomes 001..999

return c + str(digit).zfill(d+1-len(c))[len(c)-d-1:]

for i in range(0, 28000):
print(full_format(i, d=3))

最佳答案

下面从 A001-Z999、AA01-ZZ99 转换 0-92897 并打印翻转点以供演示:

def full_format(i):
# limit of first range is 26 letters (A-Z) times 999 numbers (001-999)
if i < 26 * 999:
c,n = divmod(i,999) # quotient c is index of letter 0-25, remainder n is 0-998
c = chr(ord('A') + c) # compute letter
n += 1
return f'{c}{n:03}'
# After first range, second range is 26 letters times 26 letters * 99 numbers (01-99)
elif i < 26*999 + 26*26*99:
i -= 26*999 # remove first range offset
cc,n = divmod(i,99) # remainder n is 0-98, use quotient cc to compute two letters
c1,c2 = divmod(cc,26) # c1 is index of first letter, c2 is index of second letter
c1 = chr(ord('A') + c1) # compute first letter
c2 = chr(ord('A') + c2) # compute second letter
n += 1
return f'{c1}{c2}{n:02}'
else:
raise OverflowError(f'limit is {26*999+26*26*99}')

# Generate all possibilities into a list for demonstration.
L = [full_format(i) for i in range(92898)]

print(L[0])
print(L[999-1])
print(L[999])
print(L[26*999-1])
print(L[26*999])
print(L[26*999+99])
print(L[26*999+99*26-1])
print(L[26*999+99*26])
print(L[26*999+99*26*26-1])
full_format(92898) # overflow
A001
A999
B001
Z999
AA01
AB01
AZ99
BA01
ZZ99
Traceback (most recent call last):
File "C:\test.py", line 31, in <module>
full_format(92898) # overflow
File "C:\test.py", line 18, in full_format
raise OverflowError(f'limit is {26*999+26*26*99}')
OverflowError: limit is 92898

关于python - 如何在python中创建一个4位数的密码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60182330/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com