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

Regexp Basics - is it a letter?

daco2020 2022. 4. 3. 20:19
반응형

Description:

Complete the code which should return true if the given object is a single ASCII letter (lower or upper case), false otherwise.

 

 

Solution:

1. Get the ASCII code of the argument.
2. Check whether the obtained ASCII code corresponds to the alphabet.
3. True if true, false otherwise.

 

def is_letter(s):
    ascii_num = len(s) == 1 and ord(s) 
    return (ascii_num >= 65 and ascii_num <= 90) or (ascii_num >= 97 and ascii_num <= 122

 

 

Bast Practice:

def is_letter(s):
    return len(s) == 1 and s.isalpha()

In Python, using isalpha(), you can check whether an alphabet is an alphabet without ASCII code.

 

 

 

반응형

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

Hide Kata Description  (0) 2022.04.05
How Green Is My Valley?  (0) 2022.04.04
Delete occurrences of an element if it occurs more than n times  (0) 2022.04.02
Jaden Casing Strings  (0) 2022.04.01
Complementary DNA  (0) 2022.03.31