Skip to content

Commit f1f6afa

Browse files
authored
[New Exercise]: parallel letter frequency (#427)
* [New Exercise]: Parallel Letter Frequency Add exercise `parallel letter frequency` * Update config.json Change difficulty
1 parent 8e1dc75 commit f1f6afa

7 files changed

Lines changed: 260 additions & 1 deletion

File tree

config.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -615,14 +615,22 @@
615615
"prerequisites": [],
616616
"difficulty": 2
617617
},
618-
{
618+
{
619619
"slug": "line-up",
620620
"name": "Line Up",
621621
"uuid": "35d96adf-0fc2-4442-9299-76d880a70b3c",
622622
"practices": [],
623623
"prerequisites": [],
624624
"difficulty": 2
625625
},
626+
{
627+
"slug": "parallel-letter-frequency",
628+
"name": "Parallel Letter Frequency",
629+
"uuid": "6382ecca-b108-4fae-9901-dc1a9591a9cb",
630+
"practices": [],
631+
"prerequisites": [],
632+
"difficulty": 3
633+
},
626634
{
627635
"slug": "error-handling",
628636
"name": "Error Handling",
Lines changed: 7 additions & 0 deletions
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+
Employ parallelism to calculate the total frequency of each letter in a list of texts.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Function Get-LetterFrequencies() {
2+
<#
3+
.SYNOPSIS
4+
Count the frequency of letters in texts using parallel computation.
5+
6+
.PARAMETER Texts
7+
An array of strings.
8+
#>
9+
[CmdletBinding()]
10+
Param(
11+
[string[]]$Texts
12+
)
13+
$final = @{}
14+
$Texts | ForEach-Object -Parallel {
15+
$local = @{}
16+
foreach ($ch in $_.ToLower().ToCharArray()) {
17+
if ($ch -match '\p{L}') {
18+
$local[$ch] = 1 + ($local[$ch] ?? 0)
19+
}
20+
}
21+
$local} | ForEach-Object {
22+
foreach ($k in $_.Keys) {
23+
$final[[string]$k] = ($final[[string]$k] ?? 0) + $_[$k]
24+
}
25+
}
26+
$final
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [
3+
"glaxxie"
4+
],
5+
"files": {
6+
"solution": [
7+
"ParallelLetterFrequency.ps1"
8+
],
9+
"test": [
10+
"ParallelLetterFrequency.tests.ps1"
11+
],
12+
"example": [
13+
".meta/ParallelLetterFrequency.example.ps1"
14+
]
15+
},
16+
"blurb": "Count the frequency of letters in texts using parallel computation."
17+
}
Lines changed: 49 additions & 0 deletions
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"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Function Get-LetterFrequencies() {
2+
<#
3+
.SYNOPSIS
4+
Count the frequency of letters in texts using parallel computation.
5+
6+
.PARAMETER Texts
7+
An array of strings.
8+
#>
9+
[CmdletBinding()]
10+
Param(
11+
[string[]]$Texts
12+
)
13+
14+
Throw "Please implement this function"
15+
}

0 commit comments

Comments
 (0)