-1

Hello I'm trying an hackerrank problem : String Validators

I actually solved the question but i'm trying a different approach on it.

Such as:

list=['isalnum()','isalpha()','isdigit()','islower()','isupper()']

for l in list:

    count =0

    for i in range(len(s)+1):
        a=i.l

        if a==1:
            count+=1

        if count !=0:
            print(False)
        elif count >0:
            print(True)

Is there any way that I can convert components inside the list as a function and use it?

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • Does this answer your question? [Python: call a function from string name](https://stackoverflow.com/questions/7936572/python-call-a-function-from-string-name) – takendarkk Apr 02 '20 at 21:56

1 Answers1

1

In python you can actually make a list of function: if you have your array looking like this:

list=[isalnum,isalpha, isdigit, islower, isupper]

if you type

list[0]()

the function isalnum will be executed, obviously if they take parameters you can also pass parameters

in case you want the members of your list to be like you wrote:

list=['isalnum()','isalpha()','isdigit()','islower()','isupper()']

you can use the function eval() even if i don't suggest it:

eval(list[0])

will evaluate the string as a line of code and execute the function isalnum()