Feat: add new Markdown parser (#39800)

and change all the challenges to new `md` format.
This commit is contained in:
Oliver Eyton-Williams
2020-11-27 19:02:05 +01:00
committed by GitHub
parent a07f84c8ec
commit 0bd52f8bd1
2580 changed files with 113436 additions and 111979 deletions

View File

@ -5,48 +5,180 @@ challengeType: 5
forumTopicId: 385263
---
## Description
<section id='description'>
# --description--
Given a string, calculate the frequency of each character.
All characters should be counted. This includes lower and upper case letters, digits, whitespace, special characters, or any other distinct characters.
</section>
## Instructions
<section id='instructions'>
# --instructions--
Write a function to count the occurrences of each character in a given string.
The function should return a 2D array with each of the elements in the following form: <code>['char', freq]</code>. The character should be a string with a length of 1, and frequency is a number denoting the count.
For example, given the string "ab", your function should return <code>[['a', 1], ['b', 1]]</code>.
</section>
## Tests
<section id='tests'>
The function should return a 2D array with each of the elements in the following form: `['char', freq]`. The character should be a string with a length of 1, and frequency is a number denoting the count.
``` yml
tests:
- text: <code>letterFrequency</code> should be a function.
testString: assert(typeof letterFrequency == 'function');
- text: <code>letterFrequency("Not all that Mrs. Bennet, however")</code> should return an array.
testString: assert(Array.isArray(letterFrequency("Not all that Mrs. Bennet, however")));
- text: <code>letterFrequency("Not all that Mrs. Bennet, however")</code> should return <code>[[" ", 5], [", ", 1], [".", 1], ["B", 1], ["M", 1], ["N", 1], ["a", 2], ["e", 4], ["h", 2], ["l", 2], ["n", 2], ["o", 2], ["r", 2], ["s", 1], ["t", 4], ["v", 1], ["w", 1]]</code>.
testString: assert.deepEqual(letterFrequency("Not all that Mrs. Bennet, however"), [[' ', 5], [',', 1], ['.', 1], ['B', 1], ['M', 1], ['N', 1], ['a', 2], ['e', 4], ['h', 2], ['l', 2], ['n', 2], ['o', 2], ['r', 2], ['s', 1], ['t', 4], ['v', 1], ['w', 1]]);
- text: <code>letterFrequency("daughters, could ask on the ")</code> should return <code>[[' ',5],[',',1],['a',2],['c',1],['d',2],['e',2],['g',1],['h',2],['k',1],['l',1],['n',1],['o',2],['r',1],['s',2],['t',2],['u',2]]</code>.
testString: assert.deepEqual(letterFrequency("daughters, could ask on the "), [[' ', 5], [',', 1], ['a', 2], ['c', 1], ['d', 2], ['e', 2], ['g', 1], ['h', 2], ['k', 1], ['l', 1], ['n', 1], ['o', 2], ['r', 1], ['s', 2], ['t', 2], ['u', 2]]);
- text: <code>letterFrequency("husband any satisfactory description")</code> should return <code>[[" ", 3], ["a", 4], ["b", 1], ["c", 2], ["d", 2], ["e", 1], ["f", 1], ["h", 1], ["i", 3], ["n", 3], ["o", 2], ["p", 1], ["r", 2], ["s", 4], ["t", 3], ["u", 1], ["y", 2]]</code>.
testString: assert.deepEqual(letterFrequency("husband any satisfactory description"), [[' ', 3], ['a', 4], ['b', 1], ['c', 2], ['d', 2], ['e', 1], ['f', 1], ['h', 1], ['i', 3], ['n', 3], ['o', 2], ['p', 1], ['r', 2], ['s', 4], ['t', 3], ['u', 1], ['y', 2]]);
- text: <code>letterFrequency("in various ways--with barefaced")</code> should return <code>[[" ", 3], ["-", 2], ["a", 4], ["b", 1], ["c", 1], ["d", 1], ["e", 2], ["f", 1], ["h", 1], ["i", 3], ["n", 1], ["o", 1], ["r", 2], ["s", 2], ["t", 1], ["u", 1], ["v", 1], ["w", 2], ["y", 1]]</code>.
testString: assert.deepEqual(letterFrequency("in various ways--with barefaced"), [[' ', 3], ['-', 2], ['a', 4], ['b', 1], ['c', 1], ['d', 1], ['e', 2], ['f', 1], ['h', 1], ['i', 3], ['n', 1], ['o', 1], ['r', 2], ['s', 2], ['t', 1], ['u', 1], ['v', 1], ['w', 2], ['y', 1]]);
- text: <code>letterFrequency("distant surmises; but he eluded")</code> should return <code>[[" ", 4], ["; ", 1], ["a", 1], ["b", 1], ["d", 3], ["e", 4], ["h", 1], ["i", 2], ["l", 1], ["m", 1], ["n", 1], ["r", 1], ["s", 4], ["t", 3], ["u", 3]]</code>.
testString: assert.deepEqual(letterFrequency("distant surmises; but he eluded"), [[' ', 4], [';', 1], ['a', 1], ['b', 1], ['d', 3], ['e', 4], ['h', 1], ['i', 2], ['l', 1], ['m', 1], ['n', 1], ['r', 1], ['s', 4], ['t', 3], ['u', 3]]);
- text: <code>letterFrequency("last obliged to accept the second-hand,")</code> should return <code>[[" ", 5], [", ", 1], ["-", 1], ["a", 3], ["b", 1], ["c", 3], ["d", 3], ["e", 4], ["g", 1], ["h", 2], ["i", 1], ["l", 2], ["n", 2], ["o", 3], ["p", 1], ["s", 2], ["t", 4]]</code>.
testString: assert.deepEqual(letterFrequency("last obliged to accept the second-hand,"), [[' ', 5], [',', 1], ['-', 1], ['a', 3], ['b', 1], ['c', 3], ['d', 3], ['e', 4], ['g', 1], ['h', 2], ['i', 1], ['l', 2], ['n', 2], ['o', 3], ['p', 1], ['s', 2], ['t', 4]]);
For example, given the string "ab", your function should return `[['a', 1], ['b', 1]]`.
# --hints--
`letterFrequency` should be a function.
```js
assert(typeof letterFrequency == 'function');
```
</section>
`letterFrequency("Not all that Mrs. Bennet, however")` should return an array.
## Challenge Seed
<section id='challengeSeed'>
```js
assert(Array.isArray(letterFrequency('Not all that Mrs. Bennet, however')));
```
<div id='js-seed'>
`letterFrequency("Not all that Mrs. Bennet, however")` should return `[[" ", 5], [", ", 1], [".", 1], ["B", 1], ["M", 1], ["N", 1], ["a", 2], ["e", 4], ["h", 2], ["l", 2], ["n", 2], ["o", 2], ["r", 2], ["s", 1], ["t", 4], ["v", 1], ["w", 1]]`.
```js
assert.deepEqual(letterFrequency('Not all that Mrs. Bennet, however'), [
[' ', 5],
[',', 1],
['.', 1],
['B', 1],
['M', 1],
['N', 1],
['a', 2],
['e', 4],
['h', 2],
['l', 2],
['n', 2],
['o', 2],
['r', 2],
['s', 1],
['t', 4],
['v', 1],
['w', 1]
]);
```
`letterFrequency("daughters, could ask on the ")` should return `[[' ',5],[',',1],['a',2],['c',1],['d',2],['e',2],['g',1],['h',2],['k',1],['l',1],['n',1],['o',2],['r',1],['s',2],['t',2],['u',2]]`.
```js
assert.deepEqual(letterFrequency('daughters, could ask on the '), [
[' ', 5],
[',', 1],
['a', 2],
['c', 1],
['d', 2],
['e', 2],
['g', 1],
['h', 2],
['k', 1],
['l', 1],
['n', 1],
['o', 2],
['r', 1],
['s', 2],
['t', 2],
['u', 2]
]);
```
`letterFrequency("husband any satisfactory description")` should return `[[" ", 3], ["a", 4], ["b", 1], ["c", 2], ["d", 2], ["e", 1], ["f", 1], ["h", 1], ["i", 3], ["n", 3], ["o", 2], ["p", 1], ["r", 2], ["s", 4], ["t", 3], ["u", 1], ["y", 2]]`.
```js
assert.deepEqual(letterFrequency('husband any satisfactory description'), [
[' ', 3],
['a', 4],
['b', 1],
['c', 2],
['d', 2],
['e', 1],
['f', 1],
['h', 1],
['i', 3],
['n', 3],
['o', 2],
['p', 1],
['r', 2],
['s', 4],
['t', 3],
['u', 1],
['y', 2]
]);
```
`letterFrequency("in various ways--with barefaced")` should return `[[" ", 3], ["-", 2], ["a", 4], ["b", 1], ["c", 1], ["d", 1], ["e", 2], ["f", 1], ["h", 1], ["i", 3], ["n", 1], ["o", 1], ["r", 2], ["s", 2], ["t", 1], ["u", 1], ["v", 1], ["w", 2], ["y", 1]]`.
```js
assert.deepEqual(letterFrequency('in various ways--with barefaced'), [
[' ', 3],
['-', 2],
['a', 4],
['b', 1],
['c', 1],
['d', 1],
['e', 2],
['f', 1],
['h', 1],
['i', 3],
['n', 1],
['o', 1],
['r', 2],
['s', 2],
['t', 1],
['u', 1],
['v', 1],
['w', 2],
['y', 1]
]);
```
`letterFrequency("distant surmises; but he eluded")` should return `[[" ", 4], ["; ", 1], ["a", 1], ["b", 1], ["d", 3], ["e", 4], ["h", 1], ["i", 2], ["l", 1], ["m", 1], ["n", 1], ["r", 1], ["s", 4], ["t", 3], ["u", 3]]`.
```js
assert.deepEqual(letterFrequency('distant surmises; but he eluded'), [
[' ', 4],
[';', 1],
['a', 1],
['b', 1],
['d', 3],
['e', 4],
['h', 1],
['i', 2],
['l', 1],
['m', 1],
['n', 1],
['r', 1],
['s', 4],
['t', 3],
['u', 3]
]);
```
`letterFrequency("last obliged to accept the second-hand,")` should return `[[" ", 5], [", ", 1], ["-", 1], ["a", 3], ["b", 1], ["c", 3], ["d", 3], ["e", 4], ["g", 1], ["h", 2], ["i", 1], ["l", 2], ["n", 2], ["o", 3], ["p", 1], ["s", 2], ["t", 4]]`.
```js
assert.deepEqual(letterFrequency('last obliged to accept the second-hand,'), [
[' ', 5],
[',', 1],
['-', 1],
['a', 3],
['b', 1],
['c', 3],
['d', 3],
['e', 4],
['g', 1],
['h', 2],
['i', 1],
['l', 2],
['n', 2],
['o', 3],
['p', 1],
['s', 2],
['t', 4]
]);
```
# --seed--
## --seed-contents--
```js
function letterFrequency(txt) {
@ -54,12 +186,7 @@ function letterFrequency(txt) {
}
```
</div>
</section>
## Solution
<section id='solution'>
# --solutions--
```js
function letterFrequency(txt) {
@ -79,5 +206,3 @@ function letterFrequency(txt) {
return keys.map(function (c) { return [c, dct[c]]; });
}
```
</section>