본문 바로가기
Problem Solving/Codility

[Python] LongestPassword

by 부르르 2021. 7. 18.

https://app.codility.com/demo/results/trainingPB6C9Y-2CD/

 

Test results - Codility

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

app.codility.com


 

Point

  • 제시된 조건에 따라 validation check를 수행한다
  • 스플릿 하여 for loop를 하나씩 통과시킨 후 validation check를 통과한 문자열에 대해 최대 길이를 업데이트한다. 

Tip

  • 내장라이브러리인 정규표현식 re 패키지를 임포트하여 사용한다

Task Score

100%

Correctness

100%

Performance

Not assessed

Detected time complexity

 

import re def valid_check(s): ​​​​na = re.compile(r'[^a-zA-Z0-9]+') ​​​​check = na.findall(s) ​​​​if check: ​​​​​​​​return False ​​​​letter = re.compile(r'[a-zA-Z]+') ​​​​check = letter.findall(s) ​​​​if len(''.join(check)) % 2 == 1: ​​​​​​​​return False ​​​​digit = re.compile(r'[0-9]+') ​​​​check = digit.findall(s) ​​​​if len(''.join(check)) % 2 == 0: ​​​​​​​​return False ​​​​return True def solution(S): ​​​​ ​​​​answer = -1 ​​​​lst = S.split(" ") ​​​​for l in lst: ​​​​​​​​if valid_check(l): ​​​​​​​​​​​​answer = max(answer, len(l)) ​​​​return answer

 

문제를 푸는데 도움이 된 테스트 케이스

 

 

728x90
반응형

'Problem Solving > Codility' 카테고리의 다른 글

[Python] TreeHeight  (0) 2021.07.18
[Python] StrSymmetryPoint  (0) 2021.07.18
[Python] FirstUnique  (0) 2021.07.15
[Python] DisappearingPairs  (0) 2021.07.15
[Python] 8.1. Dominator  (0) 2021.04.05

댓글