Skip to content

Commit

Permalink
Add parallel-letter-frequency exercise (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
glennj authored Feb 27, 2024
1 parent 82d1476 commit cedba7c
Show file tree
Hide file tree
Showing 10 changed files with 436 additions and 0 deletions.
22 changes: 22 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@
"test_runner": {
"average_run_time": 2
},
"files": {
"solution": [
"%{kebab_slug}.wren"
],
"test": [
"%{kebab_slug}.spec.wren"
],
"example": [
".meta/proof.ci.wren"
],
"exemplar": [
".meta/exemplar.wren"
]
},
"exercises": {
"practice": [
{
Expand Down Expand Up @@ -685,6 +699,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 7
},
{
"slug": "parallel-letter-frequency",
"name": "Parallel Letter Frequency",
"uuid": "9f322f6a-26e7-4595-9a13-36965657bac6",
"practices": [],
"prerequisites": [],
"difficulty": 7
}
]
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# ignore

You can read about concurrency in Wren [in the manual][concurrency].

[concurrency]: https://wren.io/concurrency.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Instructions

Count the frequency of letters in texts using parallel computation.

Parallelism is about doing things in parallel that can also be done sequentially.
A common example is counting the frequency of letters.
Create a function that returns the total frequency of each letter in a list of texts and that employs parallelism.
17 changes: 17 additions & 0 deletions exercises/practice/parallel-letter-frequency/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [
"glennj"
],
"files": {
"solution": [
"parallel-letter-frequency.wren"
],
"test": [
"parallel-letter-frequency.spec.wren"
],
"example": [
".meta/proof.ci.wren"
]
},
"blurb": "Count the frequency of letters in texts using parallel computation."
}
78 changes: 78 additions & 0 deletions exercises/practice/parallel-letter-frequency/.meta/proof.ci.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// ------------------------------------------------------------------
class Frequency {
static ofText(text) {
return text.codePoints.reduce(Frequency.new()) {|freq, cp|
var char
if (65 <= cp && cp <= 90) char = String.fromCodePoint(cp + 32)
if (97 <= cp && cp <= 122) char = String.fromCodePoint(cp)
if (char != null) freq.incr(char)
return freq
}
}

construct new() {
_map = {}
}

keys { _map.keys }
[key] { _map[key] }
[key]=(value) { _map[key] = value }

toMap { _map }
toString { _map.toString }

incr(key) { incr(key, 1) }
incr(key, amount) { _map[key] = (_map.containsKey(key) ? _map[key] : 0) + amount }

addAll(other) {
if (other is Frequency) {
other.keys.each {|key|
incr(key, other[key])
}
}
}
}

// ------------------------------------------------------------------
class ParallelLetterFrequency {
construct new(inputs) {
_texts = inputs
}

nonConcurrent() {
return _texts.reduce(Frequency.new()) {|freq, text|
freq.addAll(Frequency.ofText(text))
return freq
}.toMap
}

concurrent() {
// create a fiber for each input text
var fibers = _texts.map {|text|
return Fiber.new {|str|
Fiber.yield() // so the first `call` doesn't wait for the result
var freq = Frequency.ofText(str)
Fiber.yield(freq)
}
}.toList

// then send a piece of text to each fiber
(0..._texts.count).each {|i|
fibers[i].call(_texts[i])
}

// then collect the results
var result = Frequency.new()
fibers.each {|fiber|
var freq = fiber.call()
result.addAll(freq)
}

return result.toMap
}

calculateFrequencies() {
//return nonConcurrent()
return concurrent()
}
}
52 changes: 52 additions & 0 deletions exercises/practice/parallel-letter-frequency/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[c054d642-c1fa-4234-8007-9339f2337886]
description = "no texts"

[818031be-49dc-4675-b2f9-c4047f638a2a]
description = "one text with one letter"

[c0b81d1b-940d-4cea-9f49-8445c69c17ae]
description = "one text with multiple letters"

[708ff1e0-f14a-43fd-adb5-e76750dcf108]
description = "two texts with one letter"

[1b5c28bb-4619-4c9d-8db9-a4bb9c3bdca0]
description = "two texts with multiple letters"

[6366e2b8-b84c-4334-a047-03a00a656d63]
description = "ignore letter casing"

[92ebcbb0-9181-4421-a784-f6f5aa79f75b]
description = "ignore whitespace"

[bc5f4203-00ce-4acc-a5fa-f7b865376fd9]
description = "ignore punctuation"

[68032b8b-346b-4389-a380-e397618f6831]
description = "ignore numbers"

[aa9f97ac-3961-4af1-88e7-6efed1bfddfd]
description = "Unicode letters"
include = false
comment = "wren doesn't have a lot of string manipulation functionality"
comment = "it can't lowercase an arbitrary unicode letter"

[7b1da046-701b-41fc-813e-dcfb5ee51813]
description = "combination of lower- and uppercase letters, punctuation and white space"

[4727f020-df62-4dcf-99b2-a6e58319cb4f]
description = "large texts"

[adf8e57b-8e54-4483-b6b8-8b32c115884c]
description = "many small texts"
21 changes: 21 additions & 0 deletions exercises/practice/parallel-letter-frequency/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Exercism

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
14 changes: 14 additions & 0 deletions exercises/practice/parallel-letter-frequency/package.wren
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import "wren-package" for WrenPackage, Dependency
import "os" for Process

class Package is WrenPackage {
construct new() {}
name { "exercism/parallel-letter-frequency" }
dependencies {
return [
Dependency.new("wren-testie", "0.3.0", "https://github.com/joshgoebel/wren-testie.git")
]
}
}

Package.new().default()

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ParallelLetterFrequency {
construct new(inputs) {
// implement me
}

calculateFrequencies() {
// implement me
}
}

0 comments on commit cedba7c

Please sign in to comment.