Feat: add new Markdown parser (#39800)
and change all the challenges to new `md` format.
This commit is contained in:
committed by
GitHub
parent
a07f84c8ec
commit
0bd52f8bd1
@ -5,60 +5,125 @@ challengeType: 5
|
||||
forumTopicId: 301611
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
The mathematical term <dfn>symmetric difference</dfn> (<code>△</code> or <code>⊕</code>) of two sets is the set of elements which are in either of the two sets but not in both. For example, for sets <code>A = {1, 2, 3}</code> and <code>B = {2, 3, 4}</code>, <code>A △ B = {1, 4}</code>.
|
||||
# --description--
|
||||
|
||||
Symmetric difference is a binary operation, which means it operates on only two elements. So to evaluate an expression involving symmetric differences among <em>three</em> elements (<code>A △ B △ C</code>), you must complete one operation at a time. Thus, for sets <code>A</code> and <code>B</code> above, and <code>C = {2, 3}</code>, <code>A △ B △ C = (A △ B) △ C = {1, 4} △ {2, 3} = {1, 2, 3, 4}</code>.
|
||||
</section>
|
||||
The mathematical term <dfn>symmetric difference</dfn> (`△` or `⊕`) of two sets is the set of elements which are in either of the two sets but not in both. For example, for sets `A = {1, 2, 3}` and `B = {2, 3, 4}`, `A △ B = {1, 4}`.
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Create a function that takes two or more arrays and returns an array of their symmetric difference. The returned array must contain only unique values (<em>no duplicates</em>).
|
||||
</section>
|
||||
Symmetric difference is a binary operation, which means it operates on only two elements. So to evaluate an expression involving symmetric differences among *three* elements (`A △ B △ C`), you must complete one operation at a time. Thus, for sets `A` and `B` above, and `C = {2, 3}`, `A △ B △ C = (A △ B) △ C = {1, 4} △ {2, 3} = {1, 2, 3, 4}`.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --instructions--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>sym([1, 2, 3], [5, 2, 1, 4])</code> should return <code>[3, 4, 5]</code>.
|
||||
testString: assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5]);
|
||||
- text: <code>sym([1, 2, 3], [5, 2, 1, 4])</code> should contain only three elements.
|
||||
testString: assert.equal(sym([1, 2, 3], [5, 2, 1, 4]).length, 3);
|
||||
- text: <code>sym([1, 2, 3, 3], [5, 2, 1, 4])</code> should return <code>[3, 4, 5]</code>.
|
||||
testString: assert.sameMembers(sym([1, 2, 3, 3], [5, 2, 1, 4]), [3, 4, 5]);
|
||||
- text: <code>sym([1, 2, 3, 3], [5, 2, 1, 4])</code> should contain only three elements.
|
||||
testString: assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3);
|
||||
- text: <code>sym([1, 2, 3], [5, 2, 1, 4, 5])</code> should return <code>[3, 4, 5]</code>.
|
||||
testString: assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4, 5]), [3, 4, 5]);
|
||||
- text: <code>sym([1, 2, 3], [5, 2, 1, 4, 5])</code> should contain only three elements.
|
||||
testString: assert.equal(sym([1, 2, 3], [5, 2, 1, 4, 5]).length, 3);
|
||||
- text: <code>sym([1, 2, 5], [2, 3, 5], [3, 4, 5])</code> should return <code>[1, 4, 5]</code>
|
||||
testString: assert.sameMembers(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5]);
|
||||
- text: <code>sym([1, 2, 5], [2, 3, 5], [3, 4, 5])</code> should contain only three elements.
|
||||
testString: assert.equal(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]).length, 3);
|
||||
- text: <code>sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])</code> should return <code>[1, 4, 5]</code>.
|
||||
testString: assert.sameMembers(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5]);
|
||||
- text: <code>sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])</code> should contain only three elements.
|
||||
testString: assert.equal(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]).length, 3);
|
||||
- text: <code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])</code> should return <code>[2, 3, 4, 6, 7]</code>.
|
||||
testString: assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]), [2, 3, 4, 6, 7]);
|
||||
- text: <code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])</code> should contain only five elements.
|
||||
testString: assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]).length, 5);
|
||||
- text: <code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])</code> should return <code>[1, 2, 4, 5, 6, 7, 8, 9]</code>.
|
||||
testString: assert.sameMembers(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]), [1, 2, 4, 5, 6, 7, 8, 9]);
|
||||
- text: <code>sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])</code> should contain only eight elements.
|
||||
testString: assert.equal(sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1]).length, 8);
|
||||
Create a function that takes two or more arrays and returns an array of their symmetric difference. The returned array must contain only unique values (*no duplicates*).
|
||||
|
||||
# --hints--
|
||||
|
||||
`sym([1, 2, 3], [5, 2, 1, 4])` should return `[3, 4, 5]`.
|
||||
|
||||
```js
|
||||
assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4]), [3, 4, 5]);
|
||||
```
|
||||
|
||||
</section>
|
||||
`sym([1, 2, 3], [5, 2, 1, 4])` should contain only three elements.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert.equal(sym([1, 2, 3], [5, 2, 1, 4]).length, 3);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`sym([1, 2, 3, 3], [5, 2, 1, 4])` should return `[3, 4, 5]`.
|
||||
|
||||
```js
|
||||
assert.sameMembers(sym([1, 2, 3, 3], [5, 2, 1, 4]), [3, 4, 5]);
|
||||
```
|
||||
|
||||
`sym([1, 2, 3, 3], [5, 2, 1, 4])` should contain only three elements.
|
||||
|
||||
```js
|
||||
assert.equal(sym([1, 2, 3, 3], [5, 2, 1, 4]).length, 3);
|
||||
```
|
||||
|
||||
`sym([1, 2, 3], [5, 2, 1, 4, 5])` should return `[3, 4, 5]`.
|
||||
|
||||
```js
|
||||
assert.sameMembers(sym([1, 2, 3], [5, 2, 1, 4, 5]), [3, 4, 5]);
|
||||
```
|
||||
|
||||
`sym([1, 2, 3], [5, 2, 1, 4, 5])` should contain only three elements.
|
||||
|
||||
```js
|
||||
assert.equal(sym([1, 2, 3], [5, 2, 1, 4, 5]).length, 3);
|
||||
```
|
||||
|
||||
`sym([1, 2, 5], [2, 3, 5], [3, 4, 5])` should return `[1, 4, 5]`
|
||||
|
||||
```js
|
||||
assert.sameMembers(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]), [1, 4, 5]);
|
||||
```
|
||||
|
||||
`sym([1, 2, 5], [2, 3, 5], [3, 4, 5])` should contain only three elements.
|
||||
|
||||
```js
|
||||
assert.equal(sym([1, 2, 5], [2, 3, 5], [3, 4, 5]).length, 3);
|
||||
```
|
||||
|
||||
`sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])` should return `[1, 4, 5]`.
|
||||
|
||||
```js
|
||||
assert.sameMembers(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]), [1, 4, 5]);
|
||||
```
|
||||
|
||||
`sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5])` should contain only three elements.
|
||||
|
||||
```js
|
||||
assert.equal(sym([1, 1, 2, 5], [2, 2, 3, 5], [3, 4, 5, 5]).length, 3);
|
||||
```
|
||||
|
||||
`sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])` should return `[2, 3, 4, 6, 7]`.
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]),
|
||||
[2, 3, 4, 6, 7]
|
||||
);
|
||||
```
|
||||
|
||||
`sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3])` should contain only five elements.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3]).length,
|
||||
5
|
||||
);
|
||||
```
|
||||
|
||||
`sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])` should return `[1, 2, 4, 5, 6, 7, 8, 9]`.
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
sym(
|
||||
[3, 3, 3, 2, 5],
|
||||
[2, 1, 5, 7],
|
||||
[3, 4, 6, 6],
|
||||
[1, 2, 3],
|
||||
[5, 3, 9, 8],
|
||||
[1]
|
||||
),
|
||||
[1, 2, 4, 5, 6, 7, 8, 9]
|
||||
);
|
||||
```
|
||||
|
||||
`sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])` should contain only eight elements.
|
||||
|
||||
```js
|
||||
assert.equal(
|
||||
sym([3, 3, 3, 2, 5], [2, 1, 5, 7], [3, 4, 6, 6], [1, 2, 3], [5, 3, 9, 8], [1])
|
||||
.length,
|
||||
8
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function sym(args) {
|
||||
@ -68,15 +133,7 @@ function sym(args) {
|
||||
sym([1, 2, 3], [5, 2, 1, 4]);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function sym() {
|
||||
@ -89,7 +146,4 @@ function sym() {
|
||||
});
|
||||
}
|
||||
sym([1, 2, 3], [5, 2, 1, 4]);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,58 +5,91 @@ challengeType: 1
|
||||
forumTopicId: 301612
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
This is the first of several challenges on sorting algorithms. Given an array of unsorted items, we want to be able to return a sorted array. We will see several different methods to do this and learn some tradeoffs between these different approaches. While most modern languages have built-in sorting methods for operations like this, it is still important to understand some of the common basic approaches and learn how they can be implemented.
|
||||
|
||||
Here we will see bubble sort. The bubble sort method starts at the beginning of an unsorted array and 'bubbles up' unsorted values towards the end, iterating through the array until it is completely sorted. It does this by comparing adjacent items and swapping them if they are out of order. The method continues looping through the array until no swaps occur at which point the array is sorted.
|
||||
|
||||
This method requires multiple iterations through the array and for average and worst cases has quadratic time complexity. While simple, it is usually impractical in most situations.
|
||||
<strong>Instructions:</strong> Write a function <code>bubbleSort</code> which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest.
|
||||
<strong>Note:</strong><br>We are calling this function from behind the scenes; the test array we are using is commented out in the editor. Try logging <code>array</code> to see your sorting algorithm in action!
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
**Instructions:** Write a function `bubbleSort` which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest.
|
||||
|
||||
</section>
|
||||
**Note:**
|
||||
We are calling this function from behind the scenes; the test array we are using is commented out in the editor. Try logging `array` to see your sorting algorithm in action!
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>bubbleSort</code> should be a function.
|
||||
testString: assert(typeof bubbleSort == 'function');
|
||||
- text: <code>bubbleSort</code> should return a sorted array (least to greatest).
|
||||
testString: assert(isSorted(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])));
|
||||
- text: <code>bubbleSort</code> should return an array that is unchanged except for order.
|
||||
testString: assert.sameMembers(bubbleSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]);
|
||||
- text: <code>bubbleSort</code> should not use the built-in <code>.sort()</code> method.
|
||||
testString: assert(isBuiltInSortUsed());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`bubbleSort` should be a function.
|
||||
|
||||
```js
|
||||
function bubbleSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
bubbleSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
assert(typeof bubbleSort == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
`bubbleSort` should return a sorted array (least to greatest).
|
||||
|
||||
```js
|
||||
assert(
|
||||
isSorted(
|
||||
bubbleSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
])
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`bubbleSort` should return an array that is unchanged except for order.
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
bubbleSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
]),
|
||||
[1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
|
||||
);
|
||||
```
|
||||
|
||||
`bubbleSort` should not use the built-in `.sort()` method.
|
||||
|
||||
```js
|
||||
assert(isBuiltInSortUsed());
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
@ -74,12 +107,19 @@ function isBuiltInSortUsed(){
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
```js
|
||||
function bubbleSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
bubbleSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function bubbleSort(array) {
|
||||
@ -100,5 +140,3 @@ function bubbleSort(array) {
|
||||
return array;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,56 +5,87 @@ challengeType: 1
|
||||
forumTopicId: 301613
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
The next sorting method we'll look at is insertion sort. This method works by building up a sorted array at the beginning of the list. It begins the sorted array with the first element. Then it inspects the next element and swaps it backwards into the sorted array until it is in sorted position. It continues iterating through the list and swapping new items backwards into the sorted portion until it reaches the end. This algorithm has quadratic time complexity in the average and worst cases.
|
||||
<strong>Instructions:</strong> Write a function <code>insertionSort</code> which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest.
|
||||
<strong>Note:</strong><br>We are calling this function from behind the scenes; the test array we are using is commented out in the editor. Try logging <code>array</code> to see your sorting algorithm in action!
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
**Instructions:** Write a function `insertionSort` which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest.
|
||||
|
||||
</section>
|
||||
**Note:**
|
||||
We are calling this function from behind the scenes; the test array we are using is commented out in the editor. Try logging `array` to see your sorting algorithm in action!
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>insertionSort</code> should be a function.
|
||||
testString: assert(typeof insertionSort == 'function');
|
||||
- text: <code>insertionSort</code> should return a sorted array (least to greatest).
|
||||
testString: assert(isSorted(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])));
|
||||
- text: <code>insertionSort</code> should return an array that is unchanged except for order.
|
||||
testString: assert.sameMembers(insertionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]);
|
||||
- text: <code>insertionSort</code> should not use the built-in <code>.sort()</code> method.
|
||||
testString: assert(isBuiltInSortUsed());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`insertionSort` should be a function.
|
||||
|
||||
```js
|
||||
function insertionSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
insertionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
assert(typeof insertionSort == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
`insertionSort` should return a sorted array (least to greatest).
|
||||
|
||||
```js
|
||||
assert(
|
||||
isSorted(
|
||||
insertionSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
])
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`insertionSort` should return an array that is unchanged except for order.
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
insertionSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
]),
|
||||
[1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
|
||||
);
|
||||
```
|
||||
|
||||
`insertionSort` should not use the built-in `.sort()` method.
|
||||
|
||||
```js
|
||||
assert(isBuiltInSortUsed());
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
@ -72,12 +103,19 @@ function isBuiltInSortUsed(){
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
```js
|
||||
function insertionSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
insertionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function insertionSort (array) {
|
||||
@ -93,5 +131,3 @@ function insertionSort (array) {
|
||||
return array;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,60 +5,95 @@ challengeType: 1
|
||||
forumTopicId: 301614
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
Another common intermediate sorting algorithm is merge sort. Like quick sort, merge sort also uses a divide-and-conquer, recursive methodology to sort an array. It takes advantage of the fact that it is relatively easy to sort two arrays as long as each is sorted in the first place. But we'll start with only one array as input, so how do we get to two sorted arrays from that? Well, we can recursively divide the original input in two until we reach the base case of an array with one item. A single-item array is naturally sorted, so then we can start combining. This combination will unwind the recursive calls that split the original array, eventually producing a final sorted array of all the elements. The steps of merge sort, then, are:
|
||||
<strong>1)</strong> Recursively split the input array in half until a sub-array with only one element is produced.
|
||||
<strong>2)</strong> Merge each sorted sub-array together to produce the final sorted array.
|
||||
Merge sort is an efficient sorting method, with time complexity of <i>O(nlog(n))</i>. This algorithm is popular because it is performant and relatively easy to implement.
|
||||
|
||||
**1)** Recursively split the input array in half until a sub-array with only one element is produced.
|
||||
|
||||
**2)** Merge each sorted sub-array together to produce the final sorted array.
|
||||
|
||||
Merge sort is an efficient sorting method, with time complexity of *O(nlog(n))*. This algorithm is popular because it is performant and relatively easy to implement.
|
||||
|
||||
As an aside, this will be the last sorting algorithm we cover here. However, later in the section on tree data structures we will describe heap sort, another efficient sorting method that requires a binary heap in its implementation.
|
||||
<strong>Instructions:</strong> Write a function <code>mergeSort</code> which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest. A good way to implement this is to write one function, for instance <code>merge</code>, which is responsible for merging two sorted arrays, and another function, for instance <code>mergeSort</code>, which is responsible for the recursion that produces single-item arrays to feed into merge. Good luck!
|
||||
<strong>Note:</strong><br>We are calling this function from behind the scenes; the test array we are using is commented out in the editor. Try logging <code>array</code> to see your sorting algorithm in action!
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
**Instructions:** Write a function `mergeSort` which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest. A good way to implement this is to write one function, for instance `merge`, which is responsible for merging two sorted arrays, and another function, for instance `mergeSort`, which is responsible for the recursion that produces single-item arrays to feed into merge. Good luck!
|
||||
|
||||
</section>
|
||||
**Note:**
|
||||
We are calling this function from behind the scenes; the test array we are using is commented out in the editor. Try logging `array` to see your sorting algorithm in action!
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>mergeSort</code> should be a function.
|
||||
testString: assert(typeof mergeSort == 'function');
|
||||
- text: <code>mergeSort</code> should return a sorted array (least to greatest).
|
||||
testString: assert(isSorted(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])));
|
||||
- text: <code>mergeSort</code> should return an array that is unchanged except for order.
|
||||
testString: assert.sameMembers(mergeSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]);
|
||||
- text: <code>mergeSort</code> should not use the built-in <code>.sort()</code> method.
|
||||
testString: assert(isBuiltInSortUsed());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`mergeSort` should be a function.
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
mergeSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
assert(typeof mergeSort == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
`mergeSort` should return a sorted array (least to greatest).
|
||||
|
||||
```js
|
||||
assert(
|
||||
isSorted(
|
||||
mergeSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
])
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`mergeSort` should return an array that is unchanged except for order.
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
mergeSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
]),
|
||||
[1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
|
||||
);
|
||||
```
|
||||
|
||||
`mergeSort` should not use the built-in `.sort()` method.
|
||||
|
||||
```js
|
||||
assert(isBuiltInSortUsed());
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
@ -76,12 +111,19 @@ function isBuiltInSortUsed(){
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
mergeSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function mergeSort(array) {
|
||||
@ -116,5 +158,3 @@ function mergeSort(array) {
|
||||
|
||||
mergeSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,54 +5,86 @@ challengeType: 1
|
||||
forumTopicId: 301615
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
Here we will move on to an intermediate sorting algorithm: quick sort. Quick sort is an efficient, recursive divide-and-conquer approach to sorting an array. In this method, a pivot value is chosen in the original array. The array is then partitioned into two subarrays of values less than and greater than the pivot value. We then combine the result of recursively calling the quick sort algorithm on both sub-arrays. This continues until the base case of an empty or single-item array is reached, which we return. The unwinding of the recursive calls return us the sorted array.
|
||||
Quick sort is a very efficient sorting method, providing <i>O(nlog(n))</i> performance on average. It is also relatively easy to implement. These attributes make it a popular and useful sorting method.
|
||||
<strong>Instructions:</strong> Write a function <code>quickSort</code> which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest. While the choice of the pivot value is important, any pivot will do for our purposes here. For simplicity, the first or last element could be used.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Quick sort is a very efficient sorting method, providing *O(nlog(n))* performance on average. It is also relatively easy to implement. These attributes make it a popular and useful sorting method.
|
||||
|
||||
</section>
|
||||
**Instructions:** Write a function `quickSort` which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest. While the choice of the pivot value is important, any pivot will do for our purposes here. For simplicity, the first or last element could be used.
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>quickSort</code> should be a function.
|
||||
testString: assert(typeof quickSort == 'function');
|
||||
- text: <code>quickSort</code> should return a sorted array (least to greatest).
|
||||
testString: assert(isSorted(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])));
|
||||
- text: <code>quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])</code> should return an array that is unchanged except for order.
|
||||
testString: assert.sameMembers(quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]);
|
||||
- text: <code>quickSort</code> should not use the built-in <code>.sort()</code> method.
|
||||
testString: assert(isBuiltInSortUsed());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`quickSort` should be a function.
|
||||
|
||||
```js
|
||||
function quickSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
assert(typeof quickSort == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
`quickSort` should return a sorted array (least to greatest).
|
||||
|
||||
```js
|
||||
assert(
|
||||
isSorted(
|
||||
quickSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
])
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`quickSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])` should return an array that is unchanged except for order.
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
quickSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
]),
|
||||
[1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
|
||||
);
|
||||
```
|
||||
|
||||
`quickSort` should not use the built-in `.sort()` method.
|
||||
|
||||
```js
|
||||
assert(isBuiltInSortUsed());
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
@ -70,12 +102,17 @@ function isBuiltInSortUsed(){
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
```js
|
||||
function quickSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
```
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function quickSort(array) {
|
||||
@ -102,5 +139,3 @@ function quickSort(array) {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,57 +5,87 @@ challengeType: 1
|
||||
forumTopicId: 301616
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
Here we will implement selection sort. Selection sort works by selecting the minimum value in a list and swapping it with the first value in the list. It then starts at the second position, selects the smallest value in the remaining list, and swaps it with the second element. It continues iterating through the list and swapping elements until it reaches the end of the list. Now the list is sorted. Selection sort has quadratic time complexity in all cases.
|
||||
<strong>Instructions</strong>: Write a function <code>selectionSort</code> which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest.
|
||||
<strong>Note:</strong><br>We are calling this function from behind the scenes; the test array we are using is commented out in the editor. Try logging <code>array</code> to see your sorting algorithm in action!
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
**Instructions**: Write a function `selectionSort` which takes an array of integers as input and returns an array of these integers in sorted order from least to greatest.
|
||||
|
||||
</section>
|
||||
**Note:**
|
||||
We are calling this function from behind the scenes; the test array we are using is commented out in the editor. Try logging `array` to see your sorting algorithm in action!
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
# --hints--
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>selectionSort</code> should be a function.
|
||||
testString: assert(typeof selectionSort == 'function');
|
||||
- text: <code>selectionSort</code> should return a sorted array (least to greatest).
|
||||
testString: assert(isSorted(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92])));
|
||||
- text: <code>selectionSort</code> should return an array that is unchanged except for order.
|
||||
testString: assert.sameMembers(selectionSort([1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]), [1,4,2,8,345,123,43,32,5643,63,123,43,2,55,1,234,92]);
|
||||
- text: <code>selectionSort</code> should not use the built-in <code>.sort()</code> method.
|
||||
testString: assert(isBuiltInSortUsed());
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
|
||||
<div id='js-seed'>
|
||||
`selectionSort` should be a function.
|
||||
|
||||
```js
|
||||
function selectionSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
|
||||
selectionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
assert(typeof selectionSort == 'function');
|
||||
```
|
||||
|
||||
</div>
|
||||
`selectionSort` should return a sorted array (least to greatest).
|
||||
|
||||
```js
|
||||
assert(
|
||||
isSorted(
|
||||
selectionSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
])
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
### After Test
|
||||
<div id='js-teardown'>
|
||||
`selectionSort` should return an array that is unchanged except for order.
|
||||
|
||||
```js
|
||||
assert.sameMembers(
|
||||
selectionSort([
|
||||
1,
|
||||
4,
|
||||
2,
|
||||
8,
|
||||
345,
|
||||
123,
|
||||
43,
|
||||
32,
|
||||
5643,
|
||||
63,
|
||||
123,
|
||||
43,
|
||||
2,
|
||||
55,
|
||||
1,
|
||||
234,
|
||||
92
|
||||
]),
|
||||
[1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]
|
||||
);
|
||||
```
|
||||
|
||||
`selectionSort` should not use the built-in `.sort()` method.
|
||||
|
||||
```js
|
||||
assert(isBuiltInSortUsed());
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --after-user-code--
|
||||
|
||||
```js
|
||||
function isSorted(a){
|
||||
@ -73,12 +103,20 @@ function isBuiltInSortUsed(){
|
||||
}
|
||||
```
|
||||
|
||||
</div>
|
||||
## --seed-contents--
|
||||
|
||||
</section>
|
||||
```js
|
||||
function selectionSort(array) {
|
||||
// Only change code below this line
|
||||
return array;
|
||||
// Only change code above this line
|
||||
}
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
selectionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
|
||||
```
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function selectionSort(array) {
|
||||
@ -96,5 +134,3 @@ function selectionSort(array) {
|
||||
return array;
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,42 +5,160 @@ challengeType: 5
|
||||
forumTopicId: 16019
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
Compare and update the inventory stored in a 2D array against a second 2D array of a fresh delivery. Update the current existing inventory item quantities (in <code>arr1</code>). If an item cannot be found, add the new item and quantity into the inventory array. The returned inventory array should be in alphabetical order by item.
|
||||
</section>
|
||||
# --description--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
Compare and update the inventory stored in a 2D array against a second 2D array of a fresh delivery. Update the current existing inventory item quantities (in `arr1`). If an item cannot be found, add the new item and quantity into the inventory array. The returned inventory array should be in alphabetical order by item.
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: The function <code>updateInventory</code> should return an array.
|
||||
testString: assert.isArray(updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]));
|
||||
- text: <code>updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])</code> should return an array with a length of 6.
|
||||
testString: assert.equal(updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]).length, 6);
|
||||
- text: <code>updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])</code> should return <code>[[88, "Bowling Ball"], [2, "Dirty Sock"], [3, "Hair Pin"], [3, "Half-Eaten Apple"], [5, "Microphone"], [7, "Toothpaste"]]</code>.
|
||||
testString: assert.deepEqual(updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]), [[88, "Bowling Ball"], [2, "Dirty Sock"], [3, "Hair Pin"], [3, "Half-Eaten Apple"], [5, "Microphone"], [7, "Toothpaste"]]);
|
||||
- text: <code>updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [])</code> should return <code>[[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]]</code>.
|
||||
testString: assert.deepEqual(updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], []), [[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]]);
|
||||
- text: <code>updateInventory([], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])</code> should return <code>[[67, "Bowling Ball"], [2, "Hair Pin"], [3, "Half-Eaten Apple"], [7, "Toothpaste"]]</code>.
|
||||
testString: assert.deepEqual(updateInventory([], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]]), [[67, "Bowling Ball"], [2, "Hair Pin"], [3, "Half-Eaten Apple"], [7, "Toothpaste"]]);
|
||||
- text: <code>updateInventory([[0, "Bowling Ball"], [0, "Dirty Sock"], [0, "Hair Pin"], [0, "Microphone"]], [[1, "Hair Pin"], [1, "Half-Eaten Apple"], [1, "Bowling Ball"], [1, "Toothpaste"]])</code> should return <code>[[1, "Bowling Ball"], [0, "Dirty Sock"], [1, "Hair Pin"], [1, "Half-Eaten Apple"], [0, "Microphone"], [1, "Toothpaste"]]</code>.
|
||||
testString: assert.deepEqual(updateInventory([[0, "Bowling Ball"], [0, "Dirty Sock"], [0, "Hair Pin"], [0, "Microphone"]], [[1, "Hair Pin"], [1, "Half-Eaten Apple"], [1, "Bowling Ball"], [1, "Toothpaste"]]), [[1, "Bowling Ball"], [0, "Dirty Sock"], [1, "Hair Pin"], [1, "Half-Eaten Apple"], [0, "Microphone"], [1, "Toothpaste"]]);
|
||||
The function `updateInventory` should return an array.
|
||||
|
||||
```js
|
||||
assert.isArray(
|
||||
updateInventory(
|
||||
[
|
||||
[21, 'Bowling Ball'],
|
||||
[2, 'Dirty Sock'],
|
||||
[1, 'Hair Pin'],
|
||||
[5, 'Microphone']
|
||||
],
|
||||
[
|
||||
[2, 'Hair Pin'],
|
||||
[3, 'Half-Eaten Apple'],
|
||||
[67, 'Bowling Ball'],
|
||||
[7, 'Toothpaste']
|
||||
]
|
||||
)
|
||||
);
|
||||
```
|
||||
|
||||
</section>
|
||||
`updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])` should return an array with a length of 6.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert.equal(
|
||||
updateInventory(
|
||||
[
|
||||
[21, 'Bowling Ball'],
|
||||
[2, 'Dirty Sock'],
|
||||
[1, 'Hair Pin'],
|
||||
[5, 'Microphone']
|
||||
],
|
||||
[
|
||||
[2, 'Hair Pin'],
|
||||
[3, 'Half-Eaten Apple'],
|
||||
[67, 'Bowling Ball'],
|
||||
[7, 'Toothpaste']
|
||||
]
|
||||
).length,
|
||||
6
|
||||
);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])` should return `[[88, "Bowling Ball"], [2, "Dirty Sock"], [3, "Hair Pin"], [3, "Half-Eaten Apple"], [5, "Microphone"], [7, "Toothpaste"]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
updateInventory(
|
||||
[
|
||||
[21, 'Bowling Ball'],
|
||||
[2, 'Dirty Sock'],
|
||||
[1, 'Hair Pin'],
|
||||
[5, 'Microphone']
|
||||
],
|
||||
[
|
||||
[2, 'Hair Pin'],
|
||||
[3, 'Half-Eaten Apple'],
|
||||
[67, 'Bowling Ball'],
|
||||
[7, 'Toothpaste']
|
||||
]
|
||||
),
|
||||
[
|
||||
[88, 'Bowling Ball'],
|
||||
[2, 'Dirty Sock'],
|
||||
[3, 'Hair Pin'],
|
||||
[3, 'Half-Eaten Apple'],
|
||||
[5, 'Microphone'],
|
||||
[7, 'Toothpaste']
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
`updateInventory([[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]], [])` should return `[[21, "Bowling Ball"], [2, "Dirty Sock"], [1, "Hair Pin"], [5, "Microphone"]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
updateInventory(
|
||||
[
|
||||
[21, 'Bowling Ball'],
|
||||
[2, 'Dirty Sock'],
|
||||
[1, 'Hair Pin'],
|
||||
[5, 'Microphone']
|
||||
],
|
||||
[]
|
||||
),
|
||||
[
|
||||
[21, 'Bowling Ball'],
|
||||
[2, 'Dirty Sock'],
|
||||
[1, 'Hair Pin'],
|
||||
[5, 'Microphone']
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
`updateInventory([], [[2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"]])` should return `[[67, "Bowling Ball"], [2, "Hair Pin"], [3, "Half-Eaten Apple"], [7, "Toothpaste"]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
updateInventory(
|
||||
[],
|
||||
[
|
||||
[2, 'Hair Pin'],
|
||||
[3, 'Half-Eaten Apple'],
|
||||
[67, 'Bowling Ball'],
|
||||
[7, 'Toothpaste']
|
||||
]
|
||||
),
|
||||
[
|
||||
[67, 'Bowling Ball'],
|
||||
[2, 'Hair Pin'],
|
||||
[3, 'Half-Eaten Apple'],
|
||||
[7, 'Toothpaste']
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
`updateInventory([[0, "Bowling Ball"], [0, "Dirty Sock"], [0, "Hair Pin"], [0, "Microphone"]], [[1, "Hair Pin"], [1, "Half-Eaten Apple"], [1, "Bowling Ball"], [1, "Toothpaste"]])` should return `[[1, "Bowling Ball"], [0, "Dirty Sock"], [1, "Hair Pin"], [1, "Half-Eaten Apple"], [0, "Microphone"], [1, "Toothpaste"]]`.
|
||||
|
||||
```js
|
||||
assert.deepEqual(
|
||||
updateInventory(
|
||||
[
|
||||
[0, 'Bowling Ball'],
|
||||
[0, 'Dirty Sock'],
|
||||
[0, 'Hair Pin'],
|
||||
[0, 'Microphone']
|
||||
],
|
||||
[
|
||||
[1, 'Hair Pin'],
|
||||
[1, 'Half-Eaten Apple'],
|
||||
[1, 'Bowling Ball'],
|
||||
[1, 'Toothpaste']
|
||||
]
|
||||
),
|
||||
[
|
||||
[1, 'Bowling Ball'],
|
||||
[0, 'Dirty Sock'],
|
||||
[1, 'Hair Pin'],
|
||||
[1, 'Half-Eaten Apple'],
|
||||
[0, 'Microphone'],
|
||||
[1, 'Toothpaste']
|
||||
]
|
||||
);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function updateInventory(arr1, arr2) {
|
||||
@ -65,15 +183,7 @@ var newInv = [
|
||||
updateInventory(curInv, newInv);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function updateInventory(arr1, arr2) {
|
||||
@ -114,7 +224,4 @@ var newInv = [
|
||||
];
|
||||
|
||||
updateInventory(curInv, newInv);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,51 +5,77 @@ challengeType: 5
|
||||
forumTopicId: 16037
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
Return the number of total permutations of the provided string that don't have repeated consecutive letters. Assume that all characters in the provided string are each unique.
|
||||
For example, <code>aab</code> should return 2 because it has 6 total permutations (<code>aab</code>, <code>aab</code>, <code>aba</code>, <code>aba</code>, <code>baa</code>, <code>baa</code>), but only 2 of them (<code>aba</code> and <code>aba</code>) don't have the same letter (in this case <code>a</code>) repeating.
|
||||
</section>
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
For example, `aab` should return 2 because it has 6 total permutations (`aab`, `aab`, `aba`, `aba`, `baa`, `baa`), but only 2 of them (`aba` and `aba`) don't have the same letter (in this case `a`) repeating.
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>permAlone("aab")</code> should return a number.
|
||||
testString: assert.isNumber(permAlone('aab'));
|
||||
- text: <code>permAlone("aab")</code> should return 2.
|
||||
testString: assert.strictEqual(permAlone('aab'), 2);
|
||||
- text: <code>permAlone("aaa")</code> should return 0.
|
||||
testString: assert.strictEqual(permAlone('aaa'), 0);
|
||||
- text: <code>permAlone("aabb")</code> should return 8.
|
||||
testString: assert.strictEqual(permAlone('aabb'), 8);
|
||||
- text: <code>permAlone("abcdefa")</code> should return 3600.
|
||||
testString: assert.strictEqual(permAlone('abcdefa'), 3600);
|
||||
- text: <code>permAlone("abfdefa")</code> should return 2640.
|
||||
testString: assert.strictEqual(permAlone('abfdefa'), 2640);
|
||||
- text: <code>permAlone("zzzzzzzz")</code> should return 0.
|
||||
testString: assert.strictEqual(permAlone('zzzzzzzz'), 0);
|
||||
- text: <code>permAlone("a")</code> should return 1.
|
||||
testString: assert.strictEqual(permAlone('a'), 1);
|
||||
- text: <code>permAlone("aaab")</code> should return 0.
|
||||
testString: assert.strictEqual(permAlone('aaab'), 0);
|
||||
- text: <code>permAlone("aaabb")</code> should return 12.
|
||||
testString: assert.strictEqual(permAlone('aaabb'), 12);
|
||||
`permAlone("aab")` should return a number.
|
||||
|
||||
```js
|
||||
assert.isNumber(permAlone('aab'));
|
||||
```
|
||||
|
||||
</section>
|
||||
`permAlone("aab")` should return 2.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert.strictEqual(permAlone('aab'), 2);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`permAlone("aaa")` should return 0.
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('aaa'), 0);
|
||||
```
|
||||
|
||||
`permAlone("aabb")` should return 8.
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('aabb'), 8);
|
||||
```
|
||||
|
||||
`permAlone("abcdefa")` should return 3600.
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('abcdefa'), 3600);
|
||||
```
|
||||
|
||||
`permAlone("abfdefa")` should return 2640.
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('abfdefa'), 2640);
|
||||
```
|
||||
|
||||
`permAlone("zzzzzzzz")` should return 0.
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('zzzzzzzz'), 0);
|
||||
```
|
||||
|
||||
`permAlone("a")` should return 1.
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('a'), 1);
|
||||
```
|
||||
|
||||
`permAlone("aaab")` should return 0.
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('aaab'), 0);
|
||||
```
|
||||
|
||||
`permAlone("aaabb")` should return 12.
|
||||
|
||||
```js
|
||||
assert.strictEqual(permAlone('aaabb'), 12);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function permAlone(str) {
|
||||
@ -59,15 +85,7 @@ function permAlone(str) {
|
||||
permAlone('aab');
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function permAlone(str) {
|
||||
@ -104,7 +122,4 @@ function permuter(str) {
|
||||
}
|
||||
|
||||
permAlone('aab');
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
@ -5,20 +5,19 @@ challengeType: 5
|
||||
forumTopicId: 301617
|
||||
---
|
||||
|
||||
## Description
|
||||
<section id='description'>
|
||||
# --description--
|
||||
|
||||
Given an array `arr`, find element pairs whose sum equal the second argument `arg` and return the sum of their indices.
|
||||
|
||||
You may use multiple pairs that have the same numeric elements but different indices. Each pair should use the lowest possible available indices. Once an element has been used it cannot be reused to pair with another element. For instance, `pairwise([1, 1, 2], 3)` creates a pair `[2, 1]` using the 1 at index 0 rather than the 1 at index 1, because 0+2 < 1+2.
|
||||
You may use multiple pairs that have the same numeric elements but different indices. Each pair should use the lowest possible available indices. Once an element has been used it cannot be reused to pair with another element. For instance, `pairwise([1, 1, 2], 3)` creates a pair `[2, 1]` using the 1 at index 0 rather than the 1 at index 1, because 0+2 < 1+2.
|
||||
|
||||
For example `pairwise([7, 9, 11, 13, 15], 20)` returns `6`. The pairs that sum to 20 are `[7, 13]` and `[9, 11]`. We can then write out the array with their indices and values.
|
||||
|
||||
<div style='margin-left: 2em;'>
|
||||
|
||||
|Index|0|1|2|3|4|
|
||||
|--- |--- |--- |--- |--- |--- |
|
||||
|Value|7|9|11|13|15|
|
||||
| Index | 0 | 1 | 2 | 3 | 4 |
|
||||
| ----- | - | - | -- | -- | -- |
|
||||
| Value | 7 | 9 | 11 | 13 | 15 |
|
||||
|
||||
</div>
|
||||
|
||||
@ -26,43 +25,47 @@ Below we'll take their corresponding indices and add them.
|
||||
|
||||
<div style='margin-left: 2em;'>
|
||||
|
||||
7 + 13 = 20 → Indices 0 + 3 = 3<br>
|
||||
9 + 11 = 20 → Indices 1 + 2 = 3<br>
|
||||
3 + 3 = 6 → Return `6`
|
||||
|
||||
7 + 13 = 20 → Indices 0 + 3 = 3
|
||||
9 + 11 = 20 → Indices 1 + 2 = 3
|
||||
3 + 3 = 6 → Return `6`
|
||||
|
||||
</div>
|
||||
|
||||
</section>
|
||||
# --hints--
|
||||
|
||||
## Instructions
|
||||
<section id='instructions'>
|
||||
|
||||
</section>
|
||||
|
||||
## Tests
|
||||
<section id='tests'>
|
||||
|
||||
```yml
|
||||
tests:
|
||||
- text: <code>pairwise([1, 4, 2, 3, 0, 5], 7)</code> should return 11.
|
||||
testString: assert.deepEqual(pairwise([1, 4, 2, 3, 0, 5], 7), 11);
|
||||
- text: <code>pairwise([1, 3, 2, 4], 4)</code> should return 1.
|
||||
testString: assert.deepEqual(pairwise([1, 3, 2, 4], 4), 1);
|
||||
- text: <code>pairwise([1, 1, 1], 2)</code> should return 1.
|
||||
testString: assert.deepEqual(pairwise([1, 1, 1], 2), 1);
|
||||
- text: <code>pairwise([0, 0, 0, 0, 1, 1], 1)</code> should return 10.
|
||||
testString: assert.deepEqual(pairwise([0, 0, 0, 0, 1, 1], 1), 10);
|
||||
- text: <code>pairwise([], 100)</code> should return 0.
|
||||
testString: assert.deepEqual(pairwise([], 100), 0);
|
||||
`pairwise([1, 4, 2, 3, 0, 5], 7)` should return 11.
|
||||
|
||||
```js
|
||||
assert.deepEqual(pairwise([1, 4, 2, 3, 0, 5], 7), 11);
|
||||
```
|
||||
|
||||
</section>
|
||||
`pairwise([1, 3, 2, 4], 4)` should return 1.
|
||||
|
||||
## Challenge Seed
|
||||
<section id='challengeSeed'>
|
||||
```js
|
||||
assert.deepEqual(pairwise([1, 3, 2, 4], 4), 1);
|
||||
```
|
||||
|
||||
<div id='js-seed'>
|
||||
`pairwise([1, 1, 1], 2)` should return 1.
|
||||
|
||||
```js
|
||||
assert.deepEqual(pairwise([1, 1, 1], 2), 1);
|
||||
```
|
||||
|
||||
`pairwise([0, 0, 0, 0, 1, 1], 1)` should return 10.
|
||||
|
||||
```js
|
||||
assert.deepEqual(pairwise([0, 0, 0, 0, 1, 1], 1), 10);
|
||||
```
|
||||
|
||||
`pairwise([], 100)` should return 0.
|
||||
|
||||
```js
|
||||
assert.deepEqual(pairwise([], 100), 0);
|
||||
```
|
||||
|
||||
# --seed--
|
||||
|
||||
## --seed-contents--
|
||||
|
||||
```js
|
||||
function pairwise(arr, arg) {
|
||||
@ -72,15 +75,7 @@ function pairwise(arr, arg) {
|
||||
pairwise([1,4,2,3,0,5], 7);
|
||||
```
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
</section>
|
||||
|
||||
## Solution
|
||||
<section id='solution'>
|
||||
|
||||
# --solutions--
|
||||
|
||||
```js
|
||||
function pairwise(arr, arg) {
|
||||
@ -101,7 +96,4 @@ function pairwise(arr, arg) {
|
||||
}
|
||||
|
||||
pairwise([1,4,2,3,0,5], 7);
|
||||
|
||||
```
|
||||
|
||||
</section>
|
||||
|
Reference in New Issue
Block a user