Home > Enterprise >  i saw this code in hacker rank discussion section and i want to understand this code
i saw this code in hacker rank discussion section and i want to understand this code

Time:10-31

s = input()
a=b=c=d=e=False
    for i in s:
        a |= i.isalnum()
        b |= i.isalpha()
        c |= i.isdigit()
        d |= i.islower()
        e |= i.isupper()
    print(a,b,c,d,e,sep='\n')

i want to understand this code. this is the solution of a hacker rank problem i saw in discussion section, question says In the first line, print True if has any alphanumeric characters. Otherwise, print False. In the second line, print True if has any alphabetical characters. Otherwise, print False. In the third line, print True if has any digits. Otherwise, print False. In the fourth line, print True if has any lowercase characters. Otherwise, print False. In the fifth line, print True if has any uppercase characters. Otherwise, print False.

i use this code in my solution and it work and i want to how i read this

CodePudding user response:

Here is a step by step explanation going through your code.

Read the input of the user: s = input()

Initialize all the values to False, i.e. in the beginning we assume that none of the conditions the problem asks for is fulfilled: a=b=c=d=e=False.

The for loop iterates through all the characters in the string an checks the conditions. For example a |= i.isalnum() is equivalent to a = a or i.isalnum(). In the beginning when a == False you need one alpha-numeric character to make this expression return True. Once that happens though, a stays True because True or False gives True. In other words this is the way to check that at leas one letter fulfills the condition. The same goes for all the other 4 boolean variables

Finally print the results: print(a,b,c,d,e,sep='\n'). This means that every one of the 5 variables should be printed with a \n between them, which is the newline character. So each one of the variables will be printed in its own line.

  • Related