Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -1337,6 +1337,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "parallel-letter-frequency",
"name": "Parallel Letter Frequency",
"uuid": "f893d23c-c432-48fd-ae87-0561451cb180",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "dominoes",
"name": "Dominoes",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Instruction Appendix

For building a concurrent/parallel program in Crystal, [might this resource prove useful][resource].

The online environment for this exercise is a bit different from most other exercises.
Instead of compiling Crystal into a single-threaded binary, this exercises uses Crystal exprimental multi-threading capabilities.
As such, the execution might behave differently than expected in some environments.

For local development, it is recommended that you have Crystal version 1.19.0 or later installed, and add `-Dpreview_mt` flag when compiling or running your code to enable multi-threading support.

[resource]: https://crystal-lang.org/reference/latest/guides/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.
Employ parallelism to calculate the total frequency of each letter in a list of texts.
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": [
"meatball133"
],
"files": {
"solution": [
"src/parallel_letter_frequency.cr"
],
"test": [
"spec/parallel_letter_frequency_spec.cr"
],
"example": [
".meta/src/example.cr"
]
},
"blurb": "Count the frequency of letters in texts using parallel computation."
}
29 changes: 29 additions & 0 deletions exercises/practice/parallel-letter-frequency/.meta/src/example.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class ParallelLetterFrequency
def self.calculate_frequencies(input : Array(String)) : Hash(String, Int32)
frequencies = Hash(String, Int32).new(0)
channel = Channel(Hash(String, Int32)).new

input.each do |text|
spawn do
local = Hash(String, Int32).new(0)
processed = 0

text.each_char do |char|
next unless char.letter?
local[char.downcase.to_s] += 1

# Crystal fibers are cooperatively scheduled; yielding occasionally
# prevents one long-running fiber from starving the others.
end
channel.send(local)
end
end
input.size.times do |x|
hash = channel.receive
frequencies.merge!(hash) do |key, oldval, newval|
oldval + newval
end
end
frequencies
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "spec"
require "../src/*"

describe "<%-= to_capitalized(@json["exercise"].to_s) %>" do
<%- @json["cases"].as_a.each do |cases| %>
<%= status()%> "<%-= cases["description"] %>" do
input = [<%= cases["input"]["texts"].as_a.map { |text| text.to_s.inspect }.join(", ") %>] <%= to_s_deep(cases["input"]["texts"]) == "[]" ? "of String" : ""%>
expected = <%= to_s_deep(cases["expected"]) == "{}" ? "{} of String => Int32" : to_s_deep(cases["expected"]) %>
<%= to_capitalized(@json["exercise"].to_s) %>.<%= cases["property"].to_s.underscore %>(input).should eq(expected)
end
<% end %>
end
49 changes: 49 additions & 0 deletions exercises/practice/parallel-letter-frequency/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# 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"

[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"

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ParallelLetterFrequency
def self.calculate_frequencies(input : Array(String)) : Hash(String, Int32)
# Write your code for the 'ParallelLetterFrequency' exercise in this file.
end
end
Loading