Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(curriculum): description improvement case converter program #54051

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -7,11 +7,11 @@ dashedName: step-1

# --description--

In this project, you are going to learn about list comprehensions in Python by building a program that can take a `camelCase` or `PascalCase` formatted string and convert that to a `snake_case` formatted string.
In this project, you are going to learn about list comprehensions in Python by building a program that can take a `camelCase` or `PascalCase` formatted string and convert it into a `snake_case` formatted string.

List comprehensions in Python are a concise way to construct a list without using loops or the `.append()` method. Apart from being briefer, list comprehensions often run faster.

Start defining a new function named `convert_to_snake_case()` that accepts a string named `pascal_or_camel_cased_string` as input. For now, add a `pass` statement inside the function.
To begin, define a new function named `convert_to_snake_case()` that takes `pascal_or_camel_cased_string` as input. Within the function body, include a `pass` statement to mark the starting point of your case conversion.
larymak marked this conversation as resolved.
Show resolved Hide resolved

# --hints--

Expand Down
Expand Up @@ -7,9 +7,17 @@ dashedName: step-2

# --description--

Now create a new list named `snake_cased_char_list` inside the function. You can use a set of empty square braces to create the new list.
You need to add an empty list that will hold the characters of the string after you have converted them to snake case.

This list will hold the characters of the string after you have converted them to snake case.
In Python, a list is a collection of items that can be of different types. It's denoted by a set of square brackets `[]`.
larymak marked this conversation as resolved.
Show resolved Hide resolved

```python
# List Example

my_list = []
```

Inside the function, replace the `pass` statement by creating an empty list named `snake_cased_char_list`.

# --hints--

Expand Down
Expand Up @@ -7,9 +7,9 @@ dashedName: step-3

# --description--

Now that you have an empty list in place, you can start iterating through the input string and start converting each character to snake case.
With the empty list in place, iterate through the input string and convert each character to snake case using a `for` loop. Iterating over a sequence is called traversal.
larymak marked this conversation as resolved.
Show resolved Hide resolved

Use a `for` loop to iterate through the `pascal_or_camel_cased_string`. Make sure to name the target variable `char` which is short for character. For now, add a `pass` statement in the loop body.
Inside the function, below the list you just created, add a `for` loop to iterate through the `pascal_or_camel_cased_string`. Make sure to name the target variable `char`. For now, add a `pass` statement in the loop body.

# --hints--

Expand Down
Expand Up @@ -7,9 +7,11 @@ dashedName: step-4

# --description--

Uppercase characters in camel case or pascal case indicate the start of new words.
In both camel case and pascal case, uppercase characters mark the beginning of new words. To convert the input string to snake case, you will need to check if the characters in the input string are uppercase.

Inside the loop body, use an `if` statement in conjunction with the `.isupper()` string method to check for uppercase characters and move `pass` inside the new `if` statement.
You can use the `.isupper()` string method to check if a character is uppercase. This method returns `True` if the character is uppercase and `False` if it is not.

Inside the `for` loop, add an `if` statement to check if the current character is uppercase. Move the `pass` statement inside the new `if` statement.

# --hints--

Expand Down
Expand Up @@ -9,13 +9,7 @@ dashedName: step-5

Inside the `if` statement body, you need to convert any uppercase character to lowercase and prepend an underscore to this lowercase character.

Use the `.lower()` string method to convert uppercase characters to lowercase characters. You can then concatenate an underscore to the character using the plus sign.

```python
'_' + char.lower()
```

Assign the modified character to a variable named `converted_character` inside the if statement body.
Use the `.lower()` string method to convert uppercase characters to lowercase characters. Then, prepend an underscore to the character using concatenation. Assign the results to a variable named `converted_character`.
larymak marked this conversation as resolved.
Show resolved Hide resolved

# --hints--

Expand Down
Expand Up @@ -7,13 +7,11 @@ dashedName: step-6

