Back to Freecodecamp

Step 23

curriculum/challenges/english/blocks/learn-list-comprehension-by-building-a-case-converter-program/657f4add33ea4b1f61ba3dc8.md

latest1.6 KB
Original Source

--description--

Finally try out this new implementation by executing the program. Change the input string to 'IAmAPascalCasedString' and see if it comes out as 'i_am_a_pascal_cased_string', even though that's a lie.

If you've done everything correctly, you should see the input string converted into snake case, like before.

Congratulations! Now your convert_to_snake_case() function is ready.

--hints--

You should change the input string from 'aLongAndComplexString' to 'IAmAPascalCasedString' inside the main() function.

js
({
    test: () => {
        const transformedCode = code.replace(/\r/g, "");
        const main = __helpers.python.getDef("\n" + transformedCode, "main");
        const { function_body } = main;

        assert.match(function_body, / +print\(\s*convert_to_snake_case\(\s*'IAmAPascalCasedString'\s*\)\s*\)/);
    }
})

--seed--

--seed-contents--

py
def convert_to_snake_case(pascal_or_camel_cased_string):

    snake_cased_char_list = [
        '_' + char.lower() if char.isupper()
        else char
        for char in pascal_or_camel_cased_string
    ]

    return ''.join(snake_cased_char_list).strip('_')

--fcc-editable-region--
def main():
    print(convert_to_snake_case('aLongAndComplexString'))

    
--fcc-editable-region--

main()

--solutions--

py
def convert_to_snake_case(pascal_or_camel_cased_string):

    snake_cased_char_list = [
        '_' + char.lower() if char.isupper()
        else char
        for char in pascal_or_camel_cased_string
    ]

    return ''.join(snake_cased_char_list).strip('_')

def main():
    print(convert_to_snake_case('IAmAPascalCasedString'))

main()