Software Engineering

Writing a Sleigh Authentication in Python

Writing a Sleigh Authentication in Python
Written by admin


The problem

Christmas is coming and many individuals dreamed of getting a experience with Santa’s sleigh. However, in fact, solely Santa himself is allowed to make use of this excellent transportation. And to be able to be certain, that solely he can board the sleigh, there’s an authentication mechanism.

Your job is to implement the authenticate() methodology of the sleigh, which takes the identify of the individual, who desires to board the sleigh and a secret password. If, and provided that, the identify equals “Santa Claus” and the password is “Ho Ho Ho!” (sure, even Santa has a secret password with uppercase and lowercase letters and particular characters :D), the return worth should be true. In any other case, it ought to return false.

Examples:

sleigh = Sleigh()
sleigh.authenticate('Santa Claus', 'Ho Ho Ho!') # should return True

sleigh.authenticate('Santa', 'Ho Ho Ho!') # should return False
sleigh.authenticate('Santa Claus', 'Ho Ho!') # should return False
sleigh.authenticate('jhoffner', 'CodeWars') # Nope, even Jake will not be allowed to make use of the sleigh ;)

The answer in Python code

Possibility 1:

class Sleigh(object):
    def authenticate(self, identify, password):
        return identify == 'Santa Claus' and password == 'Ho Ho Ho!'

Possibility 2:

class Sleigh(object):
    def authenticate(self, identify, password):
        return (identify, password) == ('Santa Claus', 'Ho Ho Ho!')

Possibility 3:

class Sleigh(object):

    def __init__(self):
        self.known_credentials = {'Santa Claus': 'Ho Ho Ho!'}
        
    def authenticate(self, identify, password):
        if identify in self.known_credentials.keys() and 
          password == self.known_credentials.get(identify):
            return True
        else:
            return False

Check instances to validate our answer

check.describe("Santa's Sleigh")

sleigh = Sleigh()
def test_credentials(identify, password, anticipated):
    check.assert_equals(sleigh.authenticate(identify, password), anticipated, 'Examined identify %s and password %s' % (identify,password))

check.it('should authenticate with appropriate credentials')
test_credentials('Santa Claus', 'Ho Ho Ho!', True)
    
check.it('Should not authenticate with incorrect credentials')
test_credentials('Santa', 'Ho Ho Ho!', False)
test_credentials('Santa Claus', 'Ho Ho!', False)
test_credentials('jhoffner', 'CodeWars', False)

About the author

admin

Leave a Comment