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

scoring-project.lsp 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. ;;;;;;;;;;;;;;
  15. ;; GREED !! ;;
  16. ;;;;;;;;;;;;;;
  17. ;; Modified from Ruby Koans: about_scoring_project.rb
  18. ; *Greed* is a dice game where you roll up to five dice to accumulate
  19. ; points. The following "score" function will be used to calculate the
  20. ; score of a single roll of the dice.
  21. ;
  22. ; A greed roll is scored as follows:
  23. ;
  24. ; * A set of three ones is 1000 points
  25. ;
  26. ; * A set of three numbers (other than ones) is worth 100 times the
  27. ; number. (e.g. three fives is 500 points).
  28. ;
  29. ; * A one (that is not part of a set of three) is worth 100 points.
  30. ;
  31. ; * A five (that is not part of a set of three) is worth 50 points.
  32. ;
  33. ; * Everything else is worth 0 points.
  34. ;
  35. ;
  36. ; Examples:
  37. ;
  38. ; score([1,1,1,5,1]) => 1150 points
  39. ; score([2,3,4,6,2]) => 0 points
  40. ; score([3,4,5,3,3]) => 350 points
  41. ; score([1,5,1,2,4]) => 250 points
  42. ;
  43. ; More scoring examples are given in the tests below:
  44. ;
  45. ; Your goal is to write the score method.
  46. (defun score (dice)
  47. ; You need to write this method
  48. )
  49. (define-test test-score-of-an-empty-list-is-zero
  50. (assert-equal 0 (score nil)))
  51. (define-test test-score-of-a-single-roll-of-5-is-50
  52. (assert-equal 50 (score '(5))))
  53. (define-test test-score-of-a-single-roll-of-1-is-100
  54. (assert-equal 100 (score '(1))))
  55. (define-test test-score-of-multiple-1s-and-5s-is-the-sum-of-individual-scores
  56. (assert-equal 300 (score '(1 5 5 1))))
  57. (define-test test-score-of-single-2s-3s-4s-and-6s-are-zero
  58. (assert-equal 0 (score '(2 3 4 6))))
  59. (define-test test-score-of-a-triple-1-is-1000
  60. (assert-equal 1000 (score '(1 1 1))))
  61. (define-test test-score-of-other-triples-is-100x
  62. (assert-equal 200 (score '(2 2 2)))
  63. (assert-equal 300 (score '(3 3 3)))
  64. (assert-equal 400 (score '(4 4 4)))
  65. (assert-equal 500 (score '(5 5 5)))
  66. (assert-equal 600 (score '(6 6 6))))
  67. (define-test test-score-of-mixed-is-sum
  68. (assert-equal 250 (score '(2 5 2 2 3)))
  69. (assert-equal 550 (score '(5 5 5 5))))