-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 32bc842
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# == INSTRUCTIONS == | ||
# | ||
# Purpose: Manage a user's (valid) passwords | ||
# | ||
# Methods: | ||
# 1. Name: __init__ | ||
# Arguments: none | ||
# 2. Name: add | ||
# Purpose: add a password for a service IF it is valid, otherwise do nothing | ||
# Arguments: one string representing a service name, | ||
# one string representing a password | ||
# Returns: None | ||
# 3. Name: get_for_service | ||
# Arguments: one string representing a service name | ||
# Returns: the password for the given service, or None if none exists | ||
# 4. Name: list_services | ||
# Arguments: none | ||
# Returns: a list of all the services for which the user has a password | ||
# | ||
# A reminder of the validity rules: | ||
# 1. A password must be at least 8 characters long | ||
# 2. A password must contain at least one of each of the following: | ||
# - It must be longer than 7 characters (8 is fine) | ||
# - It must contain at least one of the following special characters: | ||
# `!`, `@`, `$`, `%` or `&` | ||
# | ||
# We recommend that you store the passwords in a dictionary, where the keys are | ||
# the service names and the values are the passwords. | ||
# | ||
# Example usage: | ||
# > password_manager = PasswordManager() | ||
# > password_manager.add('gmail', '12ab5!678') # Valid password | ||
# > password_manager.add('facebook', '$abc1234') # Valid password | ||
# > password_manager.add('twitter', '12345678') # Invalid password, so ignored | ||
# > password_manager.get_for_service('facebook') | ||
# '$abc1234' | ||
# > password_manager.get_for_service('not_real') | ||
# None | ||
# > password_manager.get_for_service('twitter') | ||
# None | ||
# > password_manager.list_services() | ||
# ['gmail', 'facebook'] | ||
# | ||
|
||
# == YOUR CODE == | ||
|
||
class PasswordManager(): | ||
|
||
def __init__(self): | ||
self.password_store = {} | ||
|
||
def add(self, service_name, password): | ||
self.service_name = service_name | ||
self.password = password | ||
specials = ["!", "@", "$", "%", "&"] | ||
if len(password) > 7: | ||
if any(c in specials for c in password): | ||
self.password_store[service_name] = password | ||
else: | ||
return False | ||
else: | ||
return False | ||
|
||
def get_for_service(self, service_name): | ||
if service_name in self.password_store: | ||
return self.password_store.get(service_name, self.password) | ||
else: | ||
return None | ||
|
||
|
||
def list_services(self): | ||
return self.password_store.keys() |