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] == '-']