-
-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parallel letter frequency #422
Merged
BNAndras
merged 27 commits into
exercism:main
from
kmarker1101:parallel-letter-frequency
Jul 3, 2024
Merged
Changes from 16 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
2b607d4
add parallel-letter-frequency, part of 48 in 24
kmarker1101 5e42d75
practice-exercise-generator: Don't include commas in test names (#420)
fapdash a095956
fix merge conflict
kmarker1101 ca7f180
fix json formating issue
kmarker1101 71baba9
fix merge issue
kmarker1101 55ef6a2
fix merge issue
kmarker1101 645a481
refactor to use parallism
kmarker1101 a79d868
check if ci failure was a fluke
kmarker1101 bb11561
fix ci failure
kmarker1101 e81e9d6
add empty string check
kmarker1101 b7138b8
fix merge conflict
kmarker1101 40ea8eb
return hash-table, update difficulty
kmarker1101 d03620f
cleanup, fix error
kmarker1101 f2018cf
add instructions.append
kmarker1101 1359c1b
Merge branch 'main' into parallel-letter-frequency
kmarker1101 dffc508
reformat instructions.append.md
kmarker1101 d374db3
Use extra file for subprocess code
fapdash 183b6ba
bin/test-examples: Handle testing of exercises with additional files
fapdash af9b857
Don't use additional solution file
fapdash 292d4db
add BNAndras and fapdash as contributors
kmarker1101 fae9f65
Use printed representation for hash table (de)serialization
fapdash 24268e0
Revert changes to `bin/test-examples`
fapdash 776d523
Merge branch 'main' into parallel-letter-frequency
kmarker1101 3366db3
Merge branch 'exercism:main' into parallel-letter-frequency
kmarker1101 0a72e9c
Merge branch 'parallel-letter-frequency' of github.com:kmarker1101/em…
kmarker1101 154b002
refactor tests
kmarker1101 2d1942f
Typo fix
BNAndras File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
exercises/practice/parallel-letter-frequency/.docs/instructions.append.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Instructions append | ||
|
||
## Using Parallelism | ||
|
||
The goal of this exercise is to practice parallelism with Emacs Lisp. | ||
|
||
In Emacs Lisp this is achived by using [`asynchronous processes`](https://www.gnu.org/software/emacs/manual/html_node/elisp/Asynchronous-Processes.html#:~:text=An%20asynchronous%20process%20is%20controlled,%2Dtype%20(see%20below)). | ||
|
||
You may also want to look at the documentation for [`batch mode`](https://www.gnu.org/software/emacs/manual/html_node/elisp/Batch-Mode.html), [`sentinels`](https://www.gnu.org/software/emacs/manual/html_node/elisp/Sentinels.html) and [`receiving output from processes`](https://www.gnu.org/software/emacs/manual/html_node/elisp/Output-from-Processes.html). | ||
BNAndras marked this conversation as resolved.
Show resolved
Hide resolved
|
7 changes: 7 additions & 0 deletions
7
exercises/practice/parallel-letter-frequency/.docs/instructions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
exercises/practice/parallel-letter-frequency/.meta/config.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{ | ||
"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." | ||
} |
74 changes: 74 additions & 0 deletions
74
exercises/practice/parallel-letter-frequency/.meta/example.el
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
;;; parallel-letter-frequency.el --- Parallel Letter Frequency (exercism) -*- lexical-binding: t; -*- | ||
|
||
;;; Commentary: | ||
|
||
;;; Code: | ||
|
||
(require 'cl-lib) | ||
|
||
|
||
(defun clean-text (text) | ||
(downcase (replace-regexp-in-string "[^[:alpha:]]" "" text))) | ||
|
||
|
||
(defun combine-frequencies (freqs-list) | ||
(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 deserialize-hash-table (str) | ||
(let ((hash-table (make-hash-table :test 'equal))) | ||
(unless (string= str "") | ||
(dolist (item (split-string str ",")) | ||
(let ((pair (split-string item ":"))) | ||
(puthash (string-to-char (nth 0 pair)) | ||
(string-to-number (nth 1 pair)) | ||
hash-table)))) | ||
hash-table)) | ||
|
||
|
||
(defun calculate-frequencies (texts) | ||
(let ((cleaned-texts (mapcar #'clean-text texts))) | ||
(if (cl-every #'string-empty-p cleaned-texts) | ||
(make-hash-table :test 'equal) | ||
(let* ((num-processes (min (length cleaned-texts) (max 1 (string-to-number (shell-command-to-string "nproc"))))) | ||
(texts-per-process (ceiling (/ (float (length cleaned-texts)) num-processes))) | ||
(results (make-hash-table :test 'equal)) | ||
(pending num-processes) | ||
(final-result (make-hash-table :test 'equal)) | ||
(processes nil)) | ||
(dotimes (i num-processes) | ||
(let* ((start-index (* i texts-per-process)) | ||
(end-index (min (* (1+ i) texts-per-process) (length cleaned-texts))) | ||
(process-texts (if (< start-index (length cleaned-texts)) | ||
(cl-subseq cleaned-texts start-index end-index) | ||
'()))) | ||
(when (not (null process-texts)) | ||
(let* ((command (format "(princ (let ((freqs (make-hash-table :test 'equal))) (dolist (text '%S) (let ((text-freqs (make-hash-table :test 'equal))) (dolist (char (string-to-list text)) (when (string-match-p \"[[:alpha:]]\" (char-to-string char)) (puthash char (1+ (gethash char text-freqs 0)) text-freqs))) (maphash (lambda (key value) (puthash key (+ value (gethash key freqs 0)) freqs)) text-freqs))) (let (result) (maphash (lambda (key value) (push (format \"%%c:%%d\" key value) result)) freqs) (string-join (reverse result) \",\")))))" | ||
process-texts)) | ||
(process (make-process | ||
:name (format "letter-freq-process-%d" i) | ||
:buffer (generate-new-buffer (format " *letter-freq-process-%d*" i)) | ||
:command (list "emacs" "--batch" "--eval" command) | ||
:sentinel (lambda (proc _event) | ||
(when (eq (process-status proc) 'exit) | ||
(with-current-buffer (process-buffer proc) | ||
(let ((result (deserialize-hash-table (buffer-string)))) | ||
(maphash (lambda (key value) | ||
(puthash key (+ value (gethash key results 0)) results)) | ||
result)) | ||
(setq pending (1- pending)) | ||
(when (= pending 0) | ||
(setq final-result (combine-frequencies (list results)))))))))) | ||
(push process processes))))) | ||
(while (> pending 0) | ||
(sleep-for 0.1)) | ||
final-result)))) | ||
|
||
|
||
(provide 'parallel-letter-frequency) | ||
;;; parallel-letter-frequency.el ends here |
49 changes: 49 additions & 0 deletions
49
exercises/practice/parallel-letter-frequency/.meta/tests.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
151 changes: 151 additions & 0 deletions
151
exercises/practice/parallel-letter-frequency/parallel-letter-frequency-test.el
BNAndras marked this conversation as resolved.
Show resolved
Hide resolved
|
Large diffs are not rendered by default.
Oops, something went wrong.
14 changes: 14 additions & 0 deletions
14
exercises/practice/parallel-letter-frequency/parallel-letter-frequency.el
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
;;; parallel-letter-frequency.el --- Parallel Letter Frequency (exercism) -*- lexical-binding: t; -*- | ||
|
||
;;; Commentary: | ||
|
||
;;; Code: | ||
|
||
|
||
(defun calculate-frequencies (texts) | ||
(error "Delete this S-Expression and write your own implementation")) | ||
|
||
|
||
(provide 'parallel-letter-frequency) | ||
;;; parallel-letter-frequency.el ends here | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small typo fix and grammar fix so we're not saying this is the only way to do parallelism.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done