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 2 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,7 +7,7 @@ dashedName: step-3

# --description--

With the empty list in place, iterate through the input string and convert it into snake case using a `for` loop.
With the empty list in place, now you can start iterating through the input string and convert it into snake case.

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 as a placeholder in the loop body.

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

# --description--

By this point, the variable `snake_cased_char_list` holds the list of converted characters. To combine these characters into a single string, you should utilize the `join()` method.
By this point, the variable `snake_cased_char_list` holds the list of converted characters. To combine these characters into a single string, you can utilize the `.join()` method.

The `join` method works by concatenating each element of the list into a string, separated by a designated string, known as the separator.
larymak marked this conversation as resolved.
Show resolved Hide resolved

```py
# join all characters using empty string
result_string = ''.join(characters)
```

larymak marked this conversation as resolved.
Show resolved Hide resolved
Now, right after the `for` loop, use the `join()` method to join the elements in `snake_cased_char_list` using an empty string as the separator. Assign the result to a new variable named `snake_cased_string`.
The above example joins together the elements of the `characters` list into a single string where each element is concatenated together without any separator between them.
larymak marked this conversation as resolved.
Show resolved Hide resolved

Now, right after the `for` loop, use the `.join()` method to join the elements in `snake_cased_char_list` using an empty string as the separator. Assign the result to a new variable named `snake_cased_string`.


# --hints--
Expand Down
Expand Up @@ -12,14 +12,14 @@ In pascal case, strings begin with a capital letter. After converting all the ch
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. For example:
larymak marked this conversation as resolved.
Show resolved Hide resolved

```py
# String with underscores
original_string = "_example_string_"

# Using .strip() method to remove underscores
clean_string = original_string.strip('_')
```

Add a new variable named `clean_snake_cased_string` and assign the result of the `.strip()` method applied to `snake_cased_string` to it.
From the example above, the `strip()` method is applied to `original_string`. This removes any leading and trailing characters specified within the parentheses, in this case, underscores. The result of the would be the string `'example_string'`.

Declare a new variable named `clean_snake_cased_string` and assign the result of the `.strip()` method applied to `snake_cased_string` , passing `'_'` as the argument to the method.
larymak marked this conversation as resolved.
Show resolved Hide resolved

# --hints--

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

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.
To display the output, pass the function call as the argument to the `print()` function.

# --hints--

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

# --description--

You will need to to convert uppercase characters to lowercase and add an underscore before them.
You will need to convert uppercase characters to lowercase and add an underscore before them. Before proceeding, join all the elements of the list `snake_cased_char_list` into a single string using an empty string `''` as the separator.
larymak marked this conversation as resolved.
Show resolved Hide resolved

Within the empty list, implement an `if` statement to check whether the character is uppercase. If it is, append `'_' + char.lower()` to the list.
The using the return statement, return an empty string `''` joined with `snake_cased_char_list` as the argument, and strip any leading or trailing underscores from the result.
larymak marked this conversation as resolved.
Show resolved Hide resolved
larymak marked this conversation as resolved.
Show resolved Hide resolved

# --hints--

You should add `'_' + char.lower() if char.isupper()` within the square braces of `snake_cased_char_list`.
You should return an empty string `''` joined with `snake_cased_char_list`, and ensure to strip any leading or trailing underscores. Ensure the indentation is correct.
larymak marked this conversation as resolved.
Show resolved Hide resolved

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

