Write your code: Random Password
Contents
Write your code: Random Password#
Write a function that works as a Random Strong Password Generator.
Rules#
Function returns a string
Length between 8 and 24 characters
At least one uppercase letter (
A-Z
)At least one lowercase letter (
a-z
)At least one digit (
0-9
)It contains at least one special character (
!@#$%^&*()+=
)Maximum of 2 repeated characters (regardless of order)
Always return a different password
You can use any
random function
from therandom
module
Useful Links#
-
Specially:
isdigit()
,islower()
,isupper()
,join()
-
Specially:
all()
,any()
,len()
Function Definition#
from random import # add function
def password_generator():
# write your code here
File "/tmp/ipykernel_2426/4144797343.py", line 1
from random import # add function
^
SyntaxError: invalid syntax
Testing#
Check if your function returns the expected value using the cell below.
import unittest
class UnitTests(unittest.TestCase):
def setUp(self):
self.password = password_generator()
self.passwords = [password_generator() for _ in range(10)]
def test_password_type(self):
self.assertTrue(isinstance(self.password, str), 'The function should return a string')
def test_password_min_size(self):
self.assertGreater(len(self.password), 7, "The function should return a password with minimum length of 8 characters")
def test_password_max_size(self):
self.assertLess(len(self.password), 25, "The function should return a password with maximum length of 24 characters")
def test_upper(self):
self.assertTrue(any(char.isupper() for char in self.password), "The function should return a password with at least 1 upper-case letter")
def test_lower(self):
self.assertTrue(any(char.islower() for char in self.password), "The function should return a password with at least 1 lower-case letter")
def test_numeric(self):
self.assertTrue(any(char.isdigit() for char in self.password), "The function should return a password with at least 1 number")
def test_special(self):
self.assertTrue(any(char in "!@#$%^&*()+=" for char in self.password), "The function should return a password with at least 1 special character")
def test_repeated_char(self):
self.assertTrue(all(self.password.count(char)<3 for char in self.password), "The function should return a password with a maximum of 2 repeated character")
def test_unique_password(self):
self.assertEqual(len(self.passwords), len(set(self.passwords)), "The function should return an unique password")
unittest.main(argv=[''], verbosity=2,exit=False)