I had me a code challenge and I was thinking: is there a way to make this more efficient and short? The challenge requires me to scan a string and ...
In the first line, print
True
if it has any alphanumeric characters. Otherwise, printFalse
.In the second line, print
True
if it has any alphabetical characters. Otherwise, printFalse
.In the third line, print
True
if it has any digits. Otherwise, printFalse
.In the fourth line, print
True
if it has any lowercase characters. Otherwise, printFalse
.In the fifth line, print
True
if has any uppercase characters. Otherwise, printFalse
.
Below is my approach :
def func_alnum(s):
for i in s:
if i.isalnum():
return True
return False
def func_isalpha(s):
for i in s:
if i.isalpha():
return True
return False
def func_isdigit(s):
for i in s:
if i.isdigit():
return True
return False
def func_islower(s):
for i in s:
if i.islower():
return True
return False
def func_isupper(s):
for i in s:
if i.isupper():
return True
return False
if __name__ == '__main__':
s = input()
s=list(s)
print(func_alnum(s))
print(func_isalpha(s))
print(func_isdigit(s))
print(func_islower(s))
print(func_isupper(s))
It feels like I made a mountain out of a molehill. But I would defer to your opinions on efficiency and shortness before I live with that opinion.