Last active
May 17, 2023 00:43
-
-
Save chase-lambert/ff674cd653fe14301fd119b30baacd75 to your computer and use it in GitHub Desktop.
rendezvous with cassidoo: 22.09.18
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
(ns calculate-gpa | |
(:require [clojure.test :refer [deftest is]])) | |
(def grade->points | |
{:A 4 | |
:A- 3.7 | |
:B+ 3.3 | |
:B 3 | |
:B- 2.7 | |
:C+ 2.3 | |
:C 2 | |
:C- 1.7 | |
:D+ 1.3 | |
:D 1 | |
:D- 0.7 | |
:F 0}) | |
(defn calculate-gpa [grades] | |
(let [raw-gpa (/ (reduce + (map grade->points grades)) | |
(count grades))] | |
(if (double? raw-gpa) | |
(parse-double (format "%.1f" raw-gpa)) | |
raw-gpa))) | |
(deftest calculate-gpa-test | |
(is (= 4 (calculate-gpa [:A]))) | |
(is (= 0 (calculate-gpa [:F :F :F]))) | |
(is (= 3.3 (calculate-gpa [:A :A- :B+ :B :B-]))) | |
(is (= 3.3 (calculate-gpa [:A :B+ :C- :A])))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment