# Create a function to generate a random, 8-character password.
# It should satisfy the following requirements:
# 1) There should be exactly two characters from each of the following categories:
# - Uppercase letters
# - Lowercase letters
# - Numerals 0 - 9
# - Special Characters from the string “!@#$%^&*”
# 2) No two character categories should be adjacent.
# bad example: AXyu74$^
# 3) The categories should be randomly ordered.
# good example: 8b&U6Nz! NLSUNULS
# */
import random, string
def generate_password():
store = {}
for category in 'ULSN':
store[category] = []
for i in range(2):
store['U'].append(random.choice(string.ascii_letters).upper())
store['L'].append(random.choice(string.ascii_letters).lower())
store['N'].append(str(random.randint(0,9)))
store['S'].append("!@#$%^&*"[random.randrange(0,7) + 1])
print("".join([item for sublist in store.values() for item in sublist]))
for item in store.items():
print(item)
# shuffle and eliminate adjacency
generate_password()
有一个有四个键的字典。每个键映射到不同的类别,每个键都有一个包含 2 个字符的列表作为值。
你如何打乱字典以随机顺序构建一个字符串,这样没有键是相邻的?
目标是有效地返回一个没有相邻值的 8 个字符长的字符串。
示例和测试用例在问题陈述中