Skip to content

Instantly share code, notes, and snippets.

@ericnormand
Last active September 18, 2020 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericnormand/f43768bac0024936e0274cfd2ce06aae to your computer and use it in GitHub Desktop.
Save ericnormand/f43768bac0024936e0274cfd2ce06aae to your computer and use it in GitHub Desktop.
395 PurelyFunctional.tv Newsletter

Seasons

Well, it's Hurricane Season here in the Gulf South. But that's not the kind of seasons I'm talking about now.

Your job is to take a month (keyword) day (number) and a hemisphere (:north or :south) and determine which season it is (return a keyword), according to this handy table.

Start       End         North  South
March 1     May 31      Spring Autumn
June 1      August 31   Summer Winter
September 1 November 30 Autumn Spring
December 1  February 29 Winter Summer

Example:

(which-season :north :march 5) ;=> :spring
(which-season :south :december 25) ;=> :summer

Thanks to this site for the challenge idea where it is considered Hard level in JavaScript.

Please submit your solutions as comments to this gist. Discussion is welcome.

@sztamas
Copy link

sztamas commented Sep 18, 2020

;; Vector of seasons in order containing the months in each season.
;; Starts with spring in northern hemisphere (autumn in southern hemisphere).
(def seasons [#{:march :april :may}
              #{:june :july :august}
              #{:september :october :november}
              #{:december :january :february}])

;; Can be left out if we don't validate days
(defn max-days-in-month [month]
  (condp contains? month
    #{:february}                         29
    #{:april :june :september :november} 30
    31))

(def season-names {:north [:spring :summer :autumn :winter]
                   :south [:autumn :winter :spring :summer]})

(defn season-index [month]
  (first (keep-indexed #(when (%2 month) %1) seasons)))

(defn which-season
  [hemisphere month day]
  {:pre [(contains? season-names hemisphere)
         (season-index month)
         (<= 1 day (max-days-in-month month))]}
  (let [seasons (hemisphere season-names)]
    (seasons (season-index month))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment