mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Added alternative way to generate password in password_generator.py (#1415)
This commit is contained in:
parent
a06995a686
commit
67aa3cfb4d
@ -1,5 +1,5 @@
|
|||||||
"""Password generator allows you to generate a random password of length N."""
|
"""Password generator allows you to generate a random password of length N."""
|
||||||
from random import choice
|
from random import choice, shuffle
|
||||||
from string import ascii_letters, digits, punctuation
|
from string import ascii_letters, digits, punctuation
|
||||||
|
|
||||||
|
|
||||||
@ -26,7 +26,20 @@ def password_generator(length=8):
|
|||||||
def alternative_password_generator(ctbi, i):
|
def alternative_password_generator(ctbi, i):
|
||||||
# Password generator = full boot with random_number, random_letters, and
|
# Password generator = full boot with random_number, random_letters, and
|
||||||
# random_character FUNCTIONS
|
# random_character FUNCTIONS
|
||||||
pass # Put your code here...
|
# Put your code here...
|
||||||
|
i = i - len(ctbi)
|
||||||
|
quotient = int(i / 3)
|
||||||
|
remainder = i % 3
|
||||||
|
#chars = ctbi + random_letters(ascii_letters, i / 3 + remainder) + random_number(digits, i / 3) + random_characters(punctuation, i / 3)
|
||||||
|
chars = ctbi + random(ascii_letters, quotient + remainder) + random(digits, quotient) + random(punctuation, quotient)
|
||||||
|
chars = list(chars)
|
||||||
|
shuffle(chars)
|
||||||
|
return "".join(chars)
|
||||||
|
|
||||||
|
|
||||||
|
#random is a generalised function for letters, characters and numbers
|
||||||
|
def random(ctbi, i):
|
||||||
|
return "".join(choice(ctbi) for x in range(i))
|
||||||
|
|
||||||
|
|
||||||
def random_number(ctbi, i):
|
def random_number(ctbi, i):
|
||||||
@ -43,7 +56,9 @@ def random_characters(ctbi, i):
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
length = int(input("Please indicate the max length of your password: ").strip())
|
length = int(input("Please indicate the max length of your password: ").strip())
|
||||||
|
ctbi = input("Please indicate the characters that must be in your password: ").strip()
|
||||||
print("Password generated:", password_generator(length))
|
print("Password generated:", password_generator(length))
|
||||||
|
print("Alternative Password generated:", alternative_password_generator(ctbi, length))
|
||||||
print("[If you are thinking of using this passsword, You better save it.]")
|
print("[If you are thinking of using this passsword, You better save it.]")
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user