# --description--

Still within the `if` statement body, use the `.append()` list method to add the converted character to the list you created earlier.
Within the `if` statement's body, we are goin to add the converted character to the list we created earlier.
larymak marked this conversation as resolved.
Show resolved Hide resolved

```py
snake_cased_char_list.append(converted_character)
```
For this, the `.append()` method will be used. This method adds a given object to the end of the list it is invoked on.

The `.append()` method adds a given object to the end of the list you invoke it on.
Use the `.append()` on the `snake_cased_char_list` to add the `converted_character` to the list.
larymak marked this conversation as resolved.
Show resolved Hide resolved

# --hints--

Expand Down
Expand Up @@ -7,7 +7,9 @@ dashedName: step-7

# --description--

Add an `else` clause on the same level as the existing `if` statement, inside the `for` loop. Add characters that are already in lowercase to the list of converted characters inside the body of the `else` clause.
You need to handle the characters that are already in lowercase by adding them to the list of converted characters.

Right after the `if` statement within the `for` loop add an `else` clause and use the `.append()` to add `char` to the `snake_cased_char_list` variable.
larymak marked this conversation as resolved.
Show resolved Hide resolved

# --hints--

Expand Down
Expand Up @@ -7,13 +7,13 @@ dashedName: step-8

# --description--

By the end of the loop, `snake_cased_char_list` should contain all the converted characters in correct order. Use the `.join()` string method to convert the list of characters into a string.
By this point, the `snake_cased_char_list` variable contains all the converted characters. You will need to convert the list of characters into a string using the `.join()` method.

```py
''.join(snake_cased_char_list)
```
larymak marked this conversation as resolved.
Show resolved Hide resolved

larymak marked this conversation as resolved.
Show resolved Hide resolved
This joins the characters from the list to the empty string on which you called the `.join()` method. Save the result in a variable named `snake_cased_string` on the same level as the `snake_cased_char_list` variable.
Right after the `snake_cased_char_list` variable, add a new variable named `snake_cased_string` and assign the result of the `.join()` method given above to it.

# --hints--

Expand Down
Expand Up @@ -7,15 +7,15 @@ dashedName: step-9

# --description--

Strings in pascal case start with a capital character. Since you've converted all such characters to lowercase and prepended an underscore to them, chances are, the converted snake case string has a dangling underscode at the start.
In pascal case, strings begin with a capital letter. After converting all the characters to lowercase and adding an underscore to them, there's a chance of having an extra underscore at the start of your string.

The easiest way to strip such unwanted character is by using the `.strip()` string method and passing an underscore to the method as argument.
The easiest way to fix this is by using the `.strip()` string method and passing an underscore to the method as argument. This will remove any leading or trailing underscores from the string.

```py
snake_cased_string.strip('_')
```
larymak marked this conversation as resolved.
Show resolved Hide resolved

Make sure to save the resulting string in a variable named `clean_snake_cased_string` on the same level as the `snake_cased_string` variable.
After the `snake_cased_string` variable, add a new variable named `clean_snake_cased_string` and assign the result of the `.strip()` method given above to it.

# --hints--

Expand Down
Expand Up @@ -7,7 +7,10 @@ dashedName: step-10

# --description--

Now all that is left to complete this function is to return the `clean_snake_cased_string` from the function. So, go ahead and return the string by adding a `return` statement on the same level as the `clean_snake_cased_string` variable.
To wrap up the function, return the `clean_snake_cased_string`. This will complete the function and allow you to use it to convert strings from pascal or camel case to snake case.

Add a `return` statement at the end of the function to return the `clean_snake_cased_string`.
larymak marked this conversation as resolved.
Show resolved Hide resolved


# --hints--

Expand Down
Expand Up @@ -7,7 +7,9 @@ dashedName: step-11

# --description--

Since the function is now complete, put it to use inside another function. Create a new function called `main()` on the same level as the `convert_to_snake_case()` function.
With the function complete, you can now use it inside another function.

