Skip to main content
Renamed functions, used any() (I was actually trying to keep it as simple as possible, but sure, why not...), fixed up some erroneous logic
Source Link
Yuushi
  • 11.1k
  • 2
  • 31
  • 66
def needs_lowerlower_required(text):
    pass

def needs_upperupper_required(text):
    pass

def needs_digitdigits_required(text):
    pass

def needs_extra_charslength_required(text):
    pass

def needs_specialspecial_required(text):
    pass

Now, Python has a string module that is (semi)deprecated (much of what was in it is now on string objects themselves), but that has some still useful things, namely, string.ascii_lowercase, string.ascii_uppercase, and string.digit. We can use these in the implementations for some of our functions. Let's just write them as simply as possible for now.

import string

def needs_lowerlower_required(text):
    for char in text:
        if char in string.ascii_lowercase:
            return 0
    return 1

def needs_upperupper_required(text):
    for char in text:
        if char in string.ascii_uppercase:
            return 0
    return 1

def needs_digitdigit_required(text):
    for char in text:
        if char in string.digits:
            return True0
    return False1
def needs_anynum_required(text, valuescharacters):
    forif charany((x in text:
       characters iffor charx in valuestext)):
            return True0
    return False1
def needs_lowerlower_required(text):
    return needs_anynum_required(text, string.ascii_lowercase)

And similarly for the needs_upperupper_required and needs_digitdigit_required.

SPECIAL_CHARS = '!@#$%^&*()-+'

def needs_specialspecial_required(text):
    return needs_anynum_required(text, SPECIAL_CHARS)

The last function we need to implement is needs_extra_charslength_required:

MINIMUM_LENGTH = 6

def needs_extra_charslength_required(text):
    if len(text) >= MINIMUM_LENGTH:
        return 0
    return MINIMUM_LENGTH - len(text)

Now we can stitch these all together within minimumNumber: (note that Python functions should be named using snake_case, so it should be minimum_number here - but I assume this function name was given to you, so I won't harp on about it too much).

def minimumNumber(n, passwordtext):
    # I'm not really sure why there's a n passed in as well?
    test_functions = [needs_lower[lower_required, needs_upperupper_required,
                      needs_digitdigits_required, needs_special,special_required]
    min_chars_required = sum([test_fn(text) for test_fn in test_functions])
    missing_length = length_required(text)
    if missing_length > needs_extra_chars]min_chars_required:
    extra_required = sum([test_fn(password) for test_fn inreturn test_functions])missing_length
    return extra_requiredmin_chars_required
def needs_lower(text):
    pass

def needs_upper(text):
    pass

def needs_digit(text):
    pass

def needs_extra_chars(text):
    pass

def needs_special(text):
    pass

Now, Python has a string module that is (semi)deprecated, but that has some still useful things, namely, string.ascii_lowercase, string.ascii_uppercase, and string.digit. We can use these in the implementations for some of our functions. Let's just write them as simply as possible for now.

import string

def needs_lower(text):
    for char in text:
        if char in string.ascii_lowercase:
            return 0
    return 1

def needs_upper(text):
    for char in text:
        if char in string.ascii_uppercase:
            return 0
    return 1

def needs_digit(text):
    for char in text:
        if char in string.digits:
            return True
    return False
def needs_any(text, values):
    for char in text:
        if char in values:
            return True
    return False
def needs_lower(text):
    return needs_any(text, string.ascii_lowercase)

And similarly for the needs_upper and needs_digit.

SPECIAL_CHARS = '!@#$%^&*()-+'

def needs_special(text):
    return needs_any(text, SPECIAL_CHARS)

The last function we need to implement is needs_extra_chars:

MINIMUM_LENGTH = 6

def needs_extra_chars(text):
    if len(text) >= MINIMUM_LENGTH:
        return 0
    return MINIMUM_LENGTH - len(text)

Now we can stitch these all together within minimumNumber:

def minimumNumber(n, password):
    # I'm not really sure why there's a n passed in as well?
    test_functions = [needs_lower, needs_upper,
                      needs_digit, needs_special,
                      needs_extra_chars]
    extra_required = sum([test_fn(password) for test_fn in test_functions])
    return extra_required
def lower_required(text):
    pass

def upper_required(text):
    pass

def digits_required(text):
    pass

def length_required(text):
    pass

def special_required(text):
    pass

Now, Python has a string module that is (semi)deprecated (much of what was in it is now on string objects themselves), but that has some still useful things, namely, string.ascii_lowercase, string.ascii_uppercase, and string.digit. We can use these in the implementations for some of our functions. Let's just write them as simply as possible for now.

import string

def lower_required(text):
    for char in text:
        if char in string.ascii_lowercase:
            return 0
    return 1

def upper_required(text):
    for char in text:
        if char in string.ascii_uppercase:
            return 0
    return 1

def digit_required(text):
    for char in text:
        if char in string.digits:
            return 0
    return 1
def num_required(text, characters):
    if any((x in characters for x in text)):
        return 0
    return 1
def lower_required(text):
    return num_required(text, string.ascii_lowercase)

And similarly for the upper_required and digit_required.

SPECIAL_CHARS = '!@#$%^&*()-+'

def special_required(text):
    return num_required(text, SPECIAL_CHARS)

The last function we need to implement is length_required:

MINIMUM_LENGTH = 6

def length_required(text):
    if len(text) >= MINIMUM_LENGTH:
        return 0
    return MINIMUM_LENGTH - len(text)

Now we can stitch these all together within minimumNumber (note that Python functions should be named using snake_case, so it should be minimum_number here - but I assume this function name was given to you, so I won't harp on about it too much).

def minimumNumber(text):
    test_functions = [lower_required, upper_required,
                      digits_required, special_required]
    min_chars_required = sum([test_fn(text) for test_fn in test_functions])
    missing_length = length_required(text)
    if missing_length > min_chars_required:
        return missing_length
    return min_chars_required
added 4 characters in body
Source Link
Yuushi
  • 11.1k
  • 2
  • 31
  • 66
def needs_lower(text):
    return needs_any(text, string.ascii_lowerascii_lowercase)
def needs_lower(text):
    return needs_any(text, string.ascii_lower)
def needs_lower(text):
    return needs_any(text, string.ascii_lowercase)
Fixed inconsistency in function names ("needs_special" & "contains_special")
Source Link
def minimumNumber(n, password):
    # I'm not really sure why there's a n passed in as well?
    test_functions = [needs_lower, needs_upper,
                      needs_digit, needs_extra_charsneeds_special,
                      contains_special]needs_extra_chars]
    extra_required = sum([test_fn(password) for test_fn in test_functions])
    return extra_required
def minimumNumber(n, password):
    # I'm not really sure why there's a n passed in as well?
    test_functions = [needs_lower, needs_upper,
                      needs_digit, needs_extra_chars,
                      contains_special]
    extra_required = sum([test_fn(password) for test_fn in test_functions])
    return extra_required
def minimumNumber(n, password):
    # I'm not really sure why there's a n passed in as well?
    test_functions = [needs_lower, needs_upper,
                      needs_digit, needs_special,
                      needs_extra_chars]
    extra_required = sum([test_fn(password) for test_fn in test_functions])
    return extra_required
deleted 2 characters in body
Source Link
Yuushi
  • 11.1k
  • 2
  • 31
  • 66
Loading
Source Link
Yuushi
  • 11.1k
  • 2
  • 31
  • 66
Loading