Find the minimum number of characters to add to make password strong

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












This is the criteria:



Its length is at least 6. It contains at least one digit. It contains at least one lowercase English character. It contains at least one uppercase English character. It contains at least one special character. The special characters are: !@#$%^&*()-+



My code is working for all test cases but I'm afraid it seems way longer than necessary. I'm trying to trim it down.



import math
import os
import random
import re
import sys

# Complete the minimumNumber function below.
def minimumNumber(n, password):
# Return the minimum number of characters to make the password strong
flag = 0
if len(password) >= 6:
passRegex = re.compile(r"^(?=.*[d])")
mo = passRegex.search(password)
if mo is None:
flag+=1
passRegex = re.compile(r"^(?=.*[A-Z])")
mo = passRegex.search(password)
if mo is None:
flag+=1
passRegex = re.compile(r"^(?=.*[a-z])")
mo = passRegex.search(password)
if mo is None:
flag+=1
passRegex = re.compile(r'[!@#$%^&*()-+]')
mo = passRegex.search(password)
if mo is None:
flag+=1
return flag
else:
if any(x in "0123456789" for x in password) is False:
flag+=1
if any(x in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for x in password) is False:
flag+=1
if any(x in "abcdefghijklmnopqrstuvwxyz" for x in password) is False:
flag+=1
if any(x in "!@#$%^&*()-+" for x in password) is False:
flag+=1
if (flag+len(password)<6):
return 6-len(password)
if (flag+len(password)>=6):
return flag


if __name__ == '__main__':

n = int(input())

password = input()

answer = minimumNumber(n, password)


Any advice is appreciated.










share|improve this question







New contributor




