Sunday, March 17, 2019

String Validators - Hacker Rank Solution

Python has built-in string validation methods for basic data. It can check if a string is composed of alphabetical characters, alphanumeric characters, digits, etc.

str.isalnum()
This method checks if all the characters of a string are alphanumeric (a-z, A-Z and 0-9).
>>> print 'ab123'.isalnum()
True
>>> print 'ab123#'.isalnum()
False

str.isalpha()
This method checks if all the characters of a string are alphabetical (a-z and A-Z).
>>> print 'abcD'.isalpha()
True
>>> print 'abcd1'.isalpha()
False

str.isdigit()
This method checks if all the characters of a string are digits (0-9).
>>> print '1234'.isdigit()
True
>>> print '123edsd'.isdigit()
False

str.islower()
This method checks if all the characters of a string are lowercase characters (a-z).
>>> print 'abcd123#'.islower()
True
>>> print 'Abcd123#'.islower()
False

str.isupper()
This method checks if all the characters of a string are uppercase characters (A-Z).
>>> print 'ABCD123#'.isupper()
True
>>> print 'Abcd123#'.isupper()
False

Task
You are given a string .
Your task is to find out if the string  contains: alphanumeric characters, alphabetical characters, digits, lowercase and uppercase characters.
Input Format
A single line containing a string .
Constraints
Output Format
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.
Sample Input
qA2
Sample Output
True
True
True
True
True

String Validators - Hacker Rank Solution

Check the string  by using the methods: .isalpha(), .isalnum(), .isdigit(), .islower() and .isupper().
Problem Setter's code:
S = raw_input()
print any([char.isalnum() for char in S])
print any([char.isalpha() for char in S])
print any([char.isdigit() for char in S])
print any([char.islower() for char in S])
print any([char.isupper() for char in S])

12 comments:


  1. correct code for given problem



    S = input()
    print(any(char.isalnum() for char in S))
    print(any(char.isalpha() for char in S))
    print(any(char.isdigit() for char in S))
    print(any(char.islower() for char in S))
    print(any(char.isupper() for char in S))

    ReplyDelete
  2. s = input()
    print(any(map(str.isalnum,s)))
    print(any(map(str.isalpha,s)))
    print(any(map(str.isdigit,s)))
    print(any(map(str.islower,s)))
    print(any(map(str.isupper,s)))

    ReplyDelete
  3. if __name__ == '__main__':
    s = input()
    print(any(a.isalnum() for a in s))
    print(any(a.isalpha() for a in s))
    print(any(a.isdigit() for a in s))
    print(any(a.islower() for a in s))
    print(any(a.isupper() for a in s))

    ReplyDelete
  4. s = input()
    alnm = 'False'
    alp = 'False'
    dig = 'False'
    low = 'False'
    upp = 'False'
    for i in s:
    if i.isalnum():
    alnm = 'True'
    if i.isalpha():
    alp = 'True'
    if i.isdigit():
    dig = 'True'
    if i.islower():
    low = 'True'
    if i.isupper():
    upp = 'True'
    print(alnm + '\n'+ alp + '\n'+dig + '\n'+ low+ '\n'+upp)

    ReplyDelete
  5. if __name__ == '__main__':
    s = input()
    alphanum=False
    alpha=False
    digit=False
    lrcase=False
    upcase=False
    for each in s:
    if each.isalnum():
    alphanum=True
    if each.isalpha():
    alpha=True
    if each.isdigit():
    digit=True
    if each.islower():
    lrcase=True
    if each.isupper():
    upcase=True
    print(alphanum,alpha,digit,lrcase,upcase, sep='\n' )

    ReplyDelete

  6. if __name__ == '__main__':
    s = input()
    print(any(char.isalnum() for char in s))
    print(any(char.isalpha() for char in s))
    print(any(char.isdigit() for char in s))
    print(any(char.islower() for char in s))
    print(any(char.isupper() for char in s))

    ReplyDelete
  7. Most Complicated solution
    s = input()
    isn = []
    isa = []
    isd = []
    isl = []
    isu = []
    for i in s:
    isn.append(i.isalnum())
    isa.append(i.isalpha())
    isd.append(i.isdigit())
    isl.append(i.islower())
    isu.append(i.isupper())

    print('True'if(isn.count(isn[0]) != len(isn))else isn[0])
    print('True'if(isa.count(isa[0]) != len(isa))else isa[0])
    print('True'if(isd.count(isd[0]) != len(isd)) else isd[0])
    print('True'if(isl.count(isl[0]) != len(isl))else isl[0])
    print('True'if(isu.count(isu[0]) != len(isu))else isu[0])

    ReplyDelete

Powered by Blogger.