1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] python regex character "-" detection problem [duplicate]

Discussão em 'Python' iniciado por Stack, Setembro 12, 2024.

  1. Stack

    Stack Membro Participativo

    I was trying to solve a coding problem and notice something unusual in python. The coding problem is:

    A website considers a password to be strong if it satisfies the following criteria:

    Its length is at least . 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: !@#$%^&*()-+

    Given the string user typed, need to find the minimum number of characters that must add to make password strong

    Example:

    This password is 5 characters long and is missing an uppercase and a special character. The minimum number of characters to add is .

    This password is 5 characters long and has at least one of each character type. The minimum number of characters to add is 1.

    Returns int: the minimum number of characters to add

    Input Format The first line contains an integer , the length of the password.

    The second line contains the password string. Each character is either a lowercase/uppercase English alphabet, a digit, or a special character.

    Constraints

    All characters in are in [a-z], [A-Z], [0-9], or [!@#$%^&*()-+ ].

    This is my solution:

    def minimumNumber(n, password):
    # Return the minimum number of characters to make the password strong

    char_req = 0

    reg_dig = re.findall("[0-9]", password)
    regex = '[!@#$%^&*()-+]'
    reg_sp = re.findall(regex, password)
    regex = '[a-z]'
    reg_l_case = re.findall(regex, password)
    regex = '[A-Z]'
    reg_u_case = re.findall(regex, password)


    if not reg_dig:
    char_req = char_req + 1
    if not reg_sp:
    char_req = char_req + 1
    if not reg_l_case:
    char_req = char_req + 1
    if not reg_u_case:
    char_req = char_req + 1
    print(char_req)
    if (n + char_req) < 6:
    char_req = char_req + (6 - (n + char_req))

    return char_req


    The solution works for almost all the cases except one test case where input is "AUzs-nV"

    The result supposed to return 1 but it returns 2 . I then looked and found the following line is not detecting the "-" symbol

    regex = '[!@#$%^&*()-+]'
    reg_sp = re.findall(regex, password)


    When I change the expression from [!@#$%^&()-+] to [-!@#$%^&()+] or [!-@#$%^&*()+] the above solution works for the specific case and "-" is detected.

    I know python counts "-" as from to to but i cant find a reason why [!-@#$%^&()+] would work and [!@#$%^&()-+] does not. I read the re library docs in "https://docs.python.org/2/library/re.html#regular-expression-syntax" but did not find anything. I know there are other ways to solve the problem.

    If anyone would explain what's going on, it will be helpful

    Continue reading...

Compartilhe esta Página