assert.match(function_body, / +snake_cased_char_list\s*=\s*\[\s*("|')_\1\s*\+\s*char\.lower\(\s*\)\s+if\s+char\.isupper\(\s*\)\s*\]/);
assert.match(function_body, /return\s*''\.join\(snake_cased_char_list\)\.strip\('_'\)/);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use AST tests when we are creating new ones, it allows any equivalent code, irrespective of spaces and quotes used and stuff.

Suggested change
const transformedCode = code.replace(/\r/g, "");
const convert_to_snake_case = __helpers.python.getDef("\n" + transformedCode, "convert_to_snake_case");
const { function_body } = convert_to_snake_case;
assert.match(function_body, / +snake_cased_char_list\s*=\s*\[\s*("|')_\1\s*\+\s*char\.lower\(\s*\)\s+if\s+char\.isupper\(\s*\)\s*\]/);
assert.match(function_body, /return\s*''\.join\(snake_cased_char_list\)\.strip\('_'\)/);
({
test: () => assert(runPython(`
_Node(_code).find_function('convert_to_snake_case').find_return().is_equivalent('return "".join(snake_cased_char_list).strip('_'))
`))
})

larymak marked this conversation as resolved.
Show resolved Hide resolved
```

# --seed--
Expand All @@ -41,13 +41,10 @@ def convert_to_snake_case(pascal_or_camel_cased_string):

# return clean_snake_cased_string

snake_cased_char_list = []
--fcc-editable-region--
snake_cased_char_list = [

]
--fcc-editable-region--


--fcc-editable-region--

def main():
print(convert_to_snake_case('aLongAndComplexString'))
Expand Down
Expand Up @@ -7,22 +7,22 @@ dashedName: step-18

# --description--

larymak marked this conversation as resolved.
Show resolved Hide resolved
When you start a list comprehension with an `if` statement, Python filters elements based on the condition provided, which means an `else` clause is required to handle the case when the condition is not met.
A list comprehension is a concise way to create lists in Python. It consists of an expression followed by a `for` clause or more `for` or `if` clauses allowing iteration over elements from an iterable and optional transformations or condition.

In this case, an `else` clause is needed to ensure that if the condition specified in the `if` statement is not met, `char` is appended to the list as is.
In this step, you will need to convert the empty list `snake_cased_char_list` into a list where each character from `pascal_or_camel_cased_string` is preceded by an underscore and converted to lowercase.

Add an `else` clause that appends `char` to the list.
Inside the empty list `snake_cased_char_list`, transform each `char` into lowercase and add an underscore before it. Add a `for` loop to iterate over each `char` in `pascal_or_camel_cased_string`.

# --hints--

You should add `else char` after `'_' + char.lower() if char.isupper()` within the square braces of `snake_cased_char_list`.
You should add `for char in pascal_or_camel_cased_string` after `'_' + char.lower()` within the square braces of `snake_cased_char_list`.

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

assert.match(function_body, / +snake_cased_char_list\s*=\s*\[\s*("|')_\1\s*\+\s*char\.lower\(\s*\)\s+if\s+char\.isupper\(\s*\)\s*else\s+char\s*\]/);
assert.match(function_body, /'_'\s*\+\s*char\.lower\(\s*\)\s+for\s+char\s+in\s+pascal_or_camel_cased_string/);
larymak marked this conversation as resolved.
Show resolved Hide resolved
```

# --seed--
Expand All @@ -44,13 +44,9 @@ def convert_to_snake_case(pascal_or_camel_cased_string):
# return clean_snake_cased_string

--fcc-editable-region--
snake_cased_char_list = [
'_' + char.lower() if char.isupper()

]
snake_cased_char_list = []
--fcc-editable-region--


return ''.join(snake_cased_char_list).strip('_')
larymak marked this conversation as resolved.
Show resolved Hide resolved
larymak marked this conversation as resolved.
Show resolved Hide resolved

def main():
print(convert_to_snake_case('aLongAndComplexString'))
Expand Down
Expand Up @@ -7,26 +7,22 @@ dashedName: step-19

# --description--

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--description--

List comprehensions accept conditional statements, to evaluate the provided expression only if certain condition are met:

spam = [i * 2 for i in iterable if i > 0]

As you can see from the output, the list of characters generated from pascal_or_camel_cased_string has been joined. Since the expression inside the list comprehension is evaluated for each character, the result is a lowercase string with all the characters separated by an underscore.

Follow the example above to add an if clause to your list comprehension so that the expression is executed only if the character is uppercase.

--hints--

You should add and if clause with the condition char.isupper() to your list comprehension.

({ test: () => assert(runPython(`
ifs = _Node(_code).find_function("convert_to_snake_case").find_variable("snake_cased_char_list").find_comp_ifs()
len(ifs) == 1 and ifs[0].is_equivalent("char.isupper()")
`)) })

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can consider to add another test to check the existing part of the comprehension.

The final piece of the puzzle is the input string itself. The list comprehension needs to know about the object it'll iterate upon.
List comprehensions are more than just transformation, they can also filter elements based on conditions using `if` statements. You can add an `if` statement within a list comprehension to filter elements based on a given condition.

In this case, it will iterate upon all the characters of the string, and this can be achieved by making use of a `for` loop.
In the previous step, you generated a list of characters from `pascal_or_camel_cased_string`, with each character converted to lowercase and preceded by an underscore. Now, you need to filter out characters that are not uppercase.

After the `else` clause, add a `for` loop to iterate over the `char` variable in the `pascal_or_camel_cased_string` string.
Inside the list comprehension after the `for` clause, add an `if` statement to check if `char` is uppercase.

# --hints--

You should add `for char in pascal_or_camel_cased_string` after `else char` within the square braces of `snake_cased_char_list`.
You should add `if` statement after the `for` clause within the square braces of `snake_cased_char_list` to check if `char` is uppercase.

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

assert.match(function_body, / +snake_cased_char_list\s*=\s*\[\s*'_'\s*\+\s*char\.lower\(\s*\)\s+if\s+char\.isupper\(\s*\)\s*else\s*char\s*for\s+char\s+in\s+pascal_or_camel_cased_string\s*\]/);
}
})
assert.match(function_body, /if\s*char\.isupper\(\)/);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test passes on the seed code.

```

# --seed--
Expand All @@ -48,14 +44,9 @@ def convert_to_snake_case(pascal_or_camel_cased_string):
# return clean_snake_cased_string

--fcc-editable-region--
snake_cased_char_list = [
'_' + char.lower() if char.isupper()
else char

]
snake_cased_char_list = ['_' + char.lower() for char in pascal_or_camel_cased_string]
larymak marked this conversation as resolved.
Show resolved Hide resolved
--fcc-editable-region--


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

def main():
print(convert_to_snake_case('aLongAndComplexString'))
Expand Down
Expand Up @@ -7,28 +7,22 @@ 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, here is a shorter alternative.
List comprehensions not only have the ability to filter elements based on conditions but also perform different transformations depending on the condition. This can be achieved by the use of an `if/else` statement within a list comprehension.

```py
return ''.join(snake_cased_char_list).strip('_')
```
Previously, you filtered out lowercase characters by adding an `if` statement to check if `char` is uppercase. Now, you will extend the comprehension to handle both cases.

Add this line on the same level as the `snake_cased_char_list` variable and inside the `convert_to_snake_case()` function.
Inside the list comprehension, add an `else` clause to ensure that if `char` is not uppercase, it remains unchanged.

# --hints--

You should return `''.join(snake_cased_char_list).strip('_')` at the end of `convert_to_snake_case()` function.
You should add an `else` clause after the `if char.isupper()` statement that ensures `char` is unchanged if it's not uppercase.

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

assert.match(function_body, / +return\s+('|")\1\.join\(\s*snake_cased_char_list\s*\)\.strip\(\s*("|')_\2\s*\)/);
}
})
assert.match(function_body, /else\s*char/);
```

# --seed--
Expand All @@ -50,15 +44,9 @@ def convert_to_snake_case(pascal_or_camel_cased_string):
# return clean_snake_cased_string

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

snake_cased_char_list = ['_' + char.lower() if char.isupper() for char in pascal_or_camel_cased_string]
larymak marked this conversation as resolved.
Show resolved Hide resolved
--fcc-editable-region--


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

def main():
print(convert_to_snake_case('aLongAndComplexString'))
Expand Down