You would like to set a password for a bank account. However, there are three restrictions on the format of the password:
it has to contain only alphanumerical characters (a−z, A−Z, 0−9);
there should be an even number of letters;
there should be an odd number of digits.
You are given a string S consisting of N characters. String S can be divided into words by splitting it at, and removing, the spaces. The goal is to choose the longest word that is a valid password. You can assume that if there are K spaces in string S then there are exactly K + 1 words.
For example, given "test 5 a0A pass007 ?xy1", there are five words and three of them are valid passwords: "5", "a0A" and "pass007". Thus the longest password is "pass007" and its length is 7. Note that neither "test" nor "?xy1" is a valid password, because "?" is not an alphanumerical character and "test" contains an even number of digits (zero).
Write a function:
def solution(S)
that, given a non-empty string S consisting of N characters, returns the length of the longest word from the string that is a valid password. If there is no such word, your function should return −1.
For example, given S = "test 5 a0A pass007 ?xy1", your function should return 7, as explained above.
N is an integer within the range [1..200];
string S consists only of printable ASCII characters and spaces.
최대한 짧은 코드로 풀어보려 했지만, 조금 긴것같다.
import string
def solution(S):
letters, digits = string.ascii_letters, string.digits
maxLength = 0
lis = S.split()
for l in lis:
lcount, dcount = 0, 0
for char in l:
if char in letters:
lcount += 1
elif char in digits:
dcount += 1
else:
flag = False
break
if flag and lcount % 2 == 0 and dcount % 2 != 0:
if len(l) > maxLength:
maxLength = len(l)
if maxLength == 0:
return -1
return maxLength