반응형
Palindrome strings
A palindrome is a word, phrase, number, or other sequence of characters which reads the same backward or forward. This includes capital letters, punctuation, and word dividers.
Implement a function that checks if something is a palindrome. If the input is a number, convert it to string first.
Examples(Input ==> Output)
"anna" ==> true
"walter" ==> false
12321 ==> true
123456 ==> false
Solution:
from typing import Union
def set_str(func):
def wrapper(value: Union[str, int]):
string = str(value)
return func(string, len(string))
return wrapper
@set_str
def is_palindrome(string: str, half_idx: int) -> bool:
return string[:half_idx] == string[half_idx::-1]
반응형
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
1007. Volume of a Cuboid (0) | 2022.10.08 |
---|---|
1006. Filter out the geese (0) | 2022.10.06 |
1004. Keep up the hoop (0) | 2022.10.04 |
1003. Find the Remainder (0) | 2022.10.03 |
1002. Twice as old (0) | 2022.10.03 |