Skip to content

Commit 8eef740

Browse files
committed
Add parallel-letter-frequency
1 parent 1e6c250 commit 8eef740

File tree

12 files changed

+18646
-45
lines changed

12 files changed

+18646
-45
lines changed

config.json

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,12 @@
6060
"prerequisites": []
6161
},
6262
{
63-
"uuid": "88baa5ab-dacb-4fab-8f16-4f3ed726b27b",
6463
"slug": "freelancer-rates",
6564
"name": "Freelancer Rates",
66-
"concepts": ["numbers"],
65+
"uuid": "88baa5ab-dacb-4fab-8f16-4f3ed726b27b",
66+
"concepts": [
67+
"numbers"
68+
],
6769
"prerequisites": [
6870
"basics",
6971
"includes"
@@ -950,7 +952,7 @@
950952
"name": "Darts",
951953
"uuid": "e8ad3859-9999-4a36-900d-d82fa6e74b08",
952954
"practices": [
953-
"if-statements"
955+
"if-statements"
954956
],
955957
"prerequisites": [
956958
"numbers"
@@ -1018,9 +1020,7 @@
10181020
"prerequisites": [
10191021
"classes"
10201022
],
1021-
"difficulty": 4,
1022-
"topics": [
1023-
]
1023+
"difficulty": 4
10241024
},
10251025
{
10261026
"slug": "eliuds-eggs",
@@ -1033,30 +1033,23 @@
10331033
"if-statements",
10341034
"comparisons"
10351035
],
1036-
"difficulty": 3,
1037-
"topics": [
1038-
]
1036+
"difficulty": 3
10391037
},
10401038
{
10411039
"slug": "diamond",
10421040
"name": "Diamond",
10431041
"uuid": "f8298f68-ef4e-41e1-99af-85a1bef71561",
1044-
"practices": [
1045-
],
1046-
"prerequisites": [
1047-
],
1048-
"difficulty": 3,
1049-
"topics": [
1050-
]
1042+
"practices": [],
1043+
"prerequisites": [],
1044+
"difficulty": 3
10511045
},
10521046
{
10531047
"slug": "run-length-encoding",
10541048
"name": "Run-Length Encoding",
10551049
"uuid": "916fedd1-a9ed-4200-b115-cc1d1bc44aba",
10561050
"practices": [],
10571051
"prerequisites": [],
1058-
"difficulty": 3,
1059-
"topics": []
1052+
"difficulty": 3
10601053
},
10611054
{
10621055
"slug": "rotational-cipher",
@@ -1068,44 +1061,39 @@
10681061
"prerequisites": [
10691062
"loops"
10701063
],
1071-
"difficulty": 3,
1072-
"topics": []
1064+
"difficulty": 3
10731065
},
10741066
{
10751067
"slug": "resistor-color",
10761068
"name": "Resistor Color",
10771069
"uuid": "15a9277e-59e2-484a-9877-64437556825c",
10781070
"practices": [],
10791071
"prerequisites": [],
1080-
"difficulty": 2,
1081-
"topics": []
1072+
"difficulty": 2
10821073
},
10831074
{
10841075
"slug": "resistor-color-duo",
10851076
"name": "Resistor Color Duo",
10861077
"uuid": "83ce95e9-f8af-4dbb-b96b-d94b12421d77",
10871078
"practices": [],
10881079
"prerequisites": [],
1089-
"difficulty": 3,
1090-
"topics": []
1080+
"difficulty": 3
10911081
},
10921082
{
10931083
"slug": "minesweeper",
10941084
"name": "Minesweeper",
10951085
"uuid": "3753a72a-78b7-429f-b79a-f68d55a00387",
10961086
"practices": [],
10971087
"prerequisites": [],
1098-
"difficulty": 5,
1099-
"topics": []
1088+
"difficulty": 5
11001089
},
11011090
{
11021091
"slug": "sublist",
11031092
"name": "Sublist",
11041093
"uuid": "801cb315-91d6-48e6-92f4-36423a89d4e1",
11051094
"practices": [],
11061095
"prerequisites": [],
1107-
"difficulty": 5,
1108-
"topics": []
1096+
"difficulty": 5
11091097
},
11101098
{
11111099
"slug": "knapsack",
@@ -1122,8 +1110,15 @@
11221110
"uuid": "83c92b7f-fb0e-43d8-ab7d-d514408b8575",
11231111
"practices": [],
11241112
"prerequisites": [],
1125-
"difficulty": 7,
1126-
"topics": []
1113+
"difficulty": 7
1114+
},
1115+
{
1116+
"slug": "parallel-letter-frequency",
1117+
"name": "Parallel Letter Frequency",
1118+
"uuid": "0a029212-bc55-4559-b3c6-2c26bec0d560",
1119+
"practices": [],
1120+
"prerequisites": [],
1121+
"difficulty": 6
11271122
}
11281123
],
11291124
"foregone": [
@@ -1214,47 +1209,47 @@
12141209
],
12151210
"key_features": [
12161211
{
1217-
"icon": "fast",
12181212
"title": "Fast execution speed",
1219-
"content": "With execution speed in mind, C++ is used in a wide range of computation-intensive domains."
1213+
"content": "With execution speed in mind, C++ is used in a wide range of computation-intensive domains.",
1214+
"icon": "fast"
12201215
},
12211216
{
1222-
"icon": "multi-paradigm",
12231217
"title": "Multi-paradigm",
1224-
"content": "C++ supports a variety of programming paradigms like OOP, functional and procedural programming."
1218+
"content": "C++ supports a variety of programming paradigms like OOP, functional and procedural programming.",
1219+
"icon": "multi-paradigm"
12251220
},
12261221
{
1227-
"icon": "powerful",
12281222
"title": "High-level programming",
1229-
"content": "C++ standardizes a comprehensive library, enabling the developer to concentrate on the problem."
1223+
"content": "C++ standardizes a comprehensive library, enabling the developer to concentrate on the problem.",
1224+
"icon": "powerful"
12301225
},
12311226
{
1232-
"icon": "homoiconic",
12331227
"title": "Low-level programming",
1234-
"content": "C++ masters the strength of its predecessor C and gives the developer access to the bits and bytes."
1228+
"content": "C++ masters the strength of its predecessor C and gives the developer access to the bits and bytes.",
1229+
"icon": "homoiconic"
12351230
},
12361231
{
1237-
"icon": "widely-used",
12381232
"title": "Popular",
1239-
"content": "Thanks to the flexibility and portability, you can find applications written in C++ in every scale."
1233+
"content": "Thanks to the flexibility and portability, you can find applications written in C++ in every scale.",
1234+
"icon": "widely-used"
12401235
},
12411236
{
1242-
"icon": "evolving",
12431237
"title": "Ever-evolving",
1244-
"content": "Although mature, C++ is still evolving and developed by a committee to fit the developers needs."
1238+
"content": "Although mature, C++ is still evolving and developed by a committee to fit the developers needs.",
1239+
"icon": "evolving"
12451240
}
12461241
],
12471242
"tags": [
1248-
"paradigm/object_oriented",
1249-
"typing/static",
1250-
"typing/strong",
12511243
"execution_mode/compiled",
1244+
"paradigm/object_oriented",
12521245
"platform/android",
12531246
"platform/ios",
12541247
"platform/linux",
12551248
"platform/mac",
12561249
"platform/windows",
12571250
"runtime/standalone_executable",
1251+
"typing/static",
1252+
"typing/strong",
12581253
"used_for/backends",
12591254
"used_for/cross_platform_development",
12601255
"used_for/embedded_systems",
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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.
8+
9+
TODO: add something about C++ specifically.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"ahans"
4+
],
5+
"files": {
6+
"solution": [
7+
"parallel_letter_frequency.cpp",
8+
"parallel_letter_frequency.h"
9+
],
10+
"test": [
11+
"parallel_letter_frequency_test.cpp"
12+
],
13+
"example": [
14+
"example.cpp",
15+
"example.h"
16+
]
17+
},
18+
"blurb": "Count the frequency of letters in texts using parallel computation."
19+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include "parallel_letter_frequency.h"
2+
3+
#include <algorithm>
4+
#include <cctype>
5+
#include <execution>
6+
7+
namespace parallel_letter_frequency {
8+
9+
namespace {
10+
11+
[[nodiscard]] std::unordered_map<char, size_t> frequency(
12+
std::string_view const text) {
13+
std::unordered_map<char, size_t> freq;
14+
for (auto c : text) freq[std::tolower(c)] += 1;
15+
return freq;
16+
}
17+
18+
} // namespace
19+
20+
std::unordered_map<char, size_t> frequency(
21+
std::vector<std::string_view> const& texts) {
22+
std::vector<std::unordered_map<char, size_t>> freqs(texts.size());
23+
// determine frequencies of texts in parallel
24+
std::transform(std::execution::par, texts.cbegin(), texts.cend(),
25+
freqs.begin(),
26+
[](auto const& text) { return frequency(text); });
27+
28+
// combine individual frequencies (this happens serially)
29+
std::unordered_map<char, size_t> totals{};
30+
for (auto const& single_text_freqs : freqs) {
31+
for (auto const [c, count] : single_text_freqs) {
32+
totals[c] += count;
33+
}
34+
}
35+
36+
return totals;
37+
}
38+
39+
} // namespace parallel_letter_frequency
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#if !defined(PARALLEL_LETTER_FREQUENCY_H)
2+
#define PARALLEL_LETTER_FREQUENCY_H
3+
4+
#include <string_view>
5+
#include <unordered_map>
6+
#include <vector>
7+
8+
namespace parallel_letter_frequency {
9+
10+
[[nodiscard]] std::unordered_map<char, size_t> frequency(
11+
std::vector<std::string_view> const& texts);
12+
13+
}
14+
15+
#endif
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
include = false
42+
43+
[7b1da046-701b-41fc-813e-dcfb5ee51813]
44+
description = "combination of lower- and uppercase letters, punctuation and white space"
45+
46+
[4727f020-df62-4dcf-99b2-a6e58319cb4f]
47+
description = "large texts"
48+
49+
[adf8e57b-8e54-4483-b6b8-8b32c115884c]
50+
description = "many small texts"

0 commit comments

Comments
 (0)