HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Check a string for any occurrences of certain character classes

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
occurrencesanycertaincharacterforclassescheckstring

Problem

This is a warm-up programming exercise for learning string methods in Python from HackerRank:


You are given a string S.
Your task is to find if string S contains: alphanumeric characters, alphabetical characters, digits, lowercase and uppercase characters.

Input Format


Single line containing, string S.

Constraints

0 < len(S) < 1000



Output Format



  • In First line, print True if S has alphanumeric character otherwise print False.



  • In Second line, print True if S has alphabetical character otherwise print False.



  • In Third line, print True if S has digits otherwise print False.



  • In Fourth line, print True if S has lowercase character otherwise print False.



  • In Fifth line, print True if S has uppcase character otherwise print False.




My working solution is here:

def parse_input():
    string = raw_input()
    return string

def check_string(string):
    check_funs = [str.isalnum,
                  str.isalpha,
                  str.isdigit,
                  str.islower,
                  str.isupper,
                  ]
    return [any(fun(char) for char in string) for fun in check_funs]

def print_output(results):
    for el in results:
        print el

if __name__ == '__main__':
    string = parse_input()
    print_output(check_string(string))


Apologies for the lack of docstrings; I think aside from that, I'm PEP8 compliant but please let me know if I missed something. I'm interested in any and all feedback of course, but in particular:

  • I feel like my check_string() function isn't very pretty. What's the preferred syntax for folding a list of class methods into a list comprehension? I found a possibly relevant Stack Overflow question but couldn't quite make heads or tails of it. It just doesn't seem right to have to do str.method to refer to a function, but then call it with fun(my_string) for fun in func_list instead of my_string.fun() for fun in func_list. (But that

Solution

I think your way of calling the methods is just fine. Readable and succinct. My problem is with the names check_funs and fun. Why are we having fun here?

Generally it's best not to use a function or an abbreviation of functions as a name. At the very least, use func as a much clearer abbreviation for function. Even though it is a somewhat relevant name here, it's not important that they're functions. What matters is what they do or what they're for. I'd cut it down to just checks and then for check in checks. Even aside from that though, make sure an abbreviation is clear and not just removing letters. If I was asked what fun was short for, function would be low down on my list. I could only follow in this instance because I knew that you were actually putting functions in the list.

Context

StackExchange Code Review Q#103991, answer score: 4

Revisions (0)

No revisions yet.