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

0210. Is it a number?

daco2020 2023. 2. 10. 23:32
반응형

Given a string s, write a method (function) that will return true if its a valid single integer or floating number or false if its not.

Valid examples, should return true:

isDigit("3")
isDigit("  3  ")
isDigit("-3.23")

should return false:

isDigit("3-4")
isDigit("  3   5")
isDigit("3 5")
isDigit("zero")



Solution:

def isDigit(string: str) -> bool:
    return string.strip().lstrip("-").replace(".", "").isdigit()


반응형

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

0212. Reversed sequence  (0) 2023.02.13
0211. Remove duplicates from list  (0) 2023.02.12
0209. Sort the Gift Code  (0) 2023.02.09
0209. Sort Out The Men From Boys  (0) 2023.02.09
0207. Divide and Conquer  (0) 2023.02.08