Create a new function called `main()` with `pass` as the body of the function.

# --hints--

Expand Down
Expand Up @@ -7,7 +7,9 @@ dashedName: step-12

# --description--

Inside the `main()` function, replace `pass` with a `convert_to_snake_case()` call. Pass the string `'aLongAndComplexString'` as input to the function and print out the output using the `print()` function.
Inside the `main()` function, replace the `pass` statement, with a call to the `convert_to_snake_case()` function, passing the string `'aLongAndComplexString'` as input.

To display the output, enclose the function call within the `print()` function.
larymak marked this conversation as resolved.
Show resolved Hide resolved

# --hints--

Expand Down
Expand Up @@ -7,9 +7,11 @@ dashedName: step-13

# --description--

Before running the `main()` function, you need to make sure that the file is running as a script. Add an `if` statement on the same level as the two existing functions and check whether `__name__ == '__main__'`.
Before running the `main()` function, you need to make sure that the file is running as a script.

Remember to use `pass` to fill the `if` statement body.
To do this add an `if` statement on the same level as the two existing functions and check whether `__name__ == '__main__'`.

Remember to use `pass` for the `if` statement's body.

# --hints--

Expand Down
Expand Up @@ -7,9 +7,7 @@ dashedName: step-16

# --description--

Start by replacing `pass` with the variable `snake_cased_char_list` and assign it an empty list. Use the square brace notation to create the list but do not put anything between the braces.

Put the braces in separate lines so that you have some space between them, where you can work on the code for the list comprehension.
Replace the `pass` keyword with the variable `snake_cased_char_list` and assign it an empty list. Use the square brace notation to create the list.

# --hints--

Expand Down
Expand Up @@ -7,7 +7,7 @@ dashedName: step-17

# --description--

Inside the space you left between the pair of square braces, you can describe the value that you would like to include in the list based on a given condition.
Inside the pair of square braces, you can describe the value that you would like to include in the list based on a given condition.

```py
snake_cased_char_list = [
larymak marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Expand Up @@ -7,7 +7,7 @@ dashedName: step-18

# --description--

larymak marked this conversation as resolved.
Show resolved Hide resolved
When you start a list comprehension with an `if` statement like this, Python requires you to also add an `else` clause to the expression.
When you start a list comprehension with an `if` statement like this, Python requires you to also add an `else` clause.
larymak marked this conversation as resolved.
Show resolved Hide resolved

```py
snake_cased_char_list = [
Expand Down
Expand Up @@ -21,7 +21,7 @@ snake_cased_char_list = [

larymak marked this conversation as resolved.
Show resolved Hide resolved
And there you have it. These three lines of code do the same task as the `for` loop you worked on previously while being cleaner and somewhat faster.

Add this last line of code to iterate over the characters of the string in your list comprehension and make sure that you're writing it within the pair of square braces.
Still within the square braces after the `else clause, add this last line of code to iterate over the characters of the string in your list comprehension.
larymak marked this conversation as resolved.
Show resolved Hide resolved

# --hints--

Expand Down
Expand Up @@ -7,13 +7,13 @@ dashedName: step-20

# --description--

larymak marked this conversation as resolved.
Show resolved Hide resolved
You will still need to join the list elements into a string, strip off any dangling underscores and return the string. Even though you can do that like you did earlier, let's see a shorter alternative.
You will still need to join the list elements into a string, strip off any dangling underscores and return the string. Even though you can do that like you did earlier, here is a shorter alternative.

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

This single line of code will join the list of characters into a string, strip off any dangling underscores, and return the resulting string. Add this line on the same level as the `snake_cased_char_list` variable and inside the `convert_to_snake_case()` function.
Add this line on the same level as the `snake_cased_char_list` variable and inside the `convert_to_snake_case()` function.

# --hints--

Expand Down