Skip to content

Commit

Permalink
add parallel-letter-frequency, part of 48 in 24
Browse files Browse the repository at this point in the history
  • Loading branch information
kmarker1101 committed Jun 24, 2024
1 parent 19823ab commit 2b607d4
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 6
},
{
"slug": "parallel-letter-frequency",
"name": "Parallel Letter Frequency",
"uuid": "3daf3903-1eb0-49b9-827a-76e6b7ca25fb",
"practices": [],
"prerequisites": [],
"difficulty": 8
}
]
},
Expand Down
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.
15 changes: 15 additions & 0 deletions exercises/practice/parallel-letter-frequency/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"authors": ["kmarker1101"],
"files": {
"solution": [
"parallel-letter-frequency.el"
],
"test": [
"parallel-letter-frequency-test.el"
],
"example": [
".meta/example.el"
]
},
"blurb": "Count the frequency of letters in texts using parallel computation."
}
48 changes: 48 additions & 0 deletions exercises/practice/parallel-letter-frequency/.meta/example.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
;;; parallel-letter-frequency.el --- Parallel Letter Frequency (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:

(defun count-frequencies (text)
"Count the frequency of each letter in TEXT."
(let ((freqs (make-hash-table :test 'equal)))
(dolist (char (string-to-list (downcase text)))
(when (string-match-p "[[:alpha:]]" (char-to-string char)) ; Check if char is a Unicode letter
(puthash char (1+ (gethash char freqs 0)) freqs)))
freqs))

(defun combine-frequencies (freqs-list)
"Combine a list of frequency hash tables into a single hash table."
(let ((combined-freqs (make-hash-table :test 'equal)))
(dolist (freqs freqs-list)
(maphash (lambda (key value)
(puthash key (+ value (gethash key combined-freqs 0)) combined-freqs))
freqs))
combined-freqs))

(defun calculate-frequencies (texts)
"Calculate letter frequencies for each string in TEXTS using threads."
(let ((threads (mapcar (lambda (text)
(make-thread
(lambda ()
(count-frequencies text))))
texts))
result
freqs-list)
;; Wait for all threads to complete and collect their results
(dolist (thread threads)
(thread-join thread)
(push (thread-join thread) freqs-list))
;; Combine the frequencies
(let ((combined-freqs (combine-frequencies freqs-list)))
(maphash (lambda (key value)
(push (list (char-to-string key) value) result))
combined-freqs)
(setq result (sort result (lambda (a b) (string< (car a) (car b)))))
(setq result (apply 'append result))
result)))


(provide 'parallel-letter-frequency)
;;; parallel-letter-frequency.el ends here
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"
Loading

0 comments on commit 2b607d4

Please sign in to comment.