Fork of https://github.com/google/lisp-koans so that I could go through them. THIS CONTAINS ANSWERS.

hash-tables.lsp 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ;; Copyright 2013 Google Inc.
  2. ;;
  3. ;; Licensed under the Apache License, Version 2.0 (the "License");
  4. ;; you may not use this file except in compliance with the License.
  5. ;; You may obtain a copy of the License at
  6. ;;
  7. ;; http://www.apache.org/licenses/LICENSE-2.0
  8. ;;
  9. ;; Unless required by applicable law or agreed to in writing, software
  10. ;; distributed under the License is distributed on an "AS IS" BASIS,
  11. ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. ;; See the License for the specific language governing permissions and
  13. ;; limitations under the License.
  14. ; based on python koans: about_dictionaries.py
  15. (define-test test-create-hash-table
  16. "make hash table with make-hash-table"
  17. (let ((my-hash-table))
  18. (setf my-hash-table (make-hash-table))
  19. (true-or-false? ___ (typep my-hash-table 'hash-table))
  20. (true-or-false? ___ (hash-table-p my-hash-table))
  21. (true-or-false? ___ (hash-table-p (make-array '(3 3 3))))
  22. (assert-equal ___ (hash-table-count my-hash-table))))
  23. (define-test test-hash-table-access
  24. "gethash is for accessing hash tables"
  25. (let ((table-of-cube-roots (make-hash-table)))
  26. "assign the key-value pair 1->'uno'"
  27. (setf (gethash 1 table-of-cube-roots) 1)
  28. (assert-equal 1 (gethash 1 table-of-cube-roots))
  29. (assert-equal 1 (hash-table-count table-of-cube-roots))
  30. (setf (gethash 8 table-of-cube-roots) 2)
  31. (setf (gethash -27 table-of-cube-roots) -3)
  32. (assert-equal ___ (gethash -3 table-of-cube-roots))
  33. (assert-equal ___ (hash-table-count table-of-cube-roots))
  34. "accessing unset keys returns nil"
  35. (assert-equal ___ (gethash 125 table-of-cube-roots))))
  36. (define-test test-hash-key-equality
  37. "hash tables need to know how to tell if two keys are equivalent.
  38. The programmer must be careful to know which equality predicate is right."
  39. (let ((hash-table-eq nil)
  40. (hash-table-equal nil)
  41. (hash-table-default nil))
  42. "define three hash tables, with different equality tests"
  43. (setf hash-table-eq (make-hash-table :test #'eq))
  44. (setf hash-table-equal (make-hash-table :test #'equal))
  45. (setf hash-table-default (make-hash-table))
  46. "add the same string twice, to each"
  47. (setf (gethash "one" hash-table-eq) "uno")
  48. (setf (gethash "one" hash-table-eq) "uno")
  49. (setf (gethash "one" hash-table-equal) "uno")
  50. (setf (gethash "one" hash-table-equal) "uno")
  51. (setf (gethash "one" hash-table-default) "uno")
  52. (setf (gethash "one" hash-table-default) "uno")
  53. "count how many unique key-value pairs in each"
  54. (assert-equal ___ (hash-table-count hash-table-eq))
  55. (assert-equal ___ (hash-table-count hash-table-equal))
  56. (assert-equal ___ (hash-table-count hash-table-default))))
  57. (define-test test-hash-table-equality
  58. (let ((h1 (make-hash-table :test #'equal))
  59. (h2 (make-hash-table :test #'equal)))
  60. (setf (gethash "one" h1) "yat")
  61. (setf (gethash "one" h2) "yat")
  62. (setf (gethash "two" h1) "yi")
  63. (setf (gethash "two" h2) "yi")
  64. (true-or-false? ___ (eq h1 h2))
  65. (true-or-false? ___ (equal h1 h2))
  66. (true-or-false? ___ (equalp h1 h2))))
  67. (define-test test-changing-hash-tables
  68. (let ((babel-fish (make-hash-table :test #'equal))
  69. (expected (make-hash-table :test #'equal)))
  70. (setf (gethash "one" babel-fish) "uno")
  71. (setf (gethash "two" babel-fish) "dos")
  72. (setf (gethash "one" expected) "ein")
  73. (setf (gethash "two" expected) "zwei")
  74. (setf (gethash "one" babel-fish) "ein")
  75. (setf (gethash "two" babel-fish) ____)
  76. (assert-true (equalp babel-fish expected))))
  77. (define-test test-hash-key-membership
  78. "hash tables use multiple value return to tell you if the key exists"
  79. (let ((prev-pres (make-hash-table :test #'equal))
  80. (value-and-exists nil))
  81. (setf (gethash "Obama" prev-pres) "Bush")
  82. (setf (gethash "Lincoln" prev-pres) "Buchanan")
  83. (setf (gethash "Washington" prev-pres) nil)
  84. (setf value-and-exists (multiple-value-list (gethash "Obama" prev-pres)))
  85. (assert-equal value-and-exists '("Bush" t))
  86. (setf value-and-exists (multiple-value-list (gethash "Lincoln" prev-pres)))
  87. (assert-equal value-and-exists ____)
  88. (setf value-and-exists (multiple-value-list (gethash "Washington" prev-pres)))
  89. (assert-equal value-and-exists ____)
  90. (setf value-and-exists (multiple-value-list (gethash "Franklin" prev-pres)))
  91. (assert-equal value-and-exists ____)))
  92. (define-test test-make-your-own-hash-table
  93. "make a hash table that meets the following conditions"
  94. (let ((colors (make-hash-table))
  95. (values (make-hash-table)))
  96. (assert-equal (hash-table-count colors) 4)
  97. (setf values (list (gethash "blue" colors)
  98. (gethash "green" colors)
  99. (gethash "red" colors)))
  100. (assert-equal values '((0 0 1) (0 1 0) (1 0 0)))))