코드로 우주평화

Kebabize 본문

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

Kebabize

daco2020 2022. 3. 16. 00:50
반응형

Description:

Modify the kebabize function so that it converts a camel case string into a kebab case.

kebabize('camelsHaveThreeHumps') // camels-have-three-humps
kebabize('camelsHave3Humps') // camels-have-humps

Notes:

  • the returned string should only contain lowercase letters

 

 

Solution:

1. Repeat the string with a for statement.
2. If there is a number, pass.
3. If there is a capital letter, add '-' in front of it and add a letter.
4. If it is a lowercase letter, add a letter.
5. If the first character of the string is '-', it is removed and returned.

 

def kebabize(string):
    str = ''
    for i in string:
        if i.isdigit():
            pass
        elif i.isupper():
            str += '-' + i.lower()
        else:
            str += i
    return [str, str[1:]][str[0:1] == '-']

 

 

 

반응형

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

Meeting  (0) 2022.03.17
English beggars  (0) 2022.03.16
+1 Array  (0) 2022.03.15
A square of squares  (0) 2022.03.13
Count the divisors of a number  (0) 2022.03.12