나는 이렇게 학습한다/Algorithm & SQL

1223. validate code with simple regex

daco2020 2022. 12. 23. 21:39
반응형

Basic regex tasks. Write a function that takes in a numeric code of any length. The function should check if the code begins with 1, 2, or 3 and return true if so. Return false otherwise.

You can assume the input will always be a number.



Solution:

def validate_code(code: int) -> bool:
    return int(str(code)[0]) <= 3
import re

def validate_code(code):
    return bool(re.match("^[1-3]",str(code)))


반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

1225. Switcheroo  (0) 2022.12.25
1224. Regex count lowercase letters  (0) 2022.12.25
1222. Formatting decimal places #0  (0) 2022.12.23
1221. Name Shuffler  (0) 2022.12.21
1220. The 'if' function  (0) 2022.12.20