diff options
author | bd <bdunahu@operationnull.com> | 2024-05-20 01:07:13 -0600 |
---|---|---|
committer | bd <bdunahu@operationnull.com> | 2024-05-20 14:59:16 -0600 |
commit | 5df534c4e3893a9f7a4f528746eca98d7a012736 (patch) | |
tree | 06ddcea130301a55d9b7f21a5c72d27ec3b4d281 | |
parent | de8ec8026b3fbf60cfa95ef1c48ad53e1b922d38 (diff) |
Add dictionary code and tests
-rw-r--r-- | palindrome/palindrome-test.scm | 16 | ||||
-rw-r--r-- | prime/prime-test.scm | 34 | ||||
-rw-r--r-- | spellcheck/hasher/dictionary.scm | 37 | ||||
-rw-r--r-- | spellcheck/spellcheck-test.scm | 57 | ||||
-rw-r--r-- | spellcheck/spellcheck.scm | 0 |
5 files changed, 113 insertions, 31 deletions
diff --git a/palindrome/palindrome-test.scm b/palindrome/palindrome-test.scm index 4109154..d3fe716 100644 --- a/palindrome/palindrome-test.scm +++ b/palindrome/palindrome-test.scm @@ -4,20 +4,16 @@ (test-begin "harness") -(test-equal "test-not-palindrome" - #f - (palindrome? "palindrome")) +(test-assert "test-not-palindrome" + (not (palindrome? "palindrome"))) -(test-equal "test-is-palindrome" - #t +(test-assert "test-is-palindrome" (palindrome? "racecar")) -(test-equal "test-not-palindrome-bad-casing" - #f - (palindrome? "paLINDROMe")) +(test-assert "test-not-palindrome-bad-casing" + (not (palindrome? "paLINDROMe"))) -(test-equal "test-palindrome-bad-casing" - #t +(test-assert "test-palindrome-bad-casing" (palindrome? "raCECAr")) (test-equal "test-pp-not-palindrome" diff --git a/prime/prime-test.scm b/prime/prime-test.scm index a1c7562..7c9d557 100644 --- a/prime/prime-test.scm +++ b/prime/prime-test.scm @@ -4,39 +4,31 @@ (test-begin "harness") -(test-equal "test-not-prime-0" - #f - (prime? 12)) +(test-assert "test-not-prime-0" + (not (prime? 12))) -(test-equal "test-not-prime-1" - #f - (prime? 21)) +(test-assert "test-not-prime-1" + (not (prime? 21))) -(test-equal "test-prime" - #t +(test-assert "test-prime" (prime? 13)) -(test-equal "test-prime-large" - #t +(test-assert "test-prime-large" (prime? 1000033)) -(test-equal "test-two" - #t +(test-assert "test-two" (prime? 2)) ;; one is NOT considered prime! -(test-equal "test-one" - #f - (prime? 1)) +(test-assert "test-one" + (not (prime? 1))) ;; zero is NOT considered prime! -(test-equal "test-zero" - #f - (prime? 0)) +(test-assert "test-zero" + (not (prime? 0))) -(test-equal "test-negative" - #f - (prime? -13)) +(test-assert "test-negative" + (not (prime? -13))) (test-end "harness") diff --git a/spellcheck/hasher/dictionary.scm b/spellcheck/hasher/dictionary.scm new file mode 100644 index 0000000..10ec3b8 --- /dev/null +++ b/spellcheck/hasher/dictionary.scm @@ -0,0 +1,37 @@ +(define-module (hasher dictionary) + #:export (create-dictionary + dict-occur-ref + dict-keys-ref + dict-values-ref)) + +(define (create-dictionary lst) + (dictionary-helper (make-hash-table) lst)) + +(define (dictionary-helper dict rest) + "Recursively adds words to DICT." + (if (null? rest) + dict + (begin + (dict-inc-word dict (car rest)) + (dictionary-helper dict (cdr rest))))) + +(define (dict-inc-word dict e) + "Inserts word E into the DICT, using E +as a key and the running total of occurances +as the value." + (hashq-set! + dict e (1+ (dict-occur-ref dict e)))) + +(define (dict-occur-ref dict e) + "A wrapper for hashq-ref. Returns '0' if an element +is not present, rather than '#f'." + (let ((occur (hashq-ref dict e))) + (if occur occur 0))) + +(define (dict-keys-ref dict) + "Returns a list of all words in the dictionary." + (map car (hash-map->list cons dict))) + +(define (dict-values-ref dict) + "Sums up the total number of occurances for all words." + (apply + (map cdr (hash-map->list cons dict)))) diff --git a/spellcheck/spellcheck-test.scm b/spellcheck/spellcheck-test.scm new file mode 100644 index 0000000..8030c41 --- /dev/null +++ b/spellcheck/spellcheck-test.scm @@ -0,0 +1,57 @@ +(use-modules (srfi srfi-64) + (hasher dictionary)) + +(define simple-dict (create-dictionary '("word"))) +(define complex-dict (create-dictionary '("Again" "the" "thirst" + "quencher" "of" "wolves" + "embarked" "on" "a" "well" + "prepared" "expedition" + "eastward" "the" "generous" + "ruler" "moved" "the" "bitter" + "heart" "of" "the" "snake" + "across" "the" "fire"))) + +(test-begin "harness") + + +(test-assert "create-dictionary returns hashtable" + (hash-table? + (create-dictionary '()))) + +(define dict (create-dictionary '("word"))) + +(test-equal "list-keys-single" + '("word") + (dict-keys-ref simple-dict)) + +(test-equal "count-values-single" + 1 + (dict-values-ref simple-dict)) + + +(test-equal "retrieve single" + 1 + (dict-occur-ref complex-dict "wolves")) + +(test-equal "retrieve double" + 2 + (dict-occur-ref complex-dict "of")) + +(test-equal "retrieve quintuple" + 5 + (dict-occur-ref complex-dict "the")) + +(test-equal "retrieve zero" + 0 + (dict-occur-ref complex-dict "king")) + +(test-equal "list-keys-sentence" + '("Again" "a" "across" "bitter" "eastward" "embarked" "expedition" "fire" "generous" "heart" "moved" "of" "on" "prepared" "quencher" "ruler" "snake" "the" "thirst" "well" "wolves") + (sort (dict-keys-ref complex-dict) string<?)) + +(test-equal "count-values-sentence" + 26 + (dict-values-ref complex-dict)) + + +(test-end "harness") diff --git a/spellcheck/spellcheck.scm b/spellcheck/spellcheck.scm new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/spellcheck/spellcheck.scm |