db18 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    up vote
    2
    down vote

    favorite












    This is the criteria:



    Its length is at least 6. It contains at least one digit. It contains at least one lowercase English character. It contains at least one uppercase English character. It contains at least one special character. The special characters are: !@#$%^&*()-+



    My code is working for all test cases but I'm afraid it seems way longer than necessary. I'm trying to trim it down.



    import math
    import os
    import random
    import re
    import sys

    # Complete the minimumNumber function below.
    def minimumNumber(n, password):
    # Return the minimum number of characters to make the password strong
    flag = 0
    if len(password) >= 6:
    passRegex = re.compile(r"^(?=.*[d])")
    mo = passRegex.search(password)
    if mo is None:
    flag+=1
    passRegex = re.compile(r"^(?=.*[A-Z])")
    mo = passRegex.search(password)
    if mo is None:
    flag+=1
    passRegex = re.compile(r"^(?=.*[a-z])")
    mo = passRegex.search(password)
    if mo is None:
    flag+=1
    passRegex = re.compile(r'[!@#$%^&*()-+]')
    mo = passRegex.search(password)
    if mo is None:
    flag+=1
    return flag
    else:
    if any(x in "0123456789" for x in password) is False:
    flag+=1
    if any(x in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for x in password) is False:
    flag+=1
    if any(x in "abcdefghijklmnopqrstuvwxyz" for x in password) is False:
    flag+=1
    if any(x in "!@#$%^&*()-+" for x in password) is False:
    flag+=1
    if (flag+len(password)<6):
    return 6-len(password)
    if (flag+len(password)>=6):
    return flag


    if __name__ == '__main__':

    n = int(input())

    password = input()

    answer = minimumNumber(n, password)


    Any advice is appreciated.










    share|improve this question







    New contributor




    db18 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      This is the criteria:



      Its length is at least 6. It contains at least one digit. It contains at least one lowercase English character. It contains at least one uppercase English character. It contains at least one special character. The special characters are: !@#$%^&*()-+



      My code is working for all test cases but I'm afraid it seems way longer than necessary. I'm trying to trim it down.



      import math
      import os
      import random
      import re
      import sys

      # Complete the minimumNumber function below.
      def minimumNumber(n, password):
      # Return the minimum number of characters to make the password strong
      flag = 0
      if len(password) >= 6:
      passRegex = re.compile(r"^(?=.*[d])")
      mo = passRegex.search(password)
      if mo is None:
      flag+=1
      passRegex = re.compile(r"^(?=.*[A-Z])")
      mo = passRegex.search(password)
      if mo is None:
      flag+=1
      passRegex = re.compile(r"^(?=.*[a-z])")
      mo = passRegex.search(password)
      if mo is None:
      flag+=1
      passRegex = re.compile(r'[!@#$%^&*()-+]')
      mo = passRegex.search(password)
      if mo is None:
      flag+=1
      return flag
      else:
      if any(x in "0123456789" for x in password) is False:
      flag+=1
      if any(x in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for x in password) is False:
      flag+=1
      if any(x in "abcdefghijklmnopqrstuvwxyz" for x in password) is False:
      flag+=1
      if any(x in "!@#$%^&*()-+" for x in password) is False:
      flag+=1
      if (flag+len(password)<6):
      return 6-len(password)
      if (flag+len(password)>=6):
      return flag


      if __name__ == '__main__':

      n = int(input())

      password = input()

      answer = minimumNumber(n, password)


      Any advice is appreciated.










      share|improve this question







      New contributor




      db18 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      This is the criteria:



      Its length is at least 6. It contains at least one digit. It contains at least one lowercase English character. It contains at least one uppercase English character. It contains at least one special character. The special characters are: !@#$%^&*()-+



      My code is working for all test cases but I'm afraid it seems way longer than necessary. I'm trying to trim it down.



      import math
      import os
      import random
      import re
      import sys

      # Complete the minimumNumber function below.
      def minimumNumber(n, password):
      # Return the minimum number of characters to make the password strong
      flag = 0
      if len(password) >= 6:
      passRegex = re.compile(r"^(?=.*[d])")
      mo = passRegex.search(password)
      if mo is None:
      flag+=1
      passRegex = re.compile(r"^(?=.*[A-Z])")
      mo = passRegex.search(password)
      if mo is None:
      flag+=1
      passRegex = re.compile(r"^(?=.*[a-z])")
      mo = passRegex.search(password)
      if mo is None:
      flag+=1
      passRegex = re.compile(r'[!@#$%^&*()-+]')
      mo = passRegex.search(password)
      if mo is None:
      flag+=1
      return flag
      else:
      if any(x in "0123456789" for x in password) is False:
      flag+=1
      if any(x in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for x in password) is False:
      flag+=1
      if any(x in "abcdefghijklmnopqrstuvwxyz" for x in password) is False:
      flag+=1
      if any(x in "!@#$%^&*()-+" for x in password) is False:
      flag+=1
      if (flag+len(password)<6):
      return 6-len(password)
      if (flag+len(password)>=6):
      return flag


      if __name__ == '__main__':

      n = int(input())

      password = input()

      answer = minimumNumber(n, password)


      Any advice is appreciated.







      python regex






      share|improve this question







      New contributor




      db18 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      db18 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      db18 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 4 hours ago









      db18

      132




      132




      New contributor




      db18 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      db18 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      db18 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          Code length is one factor in readability, but it isn't the only factor. "Is this code easy to read and modify" is a more important question to ask.



          It looks like you've gotten a method skeleton here, which kind of leads you down the path of stuffing everything in one function, which is a bit of a shame. Let's look at what we actually need here.



          1. Length of at least 6

          2. One lowercase English character

          3. One uppercase English character

          4. One digit

          5. One special character

          What if we split this up into a bunch of separate functions, which each do one thing. We'll just write the function names down for now, and leave the implementation blank.



          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


          There's quite a bit of repetition here that we can factor out.



          def needs_any(text, values):
          for char in text:
          if char in values:
          return True
          return False


          Then our previous functions can just call this:



          def needs_lower(text):
          return needs_any(text, string.ascii_lower)


          And similarly for the needs_upper and needs_special.



          In fact, we can do this with our special characters as well:



          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_extra_chars,
          contains_special]
          extra_required = sum([test_fn(password) for test_fn in test_functions])
          return extra_required


          The benefit of doing this is the fact that we can easily add extra checks. What if a new requirement comes along? We can just create a new function an add it to test_functions; the amount of existing code we have to touch is minimal.



          Just to drive home the point about size vs readability, here is a (slightly) code golfed version:



          import string

          def minimumRequired(password):
          if len(password) < 6:
          return 6 - len(password)
          unique_pw = frozenset(password)
          char_sets = (frozenset(string.ascii_lowercase), frozenset(string.ascii_uppercase),
          frozenset(string.digits), frozenset('!@#$%^&*()-+'))
          required_extra = 0
          for char_set in char_sets:
          if not char_set & unique_pw:
          required_extra += 1
          return required_extra


          This (probably - I haven't really tested it) satisfies the requirements, but how do I make changes to it? Is it obvious how it does what it does (I'd argue not really).






          share|improve this answer




















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );






            db18 is a new contributor. Be nice, and check out our Code of Conduct.









             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f203839%2ffind-the-minimum-number-of-characters-to-add-to-make-password-strong%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            Code length is one factor in readability, but it isn't the only factor. "Is this code easy to read and modify" is a more important question to ask.



            It looks like you've gotten a method skeleton here, which kind of leads you down the path of stuffing everything in one function, which is a bit of a shame. Let's look at what we actually need here.



            1. Length of at least 6

            2. One lowercase English character

            3. One uppercase English character

            4. One digit

            5. One special character

            What if we split this up into a bunch of separate functions, which each do one thing. We'll just write the function names down for now, and leave the implementation blank.



            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


            There's quite a bit of repetition here that we can factor out.



            def needs_any(text, values):
            for char in text:
            if char in values:
            return True
            return False


            Then our previous functions can just call this:



            def needs_lower(text):
            return needs_any(text, string.ascii_lower)


            And similarly for the needs_upper and needs_special.



            In fact, we can do this with our special characters as well:



            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_extra_chars,
            contains_special]
            extra_required = sum([test_fn(password) for test_fn in test_functions])
            return extra_required


            The benefit of doing this is the fact that we can easily add extra checks. What if a new requirement comes along? We can just create a new function an add it to test_functions; the amount of existing code we have to touch is minimal.



            Just to drive home the point about size vs readability, here is a (slightly) code golfed version:



            import string

            def minimumRequired(password):
            if len(password) < 6:
            return 6 - len(password)
            unique_pw = frozenset(password)
            char_sets = (frozenset(string.ascii_lowercase), frozenset(string.ascii_uppercase),
            frozenset(string.digits), frozenset('!@#$%^&*()-+'))
            required_extra = 0
            for char_set in char_sets:
            if not char_set & unique_pw:
            required_extra += 1
            return required_extra


            This (probably - I haven't really tested it) satisfies the requirements, but how do I make changes to it? Is it obvious how it does what it does (I'd argue not really).






            share|improve this answer
























              up vote
              2
              down vote



              accepted










              Code length is one factor in readability, but it isn't the only factor. "Is this code easy to read and modify" is a more important question to ask.



              It looks like you've gotten a method skeleton here, which kind of leads you down the path of stuffing everything in one function, which is a bit of a shame. Let's look at what we actually need here.



              1. Length of at least 6

              2. One lowercase English character

              3. One uppercase English character

              4. One digit

              5. One special character

              What if we split this up into a bunch of separate functions, which each do one thing. We'll just write the function names down for now, and leave the implementation blank.



              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


              There's quite a bit of repetition here that we can factor out.



              def needs_any(text, values):
              for char in text:
              if char in values:
              return True
              return False


              Then our previous functions can just call this:



              def needs_lower(text):
              return needs_any(text, string.ascii_lower)


              And similarly for the needs_upper and needs_special.



              In fact, we can do this with our special characters as well:



              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_extra_chars,
              contains_special]
              extra_required = sum([test_fn(password) for test_fn in test_functions])
              return extra_required


              The benefit of doing this is the fact that we can easily add extra checks. What if a new requirement comes along? We can just create a new function an add it to test_functions; the amount of existing code we have to touch is minimal.



              Just to drive home the point about size vs readability, here is a (slightly) code golfed version:



              import string

              def minimumRequired(password):
              if len(password) < 6:
              return 6 - len(password)
              unique_pw = frozenset(password)
              char_sets = (frozenset(string.ascii_lowercase), frozenset(string.ascii_uppercase),
              frozenset(string.digits), frozenset('!@#$%^&*()-+'))
              required_extra = 0
              for char_set in char_sets:
              if not char_set & unique_pw:
              required_extra += 1
              return required_extra


              This (probably - I haven't really tested it) satisfies the requirements, but how do I make changes to it? Is it obvious how it does what it does (I'd argue not really).






              share|improve this answer






















                up vote
                2
                down vote



                accepted







                up vote
                2
                down vote



                accepted






                Code length is one factor in readability, but it isn't the only factor. "Is this code easy to read and modify" is a more important question to ask.



                It looks like you've gotten a method skeleton here, which kind of leads you down the path of stuffing everything in one function, which is a bit of a shame. Let's look at what we actually need here.



                1. Length of at least 6

                2. One lowercase English character

                3. One uppercase English character

                4. One digit

                5. One special character

                What if we split this up into a bunch of separate functions, which each do one thing. We'll just write the function names down for now, and leave the implementation blank.



                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


                There's quite a bit of repetition here that we can factor out.



                def needs_any(text, values):
                for char in text:
                if char in values:
                return True
                return False


                Then our previous functions can just call this:



                def needs_lower(text):
                return needs_any(text, string.ascii_lower)


                And similarly for the needs_upper and needs_special.



                In fact, we can do this with our special characters as well:



                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_extra_chars,
                contains_special]
                extra_required = sum([test_fn(password) for test_fn in test_functions])
                return extra_required


                The benefit of doing this is the fact that we can easily add extra checks. What if a new requirement comes along? We can just create a new function an add it to test_functions; the amount of existing code we have to touch is minimal.



                Just to drive home the point about size vs readability, here is a (slightly) code golfed version:



                import string

                def minimumRequired(password):
                if len(password) < 6:
                return 6 - len(password)
                unique_pw = frozenset(password)
                char_sets = (frozenset(string.ascii_lowercase), frozenset(string.ascii_uppercase),
                frozenset(string.digits), frozenset('!@#$%^&*()-+'))
                required_extra = 0
                for char_set in char_sets:
                if not char_set & unique_pw:
                required_extra += 1
                return required_extra


                This (probably - I haven't really tested it) satisfies the requirements, but how do I make changes to it? Is it obvious how it does what it does (I'd argue not really).






                share|improve this answer












                Code length is one factor in readability, but it isn't the only factor. "Is this code easy to read and modify" is a more important question to ask.



                It looks like you've gotten a method skeleton here, which kind of leads you down the path of stuffing everything in one function, which is a bit of a shame. Let's look at what we actually need here.



                1. Length of at least 6

                2. One lowercase English character

                3. One uppercase English character

                4. One digit

                5. One special character

                What if we split this up into a bunch of separate functions, which each do one thing. We'll just write the function names down for now, and leave the implementation blank.



                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


                There's quite a bit of repetition here that we can factor out.



                def needs_any(text, values):
                for char in text:
                if char in values:
                return True
                return False


                Then our previous functions can just call this:



                def needs_lower(text):
                return needs_any(text, string.ascii_lower)


                And similarly for the needs_upper and needs_special.



                In fact, we can do this with our special characters as well:



                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_extra_chars,
                contains_special]
                extra_required = sum([test_fn(password) for test_fn in test_functions])
                return extra_required


                The benefit of doing this is the fact that we can easily add extra checks. What if a new requirement comes along? We can just create a new function an add it to test_functions; the amount of existing code we have to touch is minimal.



                Just to drive home the point about size vs readability, here is a (slightly) code golfed version:



                import string

                def minimumRequired(password):
                if len(password) < 6:
                return 6 - len(password)
                unique_pw = frozenset(password)
                char_sets = (frozenset(string.ascii_lowercase), frozenset(string.ascii_uppercase),
                frozenset(string.digits), frozenset('!@#$%^&*()-+'))
                required_extra = 0
                for char_set in char_sets:
                if not char_set & unique_pw:
                required_extra += 1
                return required_extra


                This (probably - I haven't really tested it) satisfies the requirements, but how do I make changes to it? Is it obvious how it does what it does (I'd argue not really).







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered 2 hours ago









                Yuushi

                9,79422259




                9,79422259




















                    db18 is a new contributor. Be nice, and check out our Code of Conduct.









                     

                    draft saved


                    draft discarded


















                    db18 is a new contributor. Be nice, and check out our Code of Conduct.












                    db18 is a new contributor. Be nice, and check out our Code of Conduct.











                    db18 is a new contributor. Be nice, and check out our Code of Conduct.













                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f203839%2ffind-the-minimum-number-of-characters-to-add-to-make-password-strong%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    List of Gilmore Girls characters

                    Confectionery