Back to Freecodecamp

Step 14

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

latest1.7 KB
Original Source

--description--

So far, in this project you have used a for loop to iterate over your input string and convert it into the desired output. Now you'll begin the transition from a for loop to a list comprehension.

Begin by commenting out all the lines of code inside the convert_to_snake_case() function. Don't delete them as they'll be helpful when you implement the logic using a list comprehension.

Remember to add the pass keyword to the function body to prevent the code from failing during the tests.

--hints--

You should comment out all the 10 lines of code inside the convert_to_snake_case() function and nothing else. Add pass to fill temporarily the function body and avoid an error.

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

        assert.equal(function_body.split("#").length - 1, 10);
    }
})

--seed--

--seed-contents--

py
--fcc-editable-region--
def convert_to_snake_case(pascal_or_camel_cased_string):
    snake_cased_char_list = []
    for char in pascal_or_camel_cased_string:
        if char.isupper():
            converted_character = '_' + char.lower()
            snake_cased_char_list.append(converted_character)
        else:
            snake_cased_char_list.append(char)
    snake_cased_string = ''.join(snake_cased_char_list)
    clean_snake_cased_string = snake_cased_string.strip('_')

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

main()