Task description
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.
Assume that:
- N is an integer within the range [1..200];
- string S consists only of printable ASCII characters and spaces.
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
-*- coding:utf-8 -*-
# &Author AnFany
# Lesson 90:Tasks from Indeed Prime 2015 challenge
# P 90.1 LongestPassword
def solution(S):
"""
返回字符串S中符合条件的最长的字符串长度
:param S: 字符串
:return: 最长字符串的长度
"""
split_list = S.split(' ')
print(split_list)
lower = list(range(ord('a'), ord('z')+1))
upper = list(range(ord('A'), ord('Z')+1))
digit = list(range(ord('0'), ord('9')+1))
str_length = []
for i in split_list:
sign = 1
alpha = 0
number = 0
for h in i:
if ord(h) in lower or ord(h) in upper:
alpha += 1
elif ord(h) in digit:
number += 1
else:
sign = 0
break
if sign:
if not alpha % 2 and number % 2:
str_length.append(len(i))
if len(str_length) == 0:
return -1
else:
return max(str_length)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
-*- coding:utf-8 -*-
# &Author AnFany
# Lesson 90:Tasks from Indeed Prime 2015 challenge
# P 90.1 LongestPassword
def solution(S):
"""
返回字符串S中符合条件的最长的字符串长度
:param S: 字符串
:return: 最长字符串的长度
"""
split_list = S.split(' ')
print(split_list)
lower = list(range(ord('a'), ord('z')+1))
upper = list(range(ord('A'), ord('Z')+1))
digit = list(range(ord('0'), ord('9')+1))
str_length = []
for i in split_list:
sign = 1
alpha = 0
number = 0
for h in i:
if ord(h) in lower or ord(h) in upper:
alpha += 1
elif ord(h) in digit:
number += 1
else:
sign = 0
break
if sign:
if not alpha % 2 and number % 2:
str_length.append(len(i))
if len(str_length) == 0:
return -1
else:
return max(str_length)
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 64, in main sol = __import__('solution') File "/tmp/solution.py", line 4 -*- coding\uff1autf-8 -*- ^ IndentationError: unexpected indent
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
#-*- coding:utf-8 -*-
# &Author AnFany
# Lesson 90:Tasks from Indeed Prime 2015 challenge
# P 90.1 LongestPassword
def solution(S):
"""
返回字符串S中符合条件的最长的字符串长度
:param S: 字符串
:return: 最长字符串的长度
"""
split_list = S.split(' ')
print(split_list)
lower = list(range(ord('a'), ord('z')+1))
upper = list(range(ord('A'), ord('Z')+1))
digit = list(range(ord('0'), ord('9')+1))
str_length = []
for i in split_list:
sign = 1
alpha = 0
number = 0
for h in i:
if ord(h) in lower or ord(h) in upper:
alpha += 1
elif ord(h) in digit:
number += 1
else:
sign = 0
break
if sign:
if not alpha % 2 and number % 2:
str_length.append(len(i))
if len(str_length) == 0:
return -1
else:
return max(str_length)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
#-*- coding:utf-8 -*-
# &Author AnFany
# Lesson 90:Tasks from Indeed Prime 2015 challenge
# P 90.1 LongestPassword
def solution(S):
"""
返回字符串S中符合条件的最长的字符串长度
:param S: 字符串
:return: 最长字符串的长度
"""
split_list = S.split(' ')
print(split_list)
lower = list(range(ord('a'), ord('z')+1))
upper = list(range(ord('A'), ord('Z')+1))
digit = list(range(ord('0'), ord('9')+1))
str_length = []
for i in split_list:
sign = 1
alpha = 0
number = 0
for h in i:
if ord(h) in lower or ord(h) in upper:
alpha += 1
elif ord(h) in digit:
number += 1
else:
sign = 0
break
if sign:
if not alpha % 2 and number % 2:
str_length.append(len(i))
if len(str_length) == 0:
return -1
else:
return max(str_length)
['test', '5', 'a0A', 'pass007', '?xy1']
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
-*- coding:utf-8 -*-
# &Author AnFany
# Lesson 90:Tasks from Indeed Prime 2015 challenge
# P 90.1 LongestPassword
def solution(S):
"""
返回字符串S中符合条件的最长的字符串长度
:param S: 字符串
:return: 最长字符串的长度
"""
split_list = S.split(' ')
lower = list(range(ord('a'), ord('z')+1))
upper = list(range(ord('A'), ord('Z')+1))
digit = list(range(ord('0'), ord('9')+1))
str_length = []
for i in split_list:
sign = 1
alpha = 0
number = 0
for h in i:
if ord(h) in lower or ord(h) in upper:
alpha += 1
elif ord(h) in digit:
number += 1
else:
sign = 0
break
if sign:
if not alpha % 2 and number % 2:
str_length.append(len(i))
if len(str_length) == 0:
return -1
else:
return max(str_length)
Traceback (most recent call last): File "exec.py", line 123, in <module> main() File "exec.py", line 64, in main sol = __import__('solution') File "/tmp/solution.py", line 4 -*- coding\uff1autf-8 -*- ^ SyntaxError: invalid syntax
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
#-*- coding:utf-8 -*-
# &Author AnFany
# Lesson 90:Tasks from Indeed Prime 2015 challenge
# P 90.1 LongestPassword
def solution(S):
"""
返回字符串S中符合条件的最长的字符串长度
:param S: 字符串
:return: 最长字符串的长度
"""
split_list = S.split(' ')
lower = list(range(ord('a'), ord('z')+1))
upper = list(range(ord('A'), ord('Z')+1))
digit = list(range(ord('0'), ord('9')+1))
str_length = []
for i in split_list:
sign = 1
alpha = 0
number = 0
for h in i:
if ord(h) in lower or ord(h) in upper:
alpha += 1
elif ord(h) in digit:
number += 1
else:
sign = 0
break
if sign:
if not alpha % 2 and number % 2:
str_length.append(len(i))
if len(str_length) == 0:
return -1
else:
return max(str_length)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
#-*- coding:utf-8 -*-
# &Author AnFany
# Lesson 90:Tasks from Indeed Prime 2015 challenge
# P 90.1 LongestPassword
def solution(S):
"""
返回字符串S中符合条件的最长的字符串长度
:param S: 字符串
:return: 最长字符串的长度
"""
split_list = S.split(' ')
lower = list(range(ord('a'), ord('z')+1))
upper = list(range(ord('A'), ord('Z')+1))
digit = list(range(ord('0'), ord('9')+1))
str_length = []
for i in split_list:
sign = 1
alpha = 0
number = 0
for h in i:
if ord(h) in lower or ord(h) in upper:
alpha += 1
elif ord(h) in digit:
number += 1
else:
sign = 0
break
if sign:
if not alpha % 2 and number % 2:
str_length.append(len(i))
if len(str_length) == 0:
return -1
else:
return max(str_length)
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
#-*- coding:utf-8 -*-
# &Author AnFany
# Lesson 90:Tasks from Indeed Prime 2015 challenge
# P 90.1 LongestPassword
def solution(S):
"""
返回字符串S中符合条件的最长的字符串长度
:param S: 字符串
:return: 最长字符串的长度
"""
split_list = S.split(' ')
lower = list(range(ord('a'), ord('z')+1))
upper = list(range(ord('A'), ord('Z')+1))
digit = list(range(ord('0'), ord('9')+1))
str_length = []
for i in split_list:
sign = 1
alpha = 0
number = 0
for h in i:
if ord(h) in lower or ord(h) in upper:
alpha += 1
elif ord(h) in digit:
number += 1
else:
sign = 0
break
if sign:
if not alpha % 2 and number % 2:
str_length.append(len(i))
if len(str_length) == 0:
return -1
else:
return max(str_length)
The solution obtained perfect score.