Skip to content

Commit 67aa3cf

Browse files
akankshamahajan99cclauss
authored andcommitted
Added alternative way to generate password in password_generator.py (TheAlgorithms#1415)
1 parent a06995a commit 67aa3cf

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

other/password_generator.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""Password generator allows you to generate a random password of length N."""
2-
from random import choice
2+
from random import choice, shuffle
33
from string import ascii_letters, digits, punctuation
44

55

@@ -26,9 +26,22 @@ def password_generator(length=8):
2626
def alternative_password_generator(ctbi, i):
2727
# Password generator = full boot with random_number, random_letters, and
2828
# random_character FUNCTIONS
29-
pass # Put your code here...
30-
29+
# Put your code here...
30+
i = i - len(ctbi)
31+
quotient = int(i / 3)
32+
remainder = i % 3
33+
#chars = ctbi + random_letters(ascii_letters, i / 3 + remainder) + random_number(digits, i / 3) + random_characters(punctuation, i / 3)
34+
chars = ctbi + random(ascii_letters, quotient + remainder) + random(digits, quotient) + random(punctuation, quotient)
35+
chars = list(chars)
36+
shuffle(chars)
37+
return "".join(chars)
3138

39+
40+
#random is a generalised function for letters, characters and numbers
41+
def random(ctbi, i):
42+
return "".join(choice(ctbi) for x in range(i))
43+
44+
3245
def random_number(ctbi, i):
3346
pass # Put your code here...
3447

@@ -43,7 +56,9 @@ def random_characters(ctbi, i):
4356

4457
def main():
4558
length = int(input("Please indicate the max length of your password: ").strip())
59+
ctbi = input("Please indicate the characters that must be in your password: ").strip()
4660
print("Password generated:", password_generator(length))
61+
print("Alternative Password generated:", alternative_password_generator(ctbi, length))
4762
print("[If you are thinking of using this passsword, You better save it.]")
4863

4964

0 commit comments

Comments
 (0)