feat(curriculum): add merge sort project to Python curriculum (#52469)

Co-authored-by: moT01 <20648924+moT01@users.noreply.github.com>
This commit is contained in:
Ihechikara Vincent Abba
2023-12-20 06:24:55 +01:00
committed by GitHub
parent 49aef261fb
commit 739312c546
35 changed files with 2267 additions and 0 deletions
@@ -0,0 +1,10 @@
---
title: Introduction to the Learn Data Structures by Building the Merge Sort Algorithm
block: learn-data-structures-by-building-the-merge-sort-algorithm
superBlock: upcoming-python
isBeta: true
---
## Introduction to the Learn Data Structures by Building the Merge Sort Algorithm
This is a test for the new project-based curriculum.
@@ -0,0 +1,152 @@
{
"name": "Learn Data Structures by Building the Merge Sort Algorithm",
"isUpcomingChange": false,
"usesMultifileEditor": true,
"hasEditableBoundaries": true,
"dashedName": "learn-data-structures-by-building-the-merge-sort-algorithm",
"order": 7,
"time": "5 hours",
"template": "",
"required": [],
"superBlock": "scientific-computing-with-python",
"isBeta": true,
"challengeOrder": [
{
"id": "655cd899f8de09431eabb40c",
"title": "Step 1"
},
{
"id": "6564a9fe51964c229d5b7f4c",
"title": "Step 2"
},
{
"id": "6564aee9c077774ea49c3faf",
"title": "Step 3"
},
{
"id": "656627b47bd2d2a4afbc945d",
"title": "Step 4"
},
{
"id": "657f3a661730891aa64f3568",
"title": "Step 5"
},
{
"id": "656639771fed09b5c0e8fe71",
"title": "Step 6"
},
{
"id": "65772ef923f922cd720e5008",
"title": "Step 7"
},
{
"id": "6577254891048c8f2c19e961",
"title": "Step 8"
},
{
"id": "6577320da0d4c2e594d418e2",
"title": "Step 9"
},
{
"id": "656680b0fc79f2c38a34d90e",
"title": "Step 10"
},
{
"id": "656702f8b4cbd8cbf0a433c6",
"title": "Step 11"
},
{
"id": "656706afd65547d22bee0b68",
"title": "Step 12"
},
{
"id": "656707e11e671ed54c96f7ec",
"title": "Step 13"
},
{
"id": "6577562501feabdf0984a184",
"title": "Step 14"
},
{
"id": "656709e50ed928da35260276",
"title": "Step 15"
},
{
"id": "657823a9f4f372518614c8b7",
"title": "Step 16"
},
{
"id": "65670d1ef177e7e2b76d9528",
"title": "Step 17"
},
{
"id": "656710d1e0ec62253426db24",
"title": "Step 18"
},
{
"id": "6569b0a63423f65dd30888df",
"title": "Step 19"
},
{
"id": "6569b19d31a09b65b4bc390a",
"title": "Step 20"
},
{
"id": "6569b20f829b7e69d43c232a",
"title": "Step 21"
},
{
"id": "6569b4e0bd29d17d4b53b16c",
"title": "Step 22"
},
{
"id": "6569b5c820a6a1859786e774",
"title": "Step 23"
},
{
"id": "6569b68fac723e8c20223ed3",
"title": "Step 24"
},
{
"id": "6569b6be44940a8e2a469c31",
"title": "Step 25"
},
{
"id": "6569b743630ee592a65a7e2f",
"title": "Step 26"
},
{
"id": "6569bca4dd9feab7b295a5e1",
"title": "Step 27"
},
{
"id": "657f59751d5a5c9b069ae0f3",
"title": "Step 28"
},
{
"id": "6569beee367427c90c74899e",
"title": "Step 29"
},
{
"id": "6569c05b9166f9d5bb36c09e",
"title": "Step 30"
},
{
"id": "6569c166d708dcdf7c8fd34c",
"title": "Step 31"
},
{
"id": "6569c1aeecaf95e25a3e2573",
"title": "Step 32"
},
{
"id": "6569c2cbf6c993ea8cd85682",
"title": "Step 33"
},
{
"id": "65817fcc829d9d176878c54a",
"title": "Step 34"
}
],
"helpCategory": "Python"
}
@@ -0,0 +1,37 @@
---
id: 655cd899f8de09431eabb40c
title: Step 1
challengeType: 20
dashedName: step-1
---
# --description--
In this project, you'll learn data structures by building the merge sort algorithm.
This is a sorting algorithm that uses the divide-and-conquer principle to sort collections of data. That is, it 'divides' a collection into smaller sub-parts, and 'conquers' the sub-parts by sorting them independently, then merges the sorted sub-parts.
Create a function called `merge_sort`. This function will handle the task of sorting a list of numbers.
Use the `pass` keyword in the function body.
# --hints--
You should declare a function named `merge_sort`. Don't forget use the `pass` keyword in the function body.
```js
({ test: () => assert(__pyodide.runPython(`
import inspect
inspect.isfunction(__locals.get("merge_sort"))
`))})
```
# --seed--
## --seed-contents--
```py
--fcc-editable-region--
--fcc-editable-region--
```
@@ -0,0 +1,33 @@
---
id: 6564a9fe51964c229d5b7f4c
title: Step 2
challengeType: 20
dashedName: step-2
---
# --description--
You'll need a parameter that denotes the data collection to be sorted. Create a parameter called `array` in the `merge_sort` function.
# --hints--
Your `merge_sort` function should take a single parameter: `array`.
```js
({ test: () => assert(__pyodide.runPython(`
import inspect
str(inspect.signature(__locals.get('merge_sort'))) == '(array)'
`))
})
```
# --seed--
## --seed-contents--
```py
--fcc-editable-region--
def merge_sort():
pass
--fcc-editable-region--
```
@@ -0,0 +1,59 @@
---
id: 6564aee9c077774ea49c3faf
title: Step 3
challengeType: 20
dashedName: step-3
---
# --description--
The merge sort algorithm mainly performs three actions:
- Divide an unsorted sequence of items into sub-parts
- Sort the items in the sub-parts
- Merge the sorted sub-parts
The above happens recursively until the sub-parts are merged into the complete sorted sequence. Let's start by dividing the sequence.
First, replace the `pass` keyword with a variable `middle_point`. Use the integer division operator (`//`) to divide the length of the `array` list by 2 and assign the result to your new `middle_point` variable. Remember to indent your code.
# --hints--
You should not have `pass` in the body of the function.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.notMatch(function_body, /^\s{4}pass/m);
}
})
```
You should declare a `middle_point` variable and assign `len(array) // 2` to it.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /^\s{4}middle_point\s*=\s*len\(\s*array\s*\)\s*\/\/\s*2/m);
}
})
```
# --seed--
## --seed-contents--
```py
--fcc-editable-region--
def merge_sort(array):
pass
--fcc-editable-region--
```
@@ -0,0 +1,55 @@
---
id: 656627b47bd2d2a4afbc945d
title: Step 4
challengeType: 20
dashedName: step-4
---
# --description--
In the previous step, you got the mid point. You can use it to divide `array` into two and assign each part to new variables.
Use the slice syntax to extract the left half of `array` and assign it to a variable named `left_part`.
# --hints--
You should have a variable named `left_part` in your `merge_sort` function.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /left_part\s*(?!=)/m);
}
})
```
You should assign `array[:middle_point]` to the `left_part` variable.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /left_part\s*=\s*array\[:middle_point\]/);
}
})
```
# --seed--
## --seed-contents--
```py
--fcc-editable-region--
def merge_sort(array):
middle_point = len(array) // 2
--fcc-editable-region--
```
@@ -0,0 +1,42 @@
---
id: 656639771fed09b5c0e8fe71
title: Step 6
challengeType: 20
dashedName: step-6
---
# --description--
Now that you've divided the `array` list into two separate lists, you'll keep dividing each list until every element stands alone in its own list. A list with a single number is always sorted.
To do that, recursively call `merge_sort` inside your function.
# --hints--
You should call `merge_sort` at the bottom of your function body.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /merge_sort\s*\(\s*\)/);
}
})
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
--fcc-editable-region--
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
--fcc-editable-region--
```
@@ -0,0 +1,119 @@
---
id: 656680b0fc79f2c38a34d90e
title: Step 10
challengeType: 20
dashedName: step-10
---
# --description--
Now it's time to sort and merge the lists (`left_part` and `right_part`) into the original `array`.
You can do this by comparing elements on both lists, and merging the smaller element to the main list. You are going to do this comparison for all the indexes in `left_part` and `right_part`.
Create three variables: `left_array_index`, `right_array_index`, and `sorted_index` and set their values to `0`. These variables will help you keep track of each index during the sorting process.
# --hints--
You should have a variable named `left_array_index` inside your `merge_sort` function
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /left_array_index\s*(?!=)/);
}
})
```
The value of `left_array_index` should be 0
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /left_array_index\s*=\s*0/);
}
})
```
You should have a variable named `right_array_index` inside your `merge_sort` function
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /right_array_index\s*(?!=)/);
}
})
```
The value of `right_array_index` should be 0
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /right_array_index\s*=\s*0/);
}
})
```
You should have a variable named `sorted_index` inside your `merge_sort` function
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /sorted_index\s*(?!=)/);
}
})
```
The value of `sorted_index` should be 0
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /sorted_index\s*=\s*0/);
}
})
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
--fcc-editable-region--
--fcc-editable-region--
```
@@ -0,0 +1,65 @@
---
id: 656702f8b4cbd8cbf0a433c6
title: Step 11
challengeType: 20
dashedName: step-11
---
# --description--
Inside your function, create a `while` loop that compares an element in `left_part` to an element in `right_part`, and merges the smaller element to the main `array` list.
Create two conditions for the loop: one that checks whether the `left_array_index` is less than the length of `left_part` and another condition that checks whether `right_array_index` is less than the length of `right_part`.
# --hints--
Your `while` loop should have these conditions: `left_array_index < len(left_part) and right_array_index < len(right_part)`. Don't forget to use the `pass` keyword in the body of the loop.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /while\s+left_array_index\s*<\s*len\(\s*left_part\s*\)\s+and\s+right_array_index\s*<\s*len\(\s*right_part\s*\):/);
}
})
```
Use the `pass` keyword in the body of the loop
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /while\s+left_array_index\s*<\s*len\(\s*left_part\s*\)\s+and\s+right_array_index\s*<\s*len\(\s*right_part\s*\):\s*pass/);
}
})
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
--fcc-editable-region--
--fcc-editable-region--
```
@@ -0,0 +1,52 @@
---
id: 656706afd65547d22bee0b68
title: Step 12
challengeType: 20
dashedName: step-12
---
# --description--
Within the `while` loop, replace `pass` with an `if` statement that checks if the index of `left_part` is less than the index of `right_part`.
Use the `pass` keyword in the body of the `if` statement.
# --hints--
You should replace `pass` keyword with an `if` statement with this condition: `left_part[left_array_index] < right_part[right_array_index]`. Add the `pass` keyword to the body of the `if` statement.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /while\s+left_array_index\s*<\s*len\(left_part\)\s+and\s+right_array_index\s*<\s*len\(right_part\):\s*[^}]*if\s+left_part\[left_array_index\]\s*<\s*right_part\[right_array_index\]:\s*pass/);
}
})
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
--fcc-editable-region--
while left_array_index < len(left_part) and right_array_index < len(right_part):
pass
--fcc-editable-region--
```
@@ -0,0 +1,56 @@
---
id: 656707e11e671ed54c96f7ec
title: Step 13
challengeType: 20
dashedName: step-13
---
# --description--
When the `if` condition evaluates to `True`, it means that the element in the `left_part` list is smaller than the element it is being compared to in the `right_part` list.
In that case, you can assign the `left_part` index to the sorted array.
Inside the `if` block, remove `pass` and assign `left_part[left_array_index]` to `array[sorted_index]`.
# --hints--
You should replace `pass` with `array[sorted_index] = left_part[left_array_index]` in the body of the `if` statement.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /while\s+left_array_index\s*<\s*len\(left_part\)\s+and\s+right_array_index\s*<\s*len\(right_part\):\s*[^}]*if\s+left_part\[left_array_index\]\s*<\s*right_part\[right_array_index\]:\s*[^}]*array\[sorted_index\]\s*=\s*left_part\[left_array_index\]\s*(?!.*\bpass\b)/);
}
})
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
--fcc-editable-region--
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
pass
--fcc-editable-region--
```
@@ -0,0 +1,56 @@
---
id: 656709e50ed928da35260276
title: Step 15
challengeType: 20
dashedName: step-15
---
# --description--
In a previous step, you assigned the element in the `left_part` to the `array` list because it was smaller. But this will not always be the case. In some comparison cases, the element on the right could be smaller.
Create an `else` clause to execute when the `left_part` index is not less than the `right_part` index.
Inside the `else` block, assign `right_part[right_array_index]` to `array[sorted_index]`.
# --hints--
You should have `array[sorted_index] = right_part[right_array_index]` in the body of your `else` clause.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /else:\s*[^}]*array\[sorted_index\]\s*=\s*right_part\[right_array_index\]/);
}
})
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
--fcc-editable-region--
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
--fcc-editable-region--
```
@@ -0,0 +1,59 @@
---
id: 65670d1ef177e7e2b76d9528
title: Step 17
challengeType: 20
dashedName: step-17
---
# --description--
The `if` and `else` statements you created in the previous steps will assign elements to the sorted array.
Each element assigned to the sorted array takes up an index in the list. So you have to move to the next index without an element.
Below the `if`/`else` block, but still within the `while` loop increment `sorted_index` by 1. This should not be in the body of the `if` or `else` statement
# --hints--
You should increment the current value of `sorted_index` by 1.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /^\s{8}sorted_index\s*(\+=\s*1|=\s*sorted_index\s*\+\s*1)/m);
}
})
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
--fcc-editable-region--
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
--fcc-editable-region--
```
@@ -0,0 +1,75 @@
---
id: 656710d1e0ec62253426db24
title: Step 18
challengeType: 20
dashedName: step-18
---
# --description--
The `while` loop you created compares one element from `left_part` with another in `right_part`, then adds the smaller element to the main `array` list.
It will continue this operation until there are no elements left to be compared. But `left_part` may still have elements left while `right_part` has none, and vice versa.
Create another `while` loop to copy the remaining elements in `left_part` into the `array` list. Use `left_array_index < len(left_part)` for the `while` condition.
# --hints--
Your `while` loop should have this condition: `left_array_index < len(left_part)`. Don't forget to use the `pass` keyword in the body of the loop.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /^\s{4}while\s+left_array_index\s*<\s*len\(left_part\):/m);
}
})
```
You should have the `pass` keyword in the body of your `while` loop
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /while\s+left_array_index\s*<\s*len\(left_part\):[^}]*\bpass\b/);
}
})
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
--fcc-editable-region--
--fcc-editable-region--
```
@@ -0,0 +1,73 @@
---
id: 6569b0a63423f65dd30888df
title: Step 19
challengeType: 20
dashedName: step-19
---
# --description--
Remove the `pass` keyword. For the `while` loop's code block, assign `left_part[left_array_index]` to `array[sorted_index]`.
# --hints--
You should have `array[sorted_index] = left_part[left_array_index]` in the body of the loop. Don't forget to remove the `pass` keyword in the body of the loop.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /while\s+left_array_index\s*<\s*len\(left_part\):[^}]*\barray\[sorted_index\]\s*=\s*left_part\[left_array_index\]/);
}
})
```
You should not have `pass` in the `while` loop.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /while\s+left_array_index\s*<\s*len\(left_part\):(?:(?!\bpass\b)[^}])*\barray\[sorted_index\]\s*=\s*left_part\[left_array_index\]/);
}
})
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
--fcc-editable-region--
while left_array_index < len(left_part):
pass
--fcc-editable-region--
```
@@ -0,0 +1,60 @@
---
id: 6569b19d31a09b65b4bc390a
title: Step 20
challengeType: 20
dashedName: step-20
---
# --description--
Still within the `while` loop, increment the value of `left_array_index` by 1.
# --hints--
You should use the `+=` operator to increment the current value of `left_array_index` by one within the `while` loop.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /while\s+left_array_index\s*<\s*len\(left_part\):[^}]*\bleft_array_index\s*\+=\s*1\b/);
}
})
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
--fcc-editable-region--
while left_array_index < len(left_part):
array[sorted_index] = left_part[left_array_index]
--fcc-editable-region--
```
@@ -0,0 +1,63 @@
---
id: 6569b20f829b7e69d43c232a
title: Step 21
challengeType: 20
dashedName: step-21
---
# --description--
The last thing to do for the `while` loop is to move to the next index in the sorted array.
Using the augmented assignment addition operator, add `1` to the value of `sorted_index`.
# --hints--
You should use the `+= ` operator to increment the current value of `sorted_index` by one within the `while` loop.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /while\s+left_array_index\s*<\s*len\(left_part\):[^}]*\bsorted_index\s*\+=\s*1\b/);
}
})
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
--fcc-editable-region--
while left_array_index < len(left_part):
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
--fcc-editable-region--
```
@@ -0,0 +1,79 @@
---
id: 6569b4e0bd29d17d4b53b16c
title: Step 22
challengeType: 20
dashedName: step-22
---
# --description--
Now, you are going to replicate the same `while` loop logic for `right_part`.
Create a `while` loop that runs when `right_array_index` is less than `len(right_part)`, and use the `pass` keyword in the body of the loop.
# --hints--
Your `while` loop should have this condition: `right_array_index < len(right_part)`. Don't forget to use the `pass` keyword in the body of the loop.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /^\s{4}while\s+right_array_index\s*<\s*len\(right_part\):/m);
}
})
```
You should have the `pass` keyword in the body of your `while` loop
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /while\s+right_array_index\s*<\s*len\(right_part\):[^}]*\bpass\b/);
}
})
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
while left_array_index < len(left_part):
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
sorted_index += 1
--fcc-editable-region--
--fcc-editable-region--
```
@@ -0,0 +1,78 @@
---
id: 6569b5c820a6a1859786e774
title: Step 23
challengeType: 20
dashedName: step-23
---
# --description--
Within the `while` loop, assign `right_part[right_array_index]` to `array[sorted_index]`.
# --hints--
You should have `array[sorted_index] = right_part[right_array_index]` in the body of the loop. Don't forget to remove the `pass` keyword in the body of the loop.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /while\s+right_array_index\s*<\s*len\(right_part\):[^}]*\barray\[sorted_index\]\s*=\s*right_part\[right_array_index\]/);
}
})
```
There should be no `pass` keyword in the `while` loop
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /while\s+right_array_index\s*<\s*len\(right_part\):(?:(?!\bpass\b)[^}])*\barray\[sorted_index\]\s*=\s*right_part\[right_array_index\]/);
}
})
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
while left_array_index < len(left_part):
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
sorted_index += 1
--fcc-editable-region--
while right_array_index < len(right_part):
pass
--fcc-editable-region--
```
@@ -0,0 +1,65 @@
---
id: 6569b68fac723e8c20223ed3
title: Step 24
challengeType: 20
dashedName: step-24
---
# --description--
Now, use the `+=` operator to increment `right_array_index` by `1`.
# --hints--
You should have `right_array_index += 1` within the `while` loop.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /while\s+right_array_index\s*<\s*len\(right_part\):[^}]*\bright_array_index\s*\+=\s*1\b/);
}
})
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
while left_array_index < len(left_part):
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
sorted_index += 1
--fcc-editable-region--
while right_array_index < len(right_part):
array[sorted_index] = right_part[right_array_index]
--fcc-editable-region--
```
@@ -0,0 +1,66 @@
---
id: 6569b6be44940a8e2a469c31
title: Step 25
challengeType: 20
dashedName: step-25
---
# --description--
For the last step in the `while` loop, increment `sorted_index` by `1`.
# --hints--
You should use the `+=` operator to increment the current value of `sorted_index` by one within the `while` loop.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /while\s+right_array_index\s*<\s*len\(right_part\):[^}]*\bsorted_index\s*\+=\s*1\b/);
}
})
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
while left_array_index < len(left_part):
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
sorted_index += 1
--fcc-editable-region--
while right_array_index < len(right_part):
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
--fcc-editable-region--
```
@@ -0,0 +1,71 @@
---
id: 6569b743630ee592a65a7e2f
title: Step 26
challengeType: 20
dashedName: step-26
---
# --description--
Before testing the `merge_sort()` function, you need to create a base case that stops the function execution when the length of `array` is less than or equal to `1`.
This base case will stop the recursion call. Without it, the merge sort operation would continue to run even when the list has been sorted or has no element in it.
Right after the function declaration, create an `if` statement with this condition: `len(array) <= 1`. Use the `pass` keyword in the function's body.
# --hints--
You should create an `if` statement to check if `len(array) <= 1`. Don't forget to use the `pass` keyword in the body of the `if` statement.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /if\s+len\(array\)\s*<=\s*1:\s*pass/);
}
})
```
# --seed--
## --seed-contents--
```py
--fcc-editable-region--
def merge_sort(array):
--fcc-editable-region--
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
while left_array_index < len(left_part):
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
sorted_index += 1
while right_array_index < len(right_part):
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
```
@@ -0,0 +1,82 @@
---
id: 6569bca4dd9feab7b295a5e1
title: Step 27
challengeType: 20
dashedName: step-27
---
# --description--
Replace the `pass` keyword within the `if` statement with a `return` statement. This will stop the execution of the `merge_sort` function when the given condition is true.
# --hints--
You should replace `pass` with a `return` statement in the body of the `if` statement.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /if\s+len\(array\)\s*<=\s*1:\s*return/);
}
})
```
There should be no `pass` keyword in the body of the `if` statement
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /if\s+len\(array\)\s*<=\s*1:\s*(?!.*\bpass\b).*return/);
}
})
```
# --seed--
## --seed-contents--
```py
--fcc-editable-region--
def merge_sort(array):
if len(array) <= 1:
pass
--fcc-editable-region--
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
while left_array_index < len(left_part):
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
sorted_index += 1
while right_array_index < len(right_part):
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
```
@@ -0,0 +1,71 @@
---
id: 6569beee367427c90c74899e
title: Step 29
challengeType: 20
dashedName: step-29
---
# --description--
It's time to test the `merge_sort` function!
Replace `pass` with a list called `numbers`, and assign this list to it: `[4, 10, 6, 14, 2, 1, 8, 5]`
# --hints--
You should replace `pass` with a `numbers` list with these values: `[4, 10, 6, 14, 2, 1, 8, 5]`
```js
({ test: () => assert.equal(__userGlobals.get("numbers"), "[4, 10, 6, 14, 2, 1, 8, 5]") })
```
You should not have the `pass` keyword in the body of your `if` statement
```js
assert.match(code, /if\s+__name__\s*==\s*'__main__':\s*(?:(?!\bpass\b)[^}]*)*/);
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
if len(array) <= 1:
return
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
while left_array_index < len(left_part):
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
sorted_index += 1
while right_array_index < len(right_part):
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
--fcc-editable-region--
if __name__ == '__main__':
pass
--fcc-editable-region--
```
@@ -0,0 +1,63 @@
---
id: 6569c05b9166f9d5bb36c09e
title: Step 30
challengeType: 20
dashedName: step-30
---
# --description--
Use the `print()` function to print the string `Unsorted array:`.
# --hints--
You should have `print('Unsorted array:')`
```js
({ test: () => assert.match(code, /^\s{4}print\s*\(\s*["']Unsorted array:["']\s*\)/m) })
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
if len(array) <= 1:
return
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
while left_array_index < len(left_part):
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
sorted_index += 1
while right_array_index < len(right_part):
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
--fcc-editable-region--
if __name__ == '__main__':
numbers = [4, 10, 6, 14, 2, 1, 8, 5]
--fcc-editable-region--
```
@@ -0,0 +1,65 @@
---
id: 6569c166d708dcdf7c8fd34c
title: Step 31
challengeType: 20
dashedName: step-31
---
# --description--
Call the `print()` function again to print the `numbers` list. This will print the unsorted list in the console.
# --hints--
You should have `print(numbers)` in your code.
```js
assert.match(code, /^\s{4}print\s*\(\s*numbers\s*\)/m)
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
if len(array) <= 1:
return
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
while left_array_index < len(left_part):
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
sorted_index += 1
while right_array_index < len(right_part):
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
--fcc-editable-region--
if __name__ == '__main__':
numbers = [4, 10, 6, 14, 2, 1, 8, 5]
print('Unsorted array:')
--fcc-editable-region--
```
@@ -0,0 +1,67 @@
---
id: 6569c1aeecaf95e25a3e2573
title: Step 32
challengeType: 20
dashedName: step-32
---
# --description--
After your `print()` calls, call the `merge_sort` function and pass in the `numbers` list as an argument.
# --hints--
You should call the `merge_sort` function passing `numbers` as the argument.
```js
({ test: () => assert.match(code, /merge_sort\s*\(\s*numbers\s*\)/) })
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
if len(array) <= 1:
return
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
while left_array_index < len(left_part):
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
sorted_index += 1
while right_array_index < len(right_part):
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
--fcc-editable-region--
if __name__ == '__main__':
numbers = [4, 10, 6, 14, 2, 1, 8, 5]
print('Unsorted array: ')
print(numbers)
--fcc-editable-region--
```
@@ -0,0 +1,118 @@
---
id: 6569c2cbf6c993ea8cd85682
title: Step 33
challengeType: 20
dashedName: step-33
---
# --description--
At this point, the `numbers` list has been sorted. Call the `print` function to print string `Sorted array: ` and the sorted list.
To do that, concatenate `'Sorted array: '` and `str(numbers)` in the `print()` call.
With that, the merge sort algorithm is complete.
# --hints--
You should have `print('Sorted array: ' + str(numbers))` in your code. Remember to add the space after the colon.
```js
({ test: () => assert.match(code, /^\s{4}print\s*\(\s*("|')Sorted array: \1\s*\+\s*str\s*\(\s*numbers\s*\)\s*\)/m) })
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
if len(array) <= 1:
return
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
while left_array_index < len(left_part):
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
sorted_index += 1
while right_array_index < len(right_part):
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
--fcc-editable-region--
if __name__ == '__main__':
numbers = [4, 10, 6, 14, 2, 1, 8, 5]
print('Unsorted array: ')
print(numbers)
merge_sort(numbers)
--fcc-editable-region--
```
# --solutions--
```py
def merge_sort(array):
if len(array) <= 1:
return
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
while left_array_index < len(left_part):
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
sorted_index += 1
while right_array_index < len(right_part):
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
if __name__ == '__main__':
numbers = [4, 10, 6, 14, 2, 1, 8, 5]
print('Unsorted array: ')
print(numbers)
merge_sort(numbers)
print('Sorted array: ' + str(numbers))
```
@@ -0,0 +1,43 @@
---
id: 6577254891048c8f2c19e961
title: Step 8
challengeType: 20
dashedName: step-8
---
# --description--
Call the `merge_sort()` function again. Do not pass in any argument for now.
# --hints--
You should call `merge_sort()` again after the previous function call.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /merge_sort\s*\(\s*\)/);
}
})
```
# --seed--
## --seed-contents--
```py
--fcc-editable-region--
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
--fcc-editable-region--
```
@@ -0,0 +1,42 @@
---
id: 65772ef923f922cd720e5008
title: Step 7
challengeType: 20
dashedName: step-7
---
# --description--
Pass `left_part` as the argument to the `merge_sort()` call you added in the last step.
# --hints--
You should pass `left_part` as the argument to your `merge_sort()` call.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /merge_sort\s*\(\s*left_part\s*\)/);
}
})
```
# --seed--
## --seed-contents--
```py
--fcc-editable-region--
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort()
--fcc-editable-region--
```
@@ -0,0 +1,43 @@
---
id: 6577320da0d4c2e594d418e2
title: Step 9
challengeType: 20
dashedName: step-9
---
# --description--
Pass `right_part` as the argument to the `merge_sort()` call you added in the last step.
# --hints--
You should pass `right_part` as the argument to your last `merge_sort()` call.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /merge_sort\s*\(\s*right_part\s*\)/);
}
})
```
# --seed--
## --seed-contents--
```py
--fcc-editable-region--
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort()
--fcc-editable-region--
```
@@ -0,0 +1,58 @@
---
id: 6577562501feabdf0984a184
title: Step 14
challengeType: 20
dashedName: step-14
---
# --description--
After assigning the `left_part` index to the sorted array, increment `left_array_index` by `1`.
# --hints--
You should add `1` to the current value of `left_array_index`.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
const allowedMatches = [
/while\s+left_array_index\s*<\s*len\(left_part\)\s+and\s+right_array_index\s*<\s*len\(right_part\):\s*[^}]*if\s+left_part\[left_array_index\]\s*<\s*right_part\[right_array_index\]:\s*[^}]*left_array_index\s*\+=\s*1\s*(?!.*\bpass\b)/,
/while\s+left_array_index\s*<\s*len\(left_part\)\s+and\s+right_array_index\s*<\s*len\(right_part\):\s*[^}]*if\s+left_part\[left_array_index\]\s*<\s*right_part\[right_array_index\]:\s*[^}]*left_array_index\s*=\s*left_array_index\s*\+\s*1\s*(?!.*\bpass\b)/
]
const anyMatch = allowedMatches.some((match) => match.test(function_body));
assert.isTrue(anyMatch);
}
})
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
--fcc-editable-region--
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
--fcc-editable-region--
```
@@ -0,0 +1,62 @@
---
id: 657823a9f4f372518614c8b7
title: Step 16
challengeType: 20
dashedName: step-16
---
# --description--
Still within the `else` block, increment `right_array_index` by `1`.
# --hints--
You should add `1` to the current value of `right_array_index`.
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
const allowedMatches = [
/else:\s*[^}]*right_array_index\s*\+=\s*1/,
/else:\s*[^}]*right_array_index\s*=\s*right_array_index\s*\+\s*1/
]
const anyMatch = allowedMatches.some((match) => match.test(function_body))
assert.isTrue(anyMatch);
}
})
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
--fcc-editable-region--
else:
array[sorted_index] = right_part[right_array_index]
--fcc-editable-region--
```
@@ -0,0 +1,52 @@
---
id: 657f3a661730891aa64f3568
title: Step 5
challengeType: 20
dashedName: step-5
---
# --description--
Use the slice syntax to extract the right half of `array` and assign it to a variable named `right_array`.
# --hints--
You should have a variable named `right_part`
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /right_part\s*(?!=)/m);
}
})
```
You should assign `array[middle_point:]` to the `right_part` variable
```js
({
test: () => {
const transformedCode = code.replace(/\r/g, "");
const merge_sort = __helpers.python.getDef("\n" + transformedCode, "merge_sort");
const { function_body } = merge_sort;
assert.match(function_body, /right_part\s*=\s*array\[middle_point:\]/);
}
})
```
# --seed--
## --seed-contents--
```py
--fcc-editable-region--
def merge_sort(array):
middle_point = len(array) // 2
left_part = array[:middle_point]
--fcc-editable-region--
```
@@ -0,0 +1,76 @@
---
id: 657f59751d5a5c9b069ae0f3
title: Step 28
challengeType: 20
dashedName: step-28
---
# --description--
You can use the `__name__` variable to determine if a Python script is being run as the main program or if it is being imported as a module (code written in another Python file).
If the value of `__name__` is set to `'__main__'`, it implies that the current script is the main program, and not a module.
In this project, you'll use the current script as the main program.
Create an `if` statement that checks whether the value of `__name__` is `'__main__'`.
Use the `pass` keyword in the body of the `if` statement.
# --hints--
Your `if` statement should check if `__name__` has a value of `'__main__'`
```js
assert.match(code, /if\s+__name__\s*==\s*("|')__main__\1\s*:/);
```
You should have the `pass` keyword in the body of your `if` statement
```js
assert.match(code, /if\s+__name__\s*==\s*("|')__main__\1\s*:\s*[^}]*\bpass\b/);
```
# --seed--
## --seed-contents--
```py
def merge_sort(array):
if len(array) <= 1:
return
middle_point = len(array) // 2
left_part = array[:middle_point]
right_part = array[middle_point:]
merge_sort(left_part)
merge_sort(right_part)
left_array_index = 0
right_array_index = 0
sorted_index = 0
while left_array_index < len(left_part) and right_array_index < len(right_part):
if left_part[left_array_index] < right_part[right_array_index]:
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
else:
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
while left_array_index < len(left_part):
array[sorted_index] = left_part[left_array_index]
left_array_index += 1
sorted_index += 1
while right_array_index < len(right_part):
array[sorted_index] = right_part[right_array_index]
right_array_index += 1
sorted_index += 1
--fcc-editable-region--
--fcc-editable-region--
```