mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2026-09-24 23:01:18 +08:00
chore(i18n,learn): processed translations (#55207)
This commit is contained in:
+5
@@ -205,6 +205,11 @@ async (getUserInput) => {
|
||||
const getMultiple = await $.get(url + '?created_by=Alice&assigned_to=Bob');
|
||||
assert.isArray(getMultiple);
|
||||
assert.lengthOf(getMultiple, 2);
|
||||
const copyId = getMultiple[0]._id;
|
||||
const getById = await $.get(url + `?_id=${copyId}`);
|
||||
assert.isArray(getById);
|
||||
assert.lengthOf(getById, 1);
|
||||
assert.equal(getById[0]._id, copyId, 'should be able to query a document by _id')
|
||||
} catch (err) {
|
||||
throw new Error(err.responseText || err.message);
|
||||
}
|
||||
|
||||
+3
-3
@@ -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 converts a `camelCase` or `PascalCase` formatted string 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.
|
||||
List comprehensions in Python offer a concise way of constructing lists without using loops or the `.append()` method, often resulting in a briefer and faster execution.
|
||||
|
||||
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 temporarily fill the function body.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
||||
+2
-2
@@ -7,9 +7,9 @@ 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.
|
||||
Inside the function, replace the `pass` statement by creating an empty list named `snake_cased_char_list`.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
||||
+2
-2
@@ -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, now you can start iterating through the input string and convert it into snake case.
|
||||
|
||||
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 as a placeholder in the loop body.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
||||
+4
-2
@@ -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--
|
||||
|
||||
|
||||
+1
-7
@@ -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. Assign the results to a variable named `converted_character`.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
||||
+3
-5
@@ -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 body, you are going to add the converted character to the list you created earlier.
|
||||
|
||||
```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()` method on the `snake_cased_char_list` to add the `converted_character` to the list.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
||||
+3
-1
@@ -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()` method to add `char` to the `snake_cased_char_list` variable.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
||||
+8
-3
@@ -7,13 +7,18 @@ 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 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 a list into a string, separated by a designated string, known as the separator.
|
||||
|
||||
```py
|
||||
''.join(snake_cased_char_list)
|
||||
result_string = ''.join(characters)
|
||||
```
|
||||
|
||||
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.
|
||||
The example above joins together the elements of the `characters` list into a single string where each element is concatenated together using an empty string as the separator.
|
||||
|
||||
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--
|
||||
|
||||
|
||||
+8
-5
@@ -7,15 +7,19 @@ 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, which removes from a string any leading or trailing characters among a set of characters passed as its argument. For example:
|
||||
|
||||
```py
|
||||
snake_cased_string.strip('_')
|
||||
original_string = "_example_string_"
|
||||
|
||||
clean_string = original_string.strip('_')
|
||||
```
|
||||
|
||||
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.
|
||||
The `strip()` method is applied to `original_string`. This removes any leading and trailing underscore. The result of the example above would be the string `'example_string'`.
|
||||
|
||||
Declare a new variable named `clean_snake_cased_string` and assign it the result of the `.strip()` method applied to `snake_cased_string` , passing `'_'` as the argument to the method.
|
||||
|
||||
# --hints--
|
||||
|
||||
@@ -48,6 +52,5 @@ def convert_to_snake_case(pascal_or_camel_cased_string):
|
||||
snake_cased_char_list.append(char)
|
||||
--fcc-editable-region--
|
||||
snake_cased_string = ''.join(snake_cased_char_list)
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
|
||||
+4
-1
@@ -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`.
|
||||
|
||||
|
||||
# --hints--
|
||||
|
||||
|
||||
+3
-1
@@ -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--
|
||||
|
||||
|
||||
+3
-1
@@ -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, pass the function call as the argument to the `print()` function.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
||||
+9
-8
@@ -7,21 +7,22 @@ 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__'`.
|
||||
In order to display the output of the `convert_to_snake_case()` function, you need to call the `main()` function.
|
||||
|
||||
Remember to use `pass` to fill the `if` statement body.
|
||||
At the same level as the two existing functions, add a call to the `main()` function. You should see the given camel or pascal cased string converted to snake case upon execution.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should write a `if` clause to check whether `__name__ == '__main__'` evaluates to `True` or not. Don't forget the colon at the end and use `pass` to fill the `if` body.
|
||||
You should add a call to the `main()` function.
|
||||
|
||||
```js
|
||||
({
|
||||
test: () => {
|
||||
const transformedCode = code.replace(/\r/g, "");
|
||||
assert.match(transformedCode, /\nif\s+__name__\s*==\s*("|')__main__\1\s*:/);
|
||||
}
|
||||
})
|
||||
test: () => {
|
||||
const pythonCode = `_Node(_code.replace('\\\\r', '')).has_call("main()")`;
|
||||
const result = runPython(pythonCode);
|
||||
assert(result);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
+2
-3
@@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 657f425dbab54e11993c80f0
|
||||
title: الخطوة 15
|
||||
title: Step 14
|
||||
challengeType: 20
|
||||
dashedName: step-14
|
||||
---
|
||||
@@ -51,7 +51,6 @@ def convert_to_snake_case(pascal_or_camel_cased_string):
|
||||
def main():
|
||||
print(convert_to_snake_case('aLongAndComplexString'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
||||
```
|
||||
|
||||
+3
-6
@@ -1,15 +1,13 @@
|
||||
---
|
||||
id: 657f4345abe7f2161f99f1ad
|
||||
title: الخطوة 16
|
||||
title: Step 15
|
||||
challengeType: 20
|
||||
dashedName: step-15
|
||||
---
|
||||
|
||||
# --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--
|
||||
|
||||
@@ -53,6 +51,5 @@ def convert_to_snake_case(pascal_or_camel_cased_string):
|
||||
def main():
|
||||
print(convert_to_snake_case('aLongAndComplexString'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
```
|
||||
|
||||
+9
-19
@@ -1,34 +1,28 @@
|
||||
---
|
||||
id: 657f43d341a0dd17120cdb08
|
||||
title: الخطوة 17
|
||||
title: Step 16
|
||||
challengeType: 20
|
||||
dashedName: step-16
|
||||
---
|
||||
|
||||
# --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.
|
||||
You will need to convert uppercase characters to lowercase and add an underscore before them.
|
||||
|
||||
```py
|
||||
snake_cased_char_list = [
|
||||
'_' + char.lower() if char.isupper()
|
||||
]
|
||||
```
|
||||
Before proceeding to work on the list comprehension, you're going to give your function a return value. In this way you'll be able to check the output.
|
||||
|
||||
Python will interpret this expression as "append `'_' + char.lower()` to the list if `char` is in uppercase" and this will convert the case for the capital letters in the input string.
|
||||
|
||||
Start by adding this line within the square braces.
|
||||
Use the `return` statement to return the list `snake_cased_char_list` from your function.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should add `'_' + char.lower() if char.isupper()` within the square braces of `snake_cased_char_list`.
|
||||
You should return the `snake_cased_char_list` list. Ensure the indentation is set correctly.
|
||||
|
||||
```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*snake_cased_char_list/);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
@@ -49,17 +43,13 @@ def convert_to_snake_case(pascal_or_camel_cased_string):
|
||||
|
||||
# return clean_snake_cased_string
|
||||
|
||||
--fcc-editable-region--
|
||||
snake_cased_char_list = [
|
||||
|
||||
]
|
||||
snake_cased_char_list = []
|
||||
--fcc-editable-region--
|
||||
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
def main():
|
||||
print(convert_to_snake_case('aLongAndComplexString'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
```
|
||||
|
||||
+37
-21
@@ -1,35 +1,56 @@
|
||||
---
|
||||
id: 657f456223b8c1187b461987
|
||||
title: الخطوة 18
|
||||
title: Step 19
|
||||
challengeType: 20
|
||||
dashedName: step-19
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
When you start a list comprehension with an `if` statement like this, Python requires you to also add an `else` clause to the expression.
|
||||
A list comprehension is a concise way to create lists in Python. A basic list comprehension consists of an expression followed by a `for` clause:
|
||||
|
||||
```py
|
||||
snake_cased_char_list = [
|
||||
'_' + char.lower() if char.isupper()
|
||||
else char
|
||||
]
|
||||
spam = [i * 2 for i in iterable]
|
||||
```
|
||||
|
||||
Python will interpret this updated expression as "append `'_' + char.lower()` to the list if `char` is in uppercase, append `char` as is otherwise" and this covers the case for both the capital and lowercase letters in the input string.
|
||||
The above uses the variable `i` to iterate over `iterable`. Each elements of the resulting list is obtained by evaluating the expression `i * 2` at the current iteration.
|
||||
|
||||
Add an `else` clause inside the pair of square braces.
|
||||
In this step, you need to fill the empty list `snake_cased_char_list` using the list comprehension syntax.
|
||||
|
||||
Turn your empty list into a list comprehension that converts each character in `pascal_or_camel_cased_string` into a lowercase character and prepends an underscore to it (the code you commented out before may help you write the expression). Use `char` to iterate over `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 turn `snake_cased_char_list` into a list comprehension that iterates over `pascal_or_camel_cased_string`.
|
||||
|
||||
```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;
|
||||
({
|
||||
test: () => assert(runPython(`
|
||||
iters = _Node(_code).find_function("convert_to_snake_case").find_variable("snake_cased_char_list").find_comp_iters()
|
||||
len(iters) == 1 and iters[0].is_equivalent("pascal_or_camel_cased_string")
|
||||
`))
|
||||
})
|
||||
```
|
||||
|
||||
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*\]/);
|
||||
Your list comprehension should use `char` to iterate over `pascal_or_camel_cased_string`.
|
||||
|
||||
```js
|
||||
({
|
||||
test: () => assert(runPython(`
|
||||
targets = _Node(_code).find_function("convert_to_snake_case").find_variable("snake_cased_char_list").find_comp_targets()
|
||||
len(targets) == 1 and targets[0].is_equivalent("char")
|
||||
`))
|
||||
})
|
||||
```
|
||||
|
||||
Your list comprehension should evaluate `'_' + char.lower()` for each `char` in `pascal_or_camel_cased_string`.
|
||||
|
||||
```js
|
||||
({
|
||||
test: () => assert(runPython(`
|
||||
_Node(_code).find_function("convert_to_snake_case").find_variable("snake_cased_char_list").find_comp_expr().is_equivalent("'_' + char.lower()")
|
||||
`))
|
||||
})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
@@ -51,17 +72,12 @@ 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('_')
|
||||
|
||||
def main():
|
||||
print(convert_to_snake_case('aLongAndComplexString'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
```
|
||||
|
||||
+14
-29
@@ -1,41 +1,32 @@
|
||||
---
|
||||
id: 657f465f8e718b19c5105ae5
|
||||
title: الخطوة 19
|
||||
title: Step 20
|
||||
challengeType: 20
|
||||
dashedName: step-20
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The final piece of the puzzle is the input string itself. The list comprehension needs to know about the object it'll iterate upon.
|
||||
|
||||
In this case, you need to iterate upon all the characters of the string.
|
||||
List comprehensions accept conditional statements, to evaluate the provided expression only if certain conditions are met:
|
||||
|
||||
```py
|
||||
snake_cased_char_list = [
|
||||
'_' + char.lower() if char.isupper()
|
||||
else char
|
||||
for char in pascal_or_camel_cased_string
|
||||
]
|
||||
spam = [i * 2 for i in iterable if i > 0]
|
||||
```
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
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.
|
||||
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 `for char in pascal_or_camel_cased_string` after `else char` within the square braces of `snake_cased_char_list`.
|
||||
You should add an `if` clause with the condition `char.isupper()` to your list comprehension.
|
||||
|
||||
```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;
|
||||
|
||||
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*\]/);
|
||||
}
|
||||
({
|
||||
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()")
|
||||
`))
|
||||
})
|
||||
```
|
||||
|
||||
@@ -58,18 +49,12 @@ 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]
|
||||
--fcc-editable-region--
|
||||
|
||||
|
||||
return ''.join(snake_cased_char_list).strip('_')
|
||||
|
||||
def main():
|
||||
print(convert_to_snake_case('aLongAndComplexString'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
```
|
||||
|
||||
+14
-23
@@ -1,33 +1,31 @@
|
||||
---
|
||||
id: 657f47b12c51e41b3149e584
|
||||
title: الخطوة 20
|
||||
title: Step 21
|
||||
challengeType: 20
|
||||
dashedName: step-21
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
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.
|
||||
Still, the final result is not exactly what you want to achieve. You need to execute a different expression for the characters filtered out by the `if` clause. You'll use an `else` clause for that:
|
||||
|
||||
```py
|
||||
return ''.join(snake_cased_char_list).strip('_')
|
||||
spam = [i * 2 if i > 0 else -1 for i in iterable]
|
||||
```
|
||||
|
||||
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.
|
||||
Note that, differently from the `if` clause, the `if`/`else` construct must be placed between the expression and the `for` keyword.
|
||||
|
||||
Modify your list comprehension so that when a character 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 modify your list comprehension to evaluate the expression `'_' + char.lower()` if `char.isupper()` and char otherwise.
|
||||
|
||||
```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;
|
||||
|
||||
assert.match(function_body, / +return\s+('|")\1\.join\(\s*snake_cased_char_list\s*\)\.strip\(\s*("|')_\2\s*\)/);
|
||||
}
|
||||
({
|
||||
test: () => assert(runPython(`
|
||||
_Node(_code).find_function("convert_to_snake_case").find_variable("snake_cased_char_list").find_comp_expr().is_equivalent("'_' + char.lower() if char.isupper() else char")
|
||||
`))
|
||||
})
|
||||
```
|
||||
|
||||
@@ -50,19 +48,12 @@ 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() for char in pascal_or_camel_cased_string if char.isupper()]
|
||||
--fcc-editable-region--
|
||||
|
||||
|
||||
return ''.join(snake_cased_char_list).strip('_')
|
||||
|
||||
def main():
|
||||
print(convert_to_snake_case('aLongAndComplexString'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
```
|
||||
|
||||
+2
-3
@@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 657f4a4a5828a01de04b652f
|
||||
title: الخطوة 21
|
||||
title: Step 22
|
||||
challengeType: 20
|
||||
dashedName: step-22
|
||||
---
|
||||
@@ -56,6 +56,5 @@ def convert_to_snake_case(pascal_or_camel_cased_string):
|
||||
def main():
|
||||
print(convert_to_snake_case('aLongAndComplexString'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
```
|
||||
|
||||
+3
-7
@@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 657f4add33ea4b1f61ba3dc8
|
||||
title: الخطوة 22
|
||||
title: Step 23
|
||||
challengeType: 20
|
||||
dashedName: step-23
|
||||
---
|
||||
@@ -51,8 +51,7 @@ def main():
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
@@ -71,8 +70,5 @@ def convert_to_snake_case(pascal_or_camel_cased_string):
|
||||
def main():
|
||||
print(convert_to_snake_case('IAmAPascalCasedString'))
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
```
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ In this project, you are going to create a Binary Search Tree (BST). A BST is a
|
||||
|
||||
This is what a Binary Search Tree looks like:
|
||||
|
||||
<img class="img-responsive center-block" alt="a binairy search tree" src="https://cdn.freecodecamp.org/curriculum/python/bst-example.png" style="background-color: white; height:500px; width:500px; padding: 10px;" />
|
||||
<img class="img-responsive center-block" alt="a binary search tree" src="https://cdn.freecodecamp.org/curriculum/python/bst-example.png" style="background-color: white; height:500px; width:500px; padding: 10px;" />
|
||||
|
||||
Begin by defining an empty `TreeNode` class. The `TreeNode` class represents a node in a binary search tree. Use the `pass` keyword to fill the class body and avoid an error.
|
||||
|
||||
|
||||
+4
-4
@@ -7,15 +7,15 @@ dashedName: task-24
|
||||
|
||||
# --description--
|
||||
|
||||
Now that you know how to use `do` to form questions about hobbies, let's use `do` to ask about daily routines. This is useful for finding out about someone's everyday activities, which is a common topic of small talk.
|
||||
Let's use `do` to ask about daily routines.
|
||||
|
||||
Note: When you use `do` as an auxiliary verb in a question, the main verb must stay in its base form. على سبيل المثال:
|
||||
When you use `do` as an auxiliary verb in a question, the main verb must stay in its base form. على سبيل المثال:
|
||||
|
||||
Statement: `She works as a Full Stack dev.`
|
||||
|
||||
Question: `Does she work as a Full Stack dev?`
|
||||
|
||||
`work` stays as `work` and does not change to `works`, even when asking about a third person singular subject like `he,` `she,` or `it.`
|
||||
Notice in the example above that `work` doesn't change to `works`, even when asking about a third person singular subject like `he,` `she,` or `it.`
|
||||
|
||||
# --question--
|
||||
|
||||
@@ -41,7 +41,7 @@ Remember, after `do,` the main verb should not end in `s.`
|
||||
|
||||
### --feedback--
|
||||
|
||||
The word order is incorrect in this option; `do` should be at the beginning
|
||||
Remember, for this kind of question you need an auxiliary verb. Also, pay attention to word order.
|
||||
|
||||
---
|
||||
|
||||
|
||||
+15
-7
@@ -7,15 +7,15 @@ dashedName: task-25
|
||||
|
||||
# --description--
|
||||
|
||||
Before you learned to ask questions using `do,` you can also use the auxiliary verb to in negative sentences. In negative sentences you combine the auxiliary verb `do` (I, you, we, they) or `does` (he, she it) with the word `not` and place it right before the main verb:
|
||||
Before you learned to ask questions using `do`, you can also use it to create negative sentences by combining `do` (for `I`, `you`, `we`, `they`) or `does` (for `he`, `she`, `it`) with the word `not` and placing it right before the main verb:
|
||||
|
||||
`I do not work everyday` - (Subject + do + not + main verb)
|
||||
`I do not work everyday` - (Subject + `do` + `not` + main verb)
|
||||
|
||||
`It does not function` - (Subject + does + not + main verb)
|
||||
`It does not function` - (Subject + `does` + `not` + main verb)
|
||||
|
||||
Often, to be more practical it is common to abbreviate `do not` and `does not` to `don’t` and `doesn’t`.
|
||||
Often, to be more practical people abbreviate `do not` to `don't` and `does not` to `doesn’t`.
|
||||
|
||||
Just like with questions, when you use `don't,` the main verb that comes after it will always be in its base form, no matter who you're talking about. Remember, you use `don't` with I, you, we, and they. When you talk about he, she, or it, you will use `doesn't,` but more on that later. Right now, let's focus on using `don't.`
|
||||
Just like with questions, when you use `don't`, the main verb that comes after it will always be in its base form, no matter who you're talking about.
|
||||
|
||||
# --instructions--
|
||||
|
||||
@@ -25,7 +25,7 @@ Listen to the audio to complete the sentence below.
|
||||
|
||||
## --sentence--
|
||||
|
||||
`Sophie: That's cool! I like photography, but I _ think of it as a hobby. I play the guitar in my free time.`
|
||||
`That's cool! I like photography, but I _ _ of it as a hobby. I play the guitar in my free time.`
|
||||
|
||||
## --blanks--
|
||||
|
||||
@@ -33,7 +33,15 @@ Listen to the audio to complete the sentence below.
|
||||
|
||||
### --feedback--
|
||||
|
||||
Sophie is talking about what she does not do. The first blank needs `don't` to show that she does not consider photography a hobby. The second blank is the base form of the verb that describes what she does in her free time (without `don't` because it's a positive statement).
|
||||
She is talking about something she doesn't do.
|
||||
|
||||
---
|
||||
|
||||
`think`
|
||||
|
||||
### --feedback--
|
||||
|
||||
When after an auxiliary verb, the main verb is not conjugated.
|
||||
|
||||
# --scene--
|
||||
|
||||
|
||||
+8
-8
@@ -10,7 +10,7 @@ Tom: Cool! I want to hear you play some day. -->
|
||||
|
||||
# --description--
|
||||
|
||||
When you want to talk about what someone hopes to do in the future, you can use `want to` to express desire or intention. Remember that `want to` doesn't change even when talking about others.
|
||||
When you want to talk about what someone hopes to do in the future, you can use `want to` to express desire or intention.
|
||||
|
||||
# --question--
|
||||
|
||||
@@ -20,15 +20,15 @@ Does Tom want to hear Sophie play the guitar?
|
||||
|
||||
## --answers--
|
||||
|
||||
`Yes, he wants to.`
|
||||
|
||||
---
|
||||
|
||||
`No, he doesn't want to.`
|
||||
|
||||
### --feedback--
|
||||
|
||||
The dialogue suggests Tom expresses a desire to hear Sophie play.
|
||||
Listen again and pay attention to see if Tom is really using a negative sentence.
|
||||
|
||||
---
|
||||
|
||||
`Yes, he wants to.`
|
||||
|
||||
---
|
||||
|
||||
@@ -44,11 +44,11 @@ The dialogue suggests Tom expresses a desire to hear Sophie play.
|
||||
|
||||
### --feedback--
|
||||
|
||||
`Wanted` is past tense, but Tom's desire to hear Sophie play is in the present.
|
||||
`Wanted` is past tense, but Tom is talking about the present.
|
||||
|
||||
## --video-solution--
|
||||
|
||||
1
|
||||
2
|
||||
|
||||
# --scene--
|
||||
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
---
|
||||
id: 657e0d0037192f3d9e3d5417
|
||||
id: 657e0d0037192f3d9e3d5417
|
||||
title: Task 128
|
||||
challengeType: 22
|
||||
dashedName: task-128
|
||||
|
||||
+1
-3
@@ -11,12 +11,10 @@ dashedName: task-14
|
||||
|
||||
`Patterns` are regular and repeated ways in which something happens or is done. In data, patterns can show trends or behaviors.
|
||||
|
||||
For example, a pattern in customer data might be that more people buy ice cream on hot days. This is a `trend pattern`.
|
||||
For example, a `pattern` in customer data might be that more people use bows as accessories. This is a `trend pattern`.
|
||||
|
||||
There are also `cycle patterns`, like a coffee shop selling more coffee in the morning.
|
||||
|
||||
Recognizing patterns helps businesses understand their customers better.
|
||||
|
||||
`Patterns` can be found in numbers, like sales increasing every month, or in behaviors, like customers preferring to shop online.
|
||||
|
||||
# --fillInTheBlank--
|
||||
|
||||
+1
-1
@@ -33,7 +33,7 @@ Listen to the audio to complete the sentence below.
|
||||
|
||||
### --feedback--
|
||||
|
||||
Sophie is describing what he does next in his routine after the first activity he mentioned.
|
||||
Sophie is describing what she does next in her routine after the first activity she mentioned.
|
||||
|
||||
# --scene--
|
||||
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@ Listen to the audio to complete the sentence below.
|
||||
|
||||
### --feedback--
|
||||
|
||||
Sophie is talking about how setting deadlines helps him maintain his progress towards his goals.
|
||||
Sophie is talking about how setting deadlines helps her maintain her progress towards her goals.
|
||||
|
||||
# --scene--
|
||||
|
||||
|
||||
+8
-2
@@ -7,7 +7,13 @@ dashedName: task-31
|
||||
|
||||
# --description--
|
||||
|
||||
The phrase `would you like` is a polite way to offer something or ask someone if they want something. When offering something, you can specify it directly after the expression `would you like`. For instance, you might ask `Would you like some coffee?` when offering coffee to a guest. If you are proposing to do something, such as `going shopping`, remember to include `to` between the expression and the action. For example: `Would you like to go shopping?`. Finally, if you are asking someone if you should do something for them, use `me` between `would you like` and `to`. For example: `Would you like me to make some coffee?`.
|
||||
The phrase `would you like` is a polite way to offer something or ask someone if they want something. When offering something, you can specify it directly after the expression `would you like`.
|
||||
|
||||
For instance, you might ask `Would you like some coffee?` when offering coffee to a guest.
|
||||
|
||||
If you are proposing to do something, such as `going shopping`, remember to include `to` between the expression and the action.
|
||||
|
||||
For example: `Would you like to go shopping?`. Finally, if you are asking someone if you should do something for them, use `me` between `would you like` and `to`. For example: `Would you like me to make some coffee?`.
|
||||
|
||||
# --question--
|
||||
|
||||
@@ -33,7 +39,7 @@ This sentence is not correct. A correct usage would be `Would you like me to mak
|
||||
|
||||
### --feedback--
|
||||
|
||||
This sentence is incorrect because `likes` should not be plural. The correct form is `Would you like a glass of water?`
|
||||
In this context, `like` doesn’t require the ending `-s` The correct form is `Would you like a glass of water?`
|
||||
|
||||
---
|
||||
|
||||
|
||||
+2
-2
@@ -26,7 +26,7 @@ For instance, if your friend asks you, `Are you coming to the movie tonight?`, y
|
||||
|
||||
### --feedback--
|
||||
|
||||
This word is part of a phrase used for expressing strong confirmation or agreement. The initial letter should be capitalized.
|
||||
This word is a pronoun. The initial letter should be capitalized.
|
||||
|
||||
---
|
||||
|
||||
@@ -34,7 +34,7 @@ This word is part of a phrase used for expressing strong confirmation or agreeme
|
||||
|
||||
### --feedback--
|
||||
|
||||
This word follows the previous one to form a common phrase indicating strong affirmation.
|
||||
Forms a common phrase indicating a strong affirmation.
|
||||
|
||||
# --scene--
|
||||
|
||||
|
||||
+5
@@ -205,6 +205,11 @@ async (getUserInput) => {
|
||||
const getMultiple = await $.get(url + '?created_by=Alice&assigned_to=Bob');
|
||||
assert.isArray(getMultiple);
|
||||
assert.lengthOf(getMultiple, 2);
|
||||
const copyId = getMultiple[0]._id;
|
||||
const getById = await $.get(url + `?_id=${copyId}`);
|
||||
assert.isArray(getById);
|
||||
assert.lengthOf(getById, 1);
|
||||
assert.equal(getById[0]._id, copyId, 'should be able to query a document by _id')
|
||||
} catch (err) {
|
||||
throw new Error(err.responseText || err.message);
|
||||
}
|
||||
|
||||
+3
-3
@@ -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 converts a `camelCase` or `PascalCase` formatted string 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.
|
||||
List comprehensions in Python offer a concise way of constructing lists without using loops or the `.append()` method, often resulting in a briefer and faster execution.
|
||||
|
||||
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 temporarily fill the function body.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
||||
+2
-2
@@ -7,9 +7,9 @@ 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.
|
||||
Inside the function, replace the `pass` statement by creating an empty list named `snake_cased_char_list`.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
||||
+2
-2
@@ -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, now you can start iterating through the input string and convert it into snake case.
|
||||
|
||||
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 as a placeholder in the loop body.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
||||
+4
-2
@@ -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--
|
||||
|
||||
|
||||
+1
-7
@@ -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. Assign the results to a variable named `converted_character`.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
||||
+3
-5
@@ -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 body, you are going to add the converted character to the list you created earlier.
|
||||
|
||||
```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()` method on the `snake_cased_char_list` to add the `converted_character` to the list.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
||||
+3
-1
@@ -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()` method to add `char` to the `snake_cased_char_list` variable.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
||||
+8
-3
@@ -7,13 +7,18 @@ 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 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 a list into a string, separated by a designated string, known as the separator.
|
||||
|
||||
```py
|
||||
''.join(snake_cased_char_list)
|
||||
result_string = ''.join(characters)
|
||||
```
|
||||
|
||||
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.
|
||||
The example above joins together the elements of the `characters` list into a single string where each element is concatenated together using an empty string as the separator.
|
||||
|
||||
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--
|
||||
|
||||
|
||||
+8
-5
@@ -7,15 +7,19 @@ 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, which removes from a string any leading or trailing characters among a set of characters passed as its argument. For example:
|
||||
|
||||
```py
|
||||
snake_cased_string.strip('_')
|
||||
original_string = "_example_string_"
|
||||
|
||||
clean_string = original_string.strip('_')
|
||||
```
|
||||
|
||||
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.
|
||||
The `strip()` method is applied to `original_string`. This removes any leading and trailing underscore. The result of the example above would be the string `'example_string'`.
|
||||
|
||||
Declare a new variable named `clean_snake_cased_string` and assign it the result of the `.strip()` method applied to `snake_cased_string` , passing `'_'` as the argument to the method.
|
||||
|
||||
# --hints--
|
||||
|
||||
@@ -48,6 +52,5 @@ def convert_to_snake_case(pascal_or_camel_cased_string):
|
||||
snake_cased_char_list.append(char)
|
||||
--fcc-editable-region--
|
||||
snake_cased_string = ''.join(snake_cased_char_list)
|
||||
|
||||
--fcc-editable-region--
|
||||
```
|
||||
|
||||
+4
-1
@@ -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`.
|
||||
|
||||
|
||||
# --hints--
|
||||
|
||||
|
||||
+3
-1
@@ -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--
|
||||
|
||||
|
||||
+3
-1
@@ -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, pass the function call as the argument to the `print()` function.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
||||
+9
-8
@@ -7,21 +7,22 @@ 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__'`.
|
||||
In order to display the output of the `convert_to_snake_case()` function, you need to call the `main()` function.
|
||||
|
||||
Remember to use `pass` to fill the `if` statement body.
|
||||
At the same level as the two existing functions, add a call to the `main()` function. You should see the given camel or pascal cased string converted to snake case upon execution.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should write a `if` clause to check whether `__name__ == '__main__'` evaluates to `True` or not. Don't forget the colon at the end and use `pass` to fill the `if` body.
|
||||
You should add a call to the `main()` function.
|
||||
|
||||
```js
|
||||
({
|
||||
test: () => {
|
||||
const transformedCode = code.replace(/\r/g, "");
|
||||
assert.match(transformedCode, /\nif\s+__name__\s*==\s*("|')__main__\1\s*:/);
|
||||
}
|
||||
})
|
||||
test: () => {
|
||||
const pythonCode = `_Node(_code.replace('\\\\r', '')).has_call("main()")`;
|
||||
const result = runPython(pythonCode);
|
||||
assert(result);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
+2
-3
@@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 657f425dbab54e11993c80f0
|
||||
title: 步驟15
|
||||
title: Step 14
|
||||
challengeType: 20
|
||||
dashedName: step-14
|
||||
---
|
||||
@@ -51,7 +51,6 @@ def convert_to_snake_case(pascal_or_camel_cased_string):
|
||||
def main():
|
||||
print(convert_to_snake_case('aLongAndComplexString'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
|
||||
```
|
||||
|
||||
+3
-6
@@ -1,15 +1,13 @@
|
||||
---
|
||||
id: 657f4345abe7f2161f99f1ad
|
||||
title: 步驟 16
|
||||
title: Step 15
|
||||
challengeType: 20
|
||||
dashedName: step-15
|
||||
---
|
||||
|
||||
# --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--
|
||||
|
||||
@@ -53,6 +51,5 @@ def convert_to_snake_case(pascal_or_camel_cased_string):
|
||||
def main():
|
||||
print(convert_to_snake_case('aLongAndComplexString'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
```
|
||||
|
||||
+9
-19
@@ -1,34 +1,28 @@
|
||||
---
|
||||
id: 657f43d341a0dd17120cdb08
|
||||
title: Step 17
|
||||
title: Step 16
|
||||
challengeType: 20
|
||||
dashedName: step-16
|
||||
---
|
||||
|
||||
# --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.
|
||||
You will need to convert uppercase characters to lowercase and add an underscore before them.
|
||||
|
||||
```py
|
||||
snake_cased_char_list = [
|
||||
'_' + char.lower() if char.isupper()
|
||||
]
|
||||
```
|
||||
Before proceeding to work on the list comprehension, you're going to give your function a return value. In this way you'll be able to check the output.
|
||||
|
||||
Python will interpret this expression as "append `'_' + char.lower()` to the list if `char` is in uppercase" and this will convert the case for the capital letters in the input string.
|
||||
|
||||
Start by adding this line within the square braces.
|
||||
Use the `return` statement to return the list `snake_cased_char_list` from your function.
|
||||
|
||||
# --hints--
|
||||
|
||||
You should add `'_' + char.lower() if char.isupper()` within the square braces of `snake_cased_char_list`.
|
||||
You should return the `snake_cased_char_list` list. Ensure the indentation is set correctly.
|
||||
|
||||
```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*snake_cased_char_list/);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
@@ -49,17 +43,13 @@ def convert_to_snake_case(pascal_or_camel_cased_string):
|
||||
|
||||
# return clean_snake_cased_string
|
||||
|
||||
--fcc-editable-region--
|
||||
snake_cased_char_list = [
|
||||
|
||||
]
|
||||
snake_cased_char_list = []
|
||||
--fcc-editable-region--
|
||||
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
def main():
|
||||
print(convert_to_snake_case('aLongAndComplexString'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
```
|
||||
|
||||
+37
-21
@@ -1,35 +1,56 @@
|
||||
---
|
||||
id: 657f456223b8c1187b461987
|
||||
title: 步驟 18
|
||||
title: Step 19
|
||||
challengeType: 20
|
||||
dashedName: step-19
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
When you start a list comprehension with an `if` statement like this, Python requires you to also add an `else` clause to the expression.
|
||||
A list comprehension is a concise way to create lists in Python. A basic list comprehension consists of an expression followed by a `for` clause:
|
||||
|
||||
```py
|
||||
snake_cased_char_list = [
|
||||
'_' + char.lower() if char.isupper()
|
||||
else char
|
||||
]
|
||||
spam = [i * 2 for i in iterable]
|
||||
```
|
||||
|
||||
Python will interpret this updated expression as "append `'_' + char.lower()` to the list if `char` is in uppercase, append `char` as is otherwise" and this covers the case for both the capital and lowercase letters in the input string.
|
||||
The above uses the variable `i` to iterate over `iterable`. Each elements of the resulting list is obtained by evaluating the expression `i * 2` at the current iteration.
|
||||
|
||||
Add an `else` clause inside the pair of square braces.
|
||||
In this step, you need to fill the empty list `snake_cased_char_list` using the list comprehension syntax.
|
||||
|
||||
Turn your empty list into a list comprehension that converts each character in `pascal_or_camel_cased_string` into a lowercase character and prepends an underscore to it (the code you commented out before may help you write the expression). Use `char` to iterate over `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 turn `snake_cased_char_list` into a list comprehension that iterates over `pascal_or_camel_cased_string`.
|
||||
|
||||
```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;
|
||||
({
|
||||
test: () => assert(runPython(`
|
||||
iters = _Node(_code).find_function("convert_to_snake_case").find_variable("snake_cased_char_list").find_comp_iters()
|
||||
len(iters) == 1 and iters[0].is_equivalent("pascal_or_camel_cased_string")
|
||||
`))
|
||||
})
|
||||
```
|
||||
|
||||
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*\]/);
|
||||
Your list comprehension should use `char` to iterate over `pascal_or_camel_cased_string`.
|
||||
|
||||
```js
|
||||
({
|
||||
test: () => assert(runPython(`
|
||||
targets = _Node(_code).find_function("convert_to_snake_case").find_variable("snake_cased_char_list").find_comp_targets()
|
||||
len(targets) == 1 and targets[0].is_equivalent("char")
|
||||
`))
|
||||
})
|
||||
```
|
||||
|
||||
Your list comprehension should evaluate `'_' + char.lower()` for each `char` in `pascal_or_camel_cased_string`.
|
||||
|
||||
```js
|
||||
({
|
||||
test: () => assert(runPython(`
|
||||
_Node(_code).find_function("convert_to_snake_case").find_variable("snake_cased_char_list").find_comp_expr().is_equivalent("'_' + char.lower()")
|
||||
`))
|
||||
})
|
||||
```
|
||||
|
||||
# --seed--
|
||||
@@ -51,17 +72,12 @@ 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('_')
|
||||
|
||||
def main():
|
||||
print(convert_to_snake_case('aLongAndComplexString'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
```
|
||||
|
||||
+14
-29
@@ -1,41 +1,32 @@
|
||||
---
|
||||
id: 657f465f8e718b19c5105ae5
|
||||
title: 步驟 19
|
||||
title: Step 20
|
||||
challengeType: 20
|
||||
dashedName: step-20
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
The final piece of the puzzle is the input string itself. The list comprehension needs to know about the object it'll iterate upon.
|
||||
|
||||
In this case, you need to iterate upon all the characters of the string.
|
||||
List comprehensions accept conditional statements, to evaluate the provided expression only if certain conditions are met:
|
||||
|
||||
```py
|
||||
snake_cased_char_list = [
|
||||
'_' + char.lower() if char.isupper()
|
||||
else char
|
||||
for char in pascal_or_camel_cased_string
|
||||
]
|
||||
spam = [i * 2 for i in iterable if i > 0]
|
||||
```
|
||||
|
||||
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.
|
||||
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.
|
||||
|
||||
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.
|
||||
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 `for char in pascal_or_camel_cased_string` after `else char` within the square braces of `snake_cased_char_list`.
|
||||
You should add an `if` clause with the condition `char.isupper()` to your list comprehension.
|
||||
|
||||
```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;
|
||||
|
||||
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*\]/);
|
||||
}
|
||||
({
|
||||
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()")
|
||||
`))
|
||||
})
|
||||
```
|
||||
|
||||
@@ -58,18 +49,12 @@ 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]
|
||||
--fcc-editable-region--
|
||||
|
||||
|
||||
return ''.join(snake_cased_char_list).strip('_')
|
||||
|
||||
def main():
|
||||
print(convert_to_snake_case('aLongAndComplexString'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
```
|
||||
|
||||
+14
-23
@@ -1,33 +1,31 @@
|
||||
---
|
||||
id: 657f47b12c51e41b3149e584
|
||||
title: 步驟 20
|
||||
title: Step 21
|
||||
challengeType: 20
|
||||
dashedName: step-21
|
||||
---
|
||||
|
||||
# --description--
|
||||
|
||||
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.
|
||||
Still, the final result is not exactly what you want to achieve. You need to execute a different expression for the characters filtered out by the `if` clause. You'll use an `else` clause for that:
|
||||
|
||||
```py
|
||||
return ''.join(snake_cased_char_list).strip('_')
|
||||
spam = [i * 2 if i > 0 else -1 for i in iterable]
|
||||
```
|
||||
|
||||
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.
|
||||
Note that, differently from the `if` clause, the `if`/`else` construct must be placed between the expression and the `for` keyword.
|
||||
|
||||
Modify your list comprehension so that when a character 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 modify your list comprehension to evaluate the expression `'_' + char.lower()` if `char.isupper()` and char otherwise.
|
||||
|
||||
```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;
|
||||
|
||||
assert.match(function_body, / +return\s+('|")\1\.join\(\s*snake_cased_char_list\s*\)\.strip\(\s*("|')_\2\s*\)/);
|
||||
}
|
||||
({
|
||||
test: () => assert(runPython(`
|
||||
_Node(_code).find_function("convert_to_snake_case").find_variable("snake_cased_char_list").find_comp_expr().is_equivalent("'_' + char.lower() if char.isupper() else char")
|
||||
`))
|
||||
})
|
||||
```
|
||||
|
||||
@@ -50,19 +48,12 @@ 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() for char in pascal_or_camel_cased_string if char.isupper()]
|
||||
--fcc-editable-region--
|
||||
|
||||
|
||||
return ''.join(snake_cased_char_list).strip('_')
|
||||
|
||||
def main():
|
||||
print(convert_to_snake_case('aLongAndComplexString'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
```
|
||||
|
||||
+2
-3
@@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 657f4a4a5828a01de04b652f
|
||||
title: 步驟21
|
||||
title: Step 22
|
||||
challengeType: 20
|
||||
dashedName: step-22
|
||||
---
|
||||
@@ -56,6 +56,5 @@ def convert_to_snake_case(pascal_or_camel_cased_string):
|
||||
def main():
|
||||
print(convert_to_snake_case('aLongAndComplexString'))
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
```
|
||||
|
||||
+3
-7
@@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 657f4add33ea4b1f61ba3dc8
|
||||
title: 步驟 22
|
||||
title: Step 23
|
||||
challengeType: 20
|
||||
dashedName: step-23
|
||||
---
|
||||
@@ -51,8 +51,7 @@ def main():
|
||||
|
||||
--fcc-editable-region--
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
@@ -71,8 +70,5 @@ def convert_to_snake_case(pascal_or_camel_cased_string):
|
||||
def main():
|
||||
print(convert_to_snake_case('IAmAPascalCasedString'))
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
main()
|
||||
```
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ dashedName: step-2
|
||||
|
||||
A vector can be defined by two coordinates, `x` and `y`, in the Euclidean plane. The distance between the origin of the axes and the point `(x, y)` will be its length, or norm. And the vector direction will point towards `(x, y)`.
|
||||
|
||||
<img class="img-responsive center-block" alt="a 2-dimensional vector of coordinates (2, 3)" src="https://cdn.freecodecamp.org/curriculum/python/2dvector.png" style="background-color: white; height: 350px; width: auto; padding: 15px;">
|
||||
<img class="img-responsive center-block" alt="a 2-dimensional vector of coordinates (2, 3)" src="https://cdn.freecodecamp.org/curriculum/python/2dvector.png" style="background-color: white; height: 350px; width: auto; padding: 15px;" />
|
||||
|
||||
Within the `Vector` class, create an `__init__` method and give it three parameters, `self`, `x`, and `y`.
|
||||
|
||||
|
||||
+2
-4
@@ -7,9 +7,7 @@ dashedName: step-6
|
||||
|
||||
# --description--
|
||||
|
||||
The length of a vector $\mathbf{a}$, or norm, is typically indicated as $\\| \mathbf{a} \\|$. It can be calculated as the square root of the sum of its squared components:
|
||||
\\[ \\| \mathbf{a} \\| = \sqrt{a_1^2 + a_2^2 + \ldots + a_n^2}
|
||||
\\]
|
||||
The length of a vector $\mathbf{a}$, or norm, is typically indicated as $\\| \mathbf{a} \\|$. It can be calculated as the square root of the sum of its squared components: \\[ \\| \mathbf{a} \\| = \sqrt{a_1^2 + a_2^2 + \ldots + a_n^2} \\]
|
||||
|
||||
Compute the vector norm and return the result from your `norm` method.
|
||||
|
||||
@@ -38,7 +36,7 @@ class Vector:
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
|
||||
def norm(self):
|
||||
pass
|
||||
--fcc-editable-region--
|
||||
|
||||
+1
-1
@@ -31,7 +31,7 @@ class Vector:
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
|
||||
def norm(self):
|
||||
return (self.x**2 + self.y**2)**0.5
|
||||
--fcc-editable-region--
|
||||
|
||||
+1
-1
@@ -29,7 +29,7 @@ class Vector:
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
|
||||
def norm(self):
|
||||
return (self.x**2 + self.y**2)**0.5
|
||||
|
||||
|
||||
+1
-1
@@ -45,7 +45,7 @@ class Vector:
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
|
||||
def norm(self):
|
||||
return (self.x**2 + self.y**2)**0.5
|
||||
|
||||
|
||||
+2
-2
@@ -44,10 +44,10 @@ class Vector:
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
|
||||
def norm(self):
|
||||
return (self.x**2 + self.y**2)**0.5
|
||||
|
||||
|
||||
def __str__(self):
|
||||
pass
|
||||
|
||||
|
||||
+3
-3
@@ -9,7 +9,7 @@ dashedName: step-11
|
||||
|
||||
A vector can have a number `n` of dimensions (components). Here's a representation of a 3-dimensional vector:
|
||||
|
||||
<img class="img-responsive center-block" alt="a 3-dimensional vector of coordinates (4, 6, 3)" src="https://cdn.freecodecamp.org/curriculum/python/3dvector.png" style="background-color: white; height: 350px; width: auto; padding: 15px;">
|
||||
<img class="img-responsive center-block" alt="a 3-dimensional vector of coordinates (4, 6, 3)" src="https://cdn.freecodecamp.org/curriculum/python/3dvector.png" style="background-color: white; height: 350px; width: auto; padding: 15px;" />
|
||||
|
||||
So far, you created a 2-dimensional vector. You want to be able to represent vectors with a different number of dimensions without rewriting the necessary code for each specific case. For that, you will use inheritance.
|
||||
|
||||
@@ -37,10 +37,10 @@ class Vector:
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
|
||||
def norm(self):
|
||||
return (self.x**2 + self.y**2)**0.5
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.x, self.y}'
|
||||
|
||||
|
||||
+3
-3
@@ -16,7 +16,7 @@ class Tree:
|
||||
|
||||
class Oak(Tree):
|
||||
pass
|
||||
|
||||
|
||||
Oak().sprout() # Output: Making new leaves!
|
||||
```
|
||||
|
||||
@@ -55,10 +55,10 @@ class R2Vector:
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
|
||||
def norm(self):
|
||||
return (self.x**2 + self.y**2)**0.5
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.x, self.y}'
|
||||
--fcc-editable-region--
|
||||
|
||||
+2
-2
@@ -40,10 +40,10 @@ class R2Vector:
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
|
||||
def norm(self):
|
||||
return (self.x**2 + self.y**2)**0.5
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.x, self.y}'
|
||||
--fcc-editable-region--
|
||||
|
||||
+2
-2
@@ -32,10 +32,10 @@ class R2Vector:
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
|
||||
def norm(self):
|
||||
return (self.x**2 + self.y**2)**0.5
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.x, self.y}'
|
||||
--fcc-editable-region--
|
||||
|
||||
+2
-2
@@ -30,10 +30,10 @@ class R2Vector:
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
|
||||
def norm(self):
|
||||
return (self.x**2 + self.y**2)**0.5
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.x, self.y}'
|
||||
--fcc-editable-region--
|
||||
|
||||
+2
-2
@@ -26,10 +26,10 @@ class R2Vector:
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
|
||||
def norm(self):
|
||||
return (self.x**2 + self.y**2)**0.5
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.x, self.y}'
|
||||
|
||||
|
||||
+2
-2
@@ -52,10 +52,10 @@ class R2Vector:
|
||||
def __init__(self, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
|
||||
def norm(self):
|
||||
return (self.x**2 + self.y**2)**0.5
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.x, self.y}'
|
||||
|
||||
|
||||
+2
-2
@@ -43,10 +43,10 @@ class R2Vector:
|
||||
def __init__(self, *, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
|
||||
def norm(self):
|
||||
return (self.x**2 + self.y**2)**0.5
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.x, self.y}'
|
||||
|
||||
|
||||
+2
-2
@@ -44,10 +44,10 @@ class R2Vector:
|
||||
def __init__(self, *, x, y):
|
||||
self.x = x
|
||||
self.y = y
|
||||
|
||||
|
||||
def norm(self):
|
||||
return (self.x**2 + self.y**2)**0.5
|
||||
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.x, self.y}'
|
||||
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ dashedName: step-25
|
||||
|
||||
# --description--
|
||||
|
||||
Finally, pass the `tuple()` call as the argument to the `str()` function.
|
||||
Finally, pass the `tuple()` call as the argument to the `str()` function.
|
||||
|
||||
# --hints--
|
||||
|
||||
|
||||
+1
-1
@@ -42,7 +42,7 @@ class R2Vector:
|
||||
def __str__(self):
|
||||
return str(tuple(getattr(self, i) for i in vars(self)))
|
||||
--fcc-editable-region--
|
||||
|
||||
|
||||
--fcc-editable-region--
|
||||
class R3Vector(R2Vector):
|
||||
def __init__(self, *, x, y, z):
|
||||
|
||||
+1
-1
@@ -48,7 +48,7 @@ class R2Vector:
|
||||
args = ', '.join(arg_list)
|
||||
return f'{self.__class__.__name__}({args})'
|
||||
--fcc-editable-region--
|
||||
|
||||
|
||||
--fcc-editable-region--
|
||||
class R3Vector(R2Vector):
|
||||
def __init__(self, *, x, y, z):
|
||||
|
||||
+1
-1
@@ -76,7 +76,7 @@ class R2Vector:
|
||||
kwargs = {i: getattr(self, i) + getattr(other, i) for i in vars(self)}
|
||||
return self.__class__(**kwargs)
|
||||
--fcc-editable-region--
|
||||
|
||||
|
||||
--fcc-editable-region--
|
||||
class R3Vector(R2Vector):
|
||||
def __init__(self, *, x, y, z):
|
||||
|
||||
+1
-1
@@ -66,7 +66,7 @@ class R2Vector:
|
||||
kwargs = {i: getattr(self, i) - getattr(other, i) for i in vars(self)}
|
||||
return self.__class__(**kwargs)
|
||||
--fcc-editable-region--
|
||||
|
||||
|
||||
--fcc-editable-region--
|
||||
class R3Vector(R2Vector):
|
||||
def __init__(self, *, x, y, z):
|
||||
|
||||
+1
-1
@@ -66,7 +66,7 @@ class R2Vector:
|
||||
if type(other) in (int, float):
|
||||
kwargs = {i: getattr(self, i) * other for i in vars(self)}
|
||||
return self.__class__(**kwargs)
|
||||
|
||||
|
||||
elif type(self) == type(other):
|
||||
pass
|
||||
--fcc-editable-region--
|
||||
|
||||
+1
-1
@@ -56,7 +56,7 @@ class R2Vector:
|
||||
if type(other) in (int, float):
|
||||
kwargs = {i: getattr(self, i) * other for i in vars(self)}
|
||||
return self.__class__(**kwargs)
|
||||
|
||||
|
||||
elif type(self) == type(other):
|
||||
args = [getattr(self, i) * getattr(other, i) for i in vars(self)]
|
||||
return sum(args)
|
||||
|
||||
+2
-3
@@ -7,8 +7,7 @@ dashedName: step-56
|
||||
|
||||
# --description--
|
||||
|
||||
It's time to test the multiplication. Declare a new variable `v5` and assign it the scalar multiplication `v1 * 3`.
|
||||
Then, call the `print` function and pass it the following f-string: `f'v1 * 3 = {v5}'`.
|
||||
It's time to test the multiplication. Declare a new variable `v5` and assign it the scalar multiplication `v1 * 3`. Then, call the `print` function and pass it the following f-string: `f'v1 * 3 = {v5}'`.
|
||||
|
||||
# --hints--
|
||||
|
||||
@@ -18,7 +17,7 @@ You should declare a new variable `v5` and assign it the scalar multiplication `
|
||||
({ test: () => assert(runPython(`_Node(_code).find_variable("v5").is_equivalent("v5 = v1 * 3")`)) })
|
||||
```
|
||||
|
||||
You should print the f-string `f'v1 * 3 = {v5}'`.
|
||||
You should print the f-string `f'v1 * 3 = {v5}'`.
|
||||
|
||||
```js
|
||||
({ test: () => assert(runPython(`_Node(_code).has_call("print(f'v1 * 3 = {v5}')")`)) })
|
||||
|
||||
+1
-1
@@ -20,7 +20,7 @@ You should update the assignment of `v5` assigning it the scalar product `v1 * v
|
||||
({ test: () => assert(runPython(`_Node(_code).find_variable("v5").is_equivalent("v5 = v1 * v2")`)) })
|
||||
```
|
||||
|
||||
You should update your `print` call to print the f-string `f'v1 * v2 = {v5}'`.
|
||||
You should update your `print` call to print the f-string `f'v1 * v2 = {v5}'`.
|
||||
|
||||
```js
|
||||
({ test: () => assert(runPython(`_Node(_code).has_call("print(f'v1 * v2 = {v5}')")`)) })
|
||||
|
||||
+1
-1
@@ -67,7 +67,7 @@ class R2Vector:
|
||||
return sum(args)
|
||||
return NotImplemented
|
||||
--fcc-editable-region--
|
||||
|
||||
|
||||
--fcc-editable-region--
|
||||
class R3Vector(R2Vector):
|
||||
def __init__(self, *, x, y, z):
|
||||
|
||||
+1
-1
@@ -63,7 +63,7 @@ class R2Vector:
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
return all(getattr(self, i) == getattr(other, i) for i in vars(self))
|
||||
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self == other
|
||||
|
||||
|
||||
+1
-1
@@ -63,7 +63,7 @@ class R2Vector:
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
return all(getattr(self, i) == getattr(other, i) for i in vars(self))
|
||||
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self == other
|
||||
|
||||
|
||||
+2
-2
@@ -71,11 +71,11 @@ class R2Vector:
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
return all(getattr(self, i) == getattr(other, i) for i in vars(self))
|
||||
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self == other
|
||||
--fcc-editable-region--
|
||||
|
||||
|
||||
--fcc-editable-region--
|
||||
class R3Vector(R2Vector):
|
||||
def __init__(self, *, x, y, z):
|
||||
|
||||
+1
-1
@@ -73,7 +73,7 @@ class R2Vector:
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
return all(getattr(self, i) == getattr(other, i) for i in vars(self))
|
||||
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self == other
|
||||
--fcc-editable-region--
|
||||
|
||||
+1
-1
@@ -63,7 +63,7 @@ class R2Vector:
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
return all(getattr(self, i) == getattr(other, i) for i in vars(self))
|
||||
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self == other
|
||||
--fcc-editable-region--
|
||||
|
||||
+1
-1
@@ -91,7 +91,7 @@ class R2Vector:
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
return all(getattr(self, i) == getattr(other, i) for i in vars(self))
|
||||
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self == other
|
||||
--fcc-editable-region--
|
||||
|
||||
+1
-1
@@ -77,7 +77,7 @@ class R2Vector:
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
return all(getattr(self, i) == getattr(other, i) for i in vars(self))
|
||||
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self == other
|
||||
--fcc-editable-region--
|
||||
|
||||
+1
-1
@@ -77,7 +77,7 @@ class R2Vector:
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
return all(getattr(self, i) == getattr(other, i) for i in vars(self))
|
||||
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self == other
|
||||
|
||||
|
||||
+1
-1
@@ -64,7 +64,7 @@ class R2Vector:
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
return all(getattr(self, i) == getattr(other, i) for i in vars(self))
|
||||
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self == other
|
||||
|
||||
|
||||
+1
-1
@@ -73,7 +73,7 @@ class R2Vector:
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
return all(getattr(self, i) == getattr(other, i) for i in vars(self))
|
||||
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self == other
|
||||
|
||||
|
||||
+2
-2
@@ -73,7 +73,7 @@ class R2Vector:
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
return all(getattr(self, i) == getattr(other, i) for i in vars(self))
|
||||
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self == other
|
||||
|
||||
@@ -97,7 +97,7 @@ class R3Vector(R2Vector):
|
||||
def __init__(self, *, x, y, z):
|
||||
super().__init__(x=x, y=y)
|
||||
self.z = z
|
||||
|
||||
|
||||
def cross(self, other):
|
||||
pass
|
||||
--fcc-editable-region--
|
||||
|
||||
+2
-2
@@ -63,7 +63,7 @@ class R2Vector:
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
return all(getattr(self, i) == getattr(other, i) for i in vars(self))
|
||||
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self == other
|
||||
|
||||
@@ -87,7 +87,7 @@ class R3Vector(R2Vector):
|
||||
def __init__(self, *, x, y, z):
|
||||
super().__init__(x=x, y=y)
|
||||
self.z = z
|
||||
|
||||
|
||||
def cross(self, other):
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
|
||||
+5
-6
@@ -9,11 +9,10 @@ dashedName: step-74
|
||||
|
||||
The dot product between two 3D vectors \\( \mathbf{a} \\) and \\( \mathbf{b} \\) can be computed as it follows:
|
||||
|
||||
\\[ \mathbf{a} \times \mathbf{b} = \begin{pmatrix} a_yb_z - a_zb_y \\\ a_zb_x - a_xb_z \\\ a_xb_y - a_yb_x \end{pmatrix}
|
||||
\\]
|
||||
|
||||
\\[ \mathbf{a} \times \mathbf{b} = \begin{pmatrix} a_yb_z - a_zb_y \\\ a_zb_x - a_xb_z \\\ a_xb_y - a_yb_x \end{pmatrix} \\]
|
||||
|
||||
Where the resulting vector is represented as a column vector.
|
||||
|
||||
|
||||
Implement the formula above to compute the dot product between two 3-dimensional vectors and return the resulting vector from the `cross()` method.
|
||||
|
||||
# --hints--
|
||||
@@ -74,7 +73,7 @@ class R2Vector:
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
return all(getattr(self, i) == getattr(other, i) for i in vars(self))
|
||||
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self == other
|
||||
|
||||
@@ -98,7 +97,7 @@ class R3Vector(R2Vector):
|
||||
def __init__(self, *, x, y, z):
|
||||
super().__init__(x=x, y=y)
|
||||
self.z = z
|
||||
|
||||
|
||||
def cross(self, other):
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
|
||||
+3
-3
@@ -69,7 +69,7 @@ class R2Vector:
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
return all(getattr(self, i) == getattr(other, i) for i in vars(self))
|
||||
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self == other
|
||||
|
||||
@@ -93,7 +93,7 @@ class R3Vector(R2Vector):
|
||||
def __init__(self, *, x, y, z):
|
||||
super().__init__(x=x, y=y)
|
||||
self.z = z
|
||||
|
||||
|
||||
def cross(self, other):
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
@@ -102,7 +102,7 @@ class R3Vector(R2Vector):
|
||||
'y': self.z * other.x - self.x * other.z,
|
||||
'z': self.x * other.y - self.y * other.x
|
||||
}
|
||||
|
||||
|
||||
return self.__class__(**kwargs)
|
||||
--fcc-editable-region--
|
||||
v1 = R2Vector(x=2, y=3)
|
||||
|
||||
+3
-3
@@ -63,7 +63,7 @@ class R2Vector:
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
return all(getattr(self, i) == getattr(other, i) for i in vars(self))
|
||||
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self == other
|
||||
|
||||
@@ -87,7 +87,7 @@ class R3Vector(R2Vector):
|
||||
def __init__(self, *, x, y, z):
|
||||
super().__init__(x=x, y=y)
|
||||
self.z = z
|
||||
|
||||
|
||||
def cross(self, other):
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
@@ -96,7 +96,7 @@ class R3Vector(R2Vector):
|
||||
'y': self.z * other.x - self.x * other.z,
|
||||
'z': self.x * other.y - self.y * other.x
|
||||
}
|
||||
|
||||
|
||||
return self.__class__(**kwargs)
|
||||
--fcc-editable-region--
|
||||
v1 = R3Vector(x=2, y=3, z=1)
|
||||
|
||||
+6
-6
@@ -65,7 +65,7 @@ class R2Vector:
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
return all(getattr(self, i) == getattr(other, i) for i in vars(self))
|
||||
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self == other
|
||||
|
||||
@@ -89,7 +89,7 @@ class R3Vector(R2Vector):
|
||||
def __init__(self, *, x, y, z):
|
||||
super().__init__(x=x, y=y)
|
||||
self.z = z
|
||||
|
||||
|
||||
def cross(self, other):
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
@@ -98,7 +98,7 @@ class R3Vector(R2Vector):
|
||||
'y': self.z * other.x - self.x * other.z,
|
||||
'z': self.x * other.y - self.y * other.x
|
||||
}
|
||||
|
||||
|
||||
return self.__class__(**kwargs)
|
||||
--fcc-editable-region--
|
||||
v1 = R3Vector(x=2, y=3, z=1)
|
||||
@@ -159,7 +159,7 @@ class R2Vector:
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
return all(getattr(self, i) == getattr(other, i) for i in vars(self))
|
||||
|
||||
|
||||
def __ne__(self, other):
|
||||
return not self == other
|
||||
|
||||
@@ -183,7 +183,7 @@ class R3Vector(R2Vector):
|
||||
def __init__(self, *, x, y, z):
|
||||
super().__init__(x=x, y=y)
|
||||
self.z = z
|
||||
|
||||
|
||||
def cross(self, other):
|
||||
if type(self) != type(other):
|
||||
return NotImplemented
|
||||
@@ -192,7 +192,7 @@ class R3Vector(R2Vector):
|
||||
'y': self.z * other.x - self.x * other.z,
|
||||
'z': self.x * other.y - self.y * other.x
|
||||
}
|
||||
|
||||
|
||||
return self.__class__(**kwargs)
|
||||
|
||||
v1 = R3Vector(x=2, y=3, z=1)
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ dashedName: step-22
|
||||
|
||||
# --description--
|
||||
|
||||
The `norm()` method is returning the correct values, but there's still something you can improve: readability.
|
||||
The `norm()` method is returning the correct values, but there's still something you can improve: readability.
|
||||
|
||||
The `vars()` built-in function takes an object as its argument and returns the `__dict__` attribute of that object.
|
||||
|
||||
|
||||
+1
-1
@@ -46,7 +46,7 @@ class R2Vector:
|
||||
args = ', '.join(arg_list)
|
||||
return f'{self.__class__.__name__}({args})'
|
||||
--fcc-editable-region--
|
||||
|
||||
|
||||
--fcc-editable-region--
|
||||
class R3Vector(R2Vector):
|
||||
def __init__(self, *, x, y, z):
|
||||
|
||||
+1
-1
@@ -42,7 +42,7 @@ class R2Vector:
|
||||
arg_list = [f'{key}={val}' for key, val in vars(self).items()]
|
||||
args = ', '.join(arg_list)
|
||||
return f'{self.__class__.__name__}({args})'
|
||||
|
||||
|
||||
def __getattribute__(self, attr):
|
||||
return 'calling __getattribute__'
|
||||
|
||||
|
||||
+1
-1
@@ -59,7 +59,7 @@ class R2Vector:
|
||||
--fcc-editable-region--
|
||||
def __getattr__(self, attr):
|
||||
return 'calling __getattr__'
|
||||
|
||||
|
||||
class R3Vector(R2Vector):
|
||||
def __init__(self, *, x, y, z):
|
||||
super().__init__(x=x, y=y)
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
---
|
||||
id: 6553ed69ece88d29594748aa
|
||||
title: Step 50
|
||||
title: Step 52
|
||||
challengeType: 20
|
||||
dashedName: step-52
|
||||
---
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user