Skip to content
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

add missing exercies for reverse-string #364 #414

Merged
merged 2 commits into from
Jun 16, 2024
Merged
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
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@
"uuid": "59c6319b-2799-4e4c-bef1-e28626878446",
"practices": [],
"prerequisites": [],
"difficulty": 2
"difficulty": 4
},
{
"slug": "roman-numerals",
Expand Down
3 changes: 3 additions & 0 deletions exercises/practice/reverse-string/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"authors": [
"fapdash"
],
"contributors": [
"kmarker1101"
],
"files": {
"solution": [
"reverse-string.el"
Expand Down
28 changes: 26 additions & 2 deletions exercises/practice/reverse-string/.meta/example.el
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,33 @@
;;; Code:


(defun reverse-string (value)
(reverse value))
(require 'cl-lib)
(require 'ucs-normalize)

(defun combining-mark-p (char)
(let ((category (get-char-code-property char 'general-category)))
(or (string= category "Mn") ; Nonspacing_Mark
(string= category "Mc") ; Spacing_Mark
(string= category "Me")))) ; Enclosing_Mark

(defun split-grapheme-clusters (str)
(let ((clusters '())
(i 0)
(len (length str)))
(while (< i len)
(let ((start i)
(char (aref str i)))
(setq i (1+ i))
(while (and (< i len)
(combining-mark-p (aref str i)))
(setq i (1+ i)))
(push (substring str start i) clusters)))
(nreverse clusters)))

(defun reverse-string (value)
(let* ((decomposed (ucs-normalize-NFD-string value))
(clusters (split-grapheme-clusters decomposed)))
(apply #'concat (reverse clusters))))

(provide 'reverse-string)
;;; reverse-string.el ends here
9 changes: 9 additions & 0 deletions exercises/practice/reverse-string/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,12 @@ description = "a palindrome"

[b9e7dec1-c6df-40bd-9fa3-cd7ded010c4c]
description = "an even-sized word"

[1bed0f8a-13b0-4bd3-9d59-3d0593326fa2]
description = "wide characters"

[93d7e1b8-f60f-4f3c-9559-4056e10d2ead]
description = "grapheme cluster with pre-combined form"

[1028b2c1-6763-4459-8540-2da47ca512d9]
description = "grapheme clusters"
9 changes: 9 additions & 0 deletions exercises/practice/reverse-string/reverse-string-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@
(ert-deftest an-even-sized-word ()
(should (string= (reverse-string "drawer") "reward")))

(ert-deftest wide-characters ()
(should (string= (reverse-string "子猫") "猫子")))

(ert-deftest grapheme-cluster-with-pre-combined-form ()
(should (string= (reverse-string "Würstchenstand") "dnatsnehctsrüW")))

(ert-deftest grapheme-cluster ()
(should (string= (reverse-string "ผู้เขียนโปรแกรม") "มรกแรปโนยขีเผู้")))


(provide 'reverse-string-test)
;;; reverse-string-test.el ends here