Skip to content

Commit 129805a

Browse files
[New Exercise] Parallel letter frequency (exercism#2379)
* Auto-generated files for parallel-letter-frequency exercise * Adding tests and example solution * Update proof.ci.js * Added some credits, prerequisites and fixed linting errors * formatting * Removing unrelated changes * Satisfying the CI checks, hopefully * Update exercises/practice/parallel-letter-frequency/parallel-letter-frequency.js Co-authored-by: Erik Schierboom <[email protected]> * adding an append to the docs * Update instructions.append.md --------- Co-authored-by: Erik Schierboom <[email protected]>
1 parent bd2149b commit 129805a

14 files changed

+423
-0
lines changed

Diff for: config.json

+14
Original file line numberDiff line numberDiff line change
@@ -2549,6 +2549,20 @@
25492549
"basics"
25502550
],
25512551
"difficulty": 2
2552+
},
2553+
{
2554+
"slug": "parallel-letter-frequency",
2555+
"name": "Parallel Letter Frequency",
2556+
"uuid": "63126f78-ba0d-4271-978d-49e9312f0db2",
2557+
"practices": [],
2558+
"prerequisites": [
2559+
"strings",
2560+
"regular-expressions",
2561+
"rest-and-spread",
2562+
"arrow-functions",
2563+
"basics"
2564+
],
2565+
"difficulty": 7
25522566
}
25532567
]
25542568
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Instructions append
2+
3+
Due to the single-threaded nature of Javascript, code that appears to execute in parallel,
4+
such as `async functions` or `Promises`, actually execute concurrently.
5+
Such solutions will pass all the tests, even though they do not meet the requrement for parallel execution.
6+
7+
## Concurency vs. Parallelism
8+
9+
Here's a quick definition for each that illustrates the diferences between the two:
10+
11+
- Concurrency is when two or more tasks can start, run and complete in overlapping time periods, being executed by the same processing unit.
12+
- Parallelism is when two or more tasks can start and run at the same time, being executed independently of eachother by separate processing units.
13+
14+
## Parallelism in Javascript
15+
16+
Even though Javascript by default is single-threaded, there is a way to execute code non-concurently,
17+
through the [Web Worker API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API).
18+
19+
As described by MDN:
20+
21+
> Web Workers makes it possible to run a script operation in a background thread separate from the main execution thread of a web application.
22+
23+
Here's a simple demo (taken from [here](https://medium.com/@ns-tech-learn/what-is-a-web-worker-how-to-use-it-and-example-2273de521f04))
24+
25+
```js
26+
// main.js
27+
const myWorker = new Worker('worker.js');
28+
29+
myWorker.postMessage(5);
30+
31+
myWorker.onmessage = function (event) {
32+
console.log('Received result from worker:', event.data);
33+
};
34+
```
35+
36+
```js
37+
// worker.js
38+
onmessage = function (event) {
39+
console.log('Received number from main thread:', event.data);
40+
41+
// Perform computation
42+
const result = event.data * 2;
43+
44+
// Send result back to the main thread
45+
postMessage(result);
46+
};
47+
```
48+
49+
As a stretch goal, consider if your implementation can be adapted to make use of `Web workers`.
50+
51+
---
52+
53+
## Further reading
54+
55+
- [MDN demo](https://mdn.github.io/dom-examples/web-workers/simple-web-worker/)
56+
- [MDN - Web Workers](https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers)
57+
- [Article about multi-threading in JS](https://medium.com/techtrument/multithreading-javascript-46156179cf9a)
58+
- [Web Worker primer](https://medium.com/@ns-tech-learn/what-is-a-web-worker-how-to-use-it-and-example-2273de521f04)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Instructions
2+
3+
Count the frequency of letters in texts using parallel computation.
4+
5+
Parallelism is about doing things in parallel that can also be done sequentially.
6+
A common example is counting the frequency of letters.
7+
Create a function that returns the total frequency of each letter in a list of texts and that employs parallelism.
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"root": true,
3+
"extends": "@exercism/eslint-config-javascript",
4+
"env": {
5+
"jest": true
6+
},
7+
"overrides": [
8+
{
9+
"files": [".meta/proof.ci.js", ".meta/exemplar.js", "*.spec.js"],
10+
"excludedFiles": ["custom.spec.js"],
11+
"extends": "@exercism/eslint-config-javascript/maintainers"
12+
}
13+
]
14+
}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/node_modules
2+
/bin/configlet
3+
/bin/configlet.exe
4+
/pnpm-lock.yaml
5+
/yarn.lock
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"Cool-Katt"
4+
],
5+
"files": {
6+
"solution": [
7+
"parallel-letter-frequency.js"
8+
],
9+
"test": [
10+
"parallel-letter-frequency.spec.js"
11+
],
12+
"example": [
13+
".meta/proof.ci.js"
14+
]
15+
},
16+
"blurb": "Count the frequency of letters in texts using parallel computation."
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export const parallelLetterFrequency = (texts) => {
2+
let result = {};
3+
let formatedTexts = texts
4+
.map((x) => x.toLowerCase().match(/\p{Letter}+/gu) ?? [])
5+
.flat();
6+
Promise.all(formatedTexts.map((t) => processSingleText(t, result)));
7+
return result;
8+
};
9+
10+
const processSingleText = (text, result) => {
11+
return new Promise((resolve) => {
12+
let res = [...text].reduce((acc, cur) => {
13+
acc[cur] = (acc[cur] || 0) + 1;
14+
return acc;
15+
}, result);
16+
resolve(res);
17+
});
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[c054d642-c1fa-4234-8007-9339f2337886]
13+
description = "no texts"
14+
15+
[818031be-49dc-4675-b2f9-c4047f638a2a]
16+
description = "one text with one letter"
17+
18+
[c0b81d1b-940d-4cea-9f49-8445c69c17ae]
19+
description = "one text with multiple letters"
20+
21+
[708ff1e0-f14a-43fd-adb5-e76750dcf108]
22+
description = "two texts with one letter"
23+
24+
[1b5c28bb-4619-4c9d-8db9-a4bb9c3bdca0]
25+
description = "two texts with multiple letters"
26+
27+
[6366e2b8-b84c-4334-a047-03a00a656d63]
28+
description = "ignore letter casing"
29+
30+
[92ebcbb0-9181-4421-a784-f6f5aa79f75b]
31+
description = "ignore whitespace"
32+
33+
[bc5f4203-00ce-4acc-a5fa-f7b865376fd9]
34+
description = "ignore punctuation"
35+
36+
[68032b8b-346b-4389-a380-e397618f6831]
37+
description = "ignore numbers"
38+
39+
[aa9f97ac-3961-4af1-88e7-6efed1bfddfd]
40+
description = "Unicode letters"
41+
42+
[7b1da046-701b-41fc-813e-dcfb5ee51813]
43+
description = "combination of lower- and uppercase letters, punctuation and white space"
44+
45+
[4727f020-df62-4dcf-99b2-a6e58319cb4f]
46+
description = "large texts"
47+
48+
[adf8e57b-8e54-4483-b6b8-8b32c115884c]
49+
description = "many small texts"

Diff for: exercises/practice/parallel-letter-frequency/.npmrc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
audit=false

Diff for: exercises/practice/parallel-letter-frequency/LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Exercism
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = {
2+
presets: ['@exercism/babel-preset-javascript'],
3+
plugins: [],
4+
};
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "@exercism/javascript-parallel-letter-frequency",
3+
"description": "Exercism practice exercise on parallel-letter-frequency",
4+
"author": "Katrina Owen",
5+
"contributors": [
6+
"Cool-Katt (https://github.com/Cool-Katt)",
7+
"Derk-Jan Karrenbeld <[email protected]> (https://derk-jan.com)",
8+
"Tejas Bubane (https://tejasbubane.github.io/)"
9+
],
10+
"private": true,
11+
"license": "MIT",
12+
"repository": {
13+
"type": "git",
14+
"url": "https://github.com/exercism/javascript",
15+
"directory": "exercises/practice/parallel-letter-frequency"
16+
},
17+
"devDependencies": {
18+
"@babel/core": "^7.23.0",
19+
"@exercism/babel-preset-javascript": "^0.2.1",
20+
"@exercism/eslint-config-javascript": "^0.6.0",
21+
"@types/jest": "^29.5.4",
22+
"@types/node": "^20.5.6",
23+
"babel-jest": "^29.6.4",
24+
"core-js": "~3.32.2",
25+
"eslint": "^8.49.0",
26+
"jest": "^29.7.0"
27+
},
28+
"dependencies": {},
29+
"scripts": {
30+
"test": "jest ./*",
31+
"watch": "jest --watch ./*",
32+
"lint": "eslint ."
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//
2+
// This is only a SKELETON file for the 'Parallel Letter Frequency' exercise. It's been provided as a
3+
// convenience to get you started writing code faster.
4+
//
5+
6+
export const parallelLetterFrequency = (texts) => {
7+
throw new Error('Remove this statement and implement this function');
8+
};

0 commit comments

Comments
 (0)