Avatar of joaofnds

joaofnds's solution

to Meetup in the Common Lisp Track

Published at Nov 03 2019 · 0 comments
Instructions
Test suite
Solution

Calculate the date of meetups.

Typically meetups happen on the same day of the week. In this exercise, you will take a description of a meetup date, and return the actual meetup date.

Examples of general descriptions are:

  • The first Monday of January 2017
  • The third Tuesday of January 2017
  • The wednesteenth of January 2017
  • The last Thursday of January 2017

The descriptors you are expected to parse are: first, second, third, fourth, fifth, last, monteenth, tuesteenth, wednesteenth, thursteenth, friteenth, saturteenth, sunteenth

Note that "monteenth", "tuesteenth", etc are all made up words. There was a meetup whose members realized that there are exactly 7 numbered days in a month that end in '-teenth'. Therefore, one is guaranteed that each day of the week (Monday, Tuesday, ...) will have exactly one date that is named with '-teenth' in every month.

Given examples of a meetup dates, each containing a month, day, year, and descriptor calculate the date of the actual meetup. For example, if given "The first Monday of January 2017", the correct meetup date is 2017/1/2.

Setup

Check out Installing Common Lisp for instructions to get started or take a look at the guides available in the track's side bar.

Formatting

While Common Lisp doesn't care about indentation and layout of code, nor whether you use spaces or tabs, this is an important consideration for submissions to exercism.io. Excercism.io's code widget cannot handle mixing of tab and space characters well so using only spaces is recommended to make the code more readable to the human reviewers. Please review your editors settings on how to accomplish this. Below are instructions for popular editors for Common Lisp.

VIM

Use the following commands to ensure VIM uses only spaces for indentation:

:set tabstop=2
:set shiftwidth=2
:set expandtab

(or as a oneliner :set tabstop=2 shiftwidth=2 expandtab). This can be added to your ~/.vimrc file to use it all the time.

Emacs

Emacs is very well suited for editing Common Lisp and has many powerful add-on packages available. The only thing that one needs to do with a stock emacs to make it work well with exercism.io is to evaluate the following code:

(setq-default indent-tabs-mode nil)

This can be placed in your ~/.emacs (or ~/.emacs.d/init.el) in order to have it set whenever Emacs is launched.

One suggested add-on for Emacs and Common Lisp is SLIME which offers tight integration with the REPL; making iterative coding and testing very easy.

Source

Jeremy Hinegardner mentioned a Boulder meetup that happens on the Wednesteenth of every month https://twitter.com/copiousfreetime

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

meetup-test.lisp

(ql:quickload "lisp-unit")
#-xlisp-test (load "meetup")

(defpackage #:meetup-test
  (:use #:common-lisp #:lisp-unit))

(in-package #:meetup-test)

(define-test monteeth-of-may-2013
  (assert-equal '(2013 5 13) (meetup:meetup 5 2013 :monday :teenth)))
(define-test monteenth-of-august-2013
  (assert-equal '(2013 8 19) (meetup:meetup 8 2013 :monday :teenth)))
(define-test monteenth-of-september-2013
  (assert-equal '(2013 9 16) (meetup:meetup 9 2013 :monday :Teenth)))

(define-test tuesteenth-of-march-2013
  (assert-equal '(2013 3 19) (meetup:meetup 3 2013 :tuesday :teenth)))
(define-test tuesteenth-of-april-2013
  (assert-equal '(2013 4 16) (meetup:meetup 4 2013 :tuesday :teenth)))
(define-test tuesteenth-of-august-2013
  (assert-equal '(2013 8 13) (meetup:meetup 8 2013 :tuesday :teenth)))

(define-test wednesteenth-of-january-2013
  (assert-equal '(2013 1 16) (meetup:meetup 1 2013 :wednesday :teenth)))
(define-test wednesteenth-of-february-2013
  (assert-equal '(2013 2 13) (meetup:meetup 2 2013 :wednesday :teenth)))
(define-test wednesteenth-of-june-2013
  (assert-equal '(2013 6 19) (meetup:meetup 6 2013 :wednesday :teenth)))

(define-test thursteenth-of-may-2013
  (assert-equal '(2013 5 16) (meetup:meetup 5 2013 :thursday :teenth)))
(define-test thursteenth-of-june-2013
  (assert-equal '(2013 6 13) (meetup:meetup 6 2013 :thursday :teenth)))
(define-test thursteenth-of-september-2013
  (assert-equal '(2013 9 19) (meetup:meetup 9 2013 :thursday :teenth)))

(define-test friteenth-of-april-2013
  (assert-equal '(2013 4 19) (meetup:meetup 4 2013 :friday :teenth)))
(define-test friteenth-of-august-2013
  (assert-equal '(2013 8 16) (meetup:meetup 8 2013 :friday :teenth)))
(define-test friteenth-of-september-2013
  (assert-equal '(2013 9 13) (meetup:meetup 9 2013 :friday :teenth)))

(define-test saturteenth-of-february-2013
  (assert-equal '(2013 2 16) (meetup:meetup 2 2013 :saturday :teenth)))
(define-test saturteenth-of-april-2013
  (assert-equal '(2013 4 13) (meetup:meetup 4 2013 :saturday :teenth)))
(define-test saturteenth-of-october-2013
  (assert-equal '(2013 10 19) (meetup:meetup 10 2013 :saturday :teenth)))

(define-test sunteenth-of-may-2013
  (assert-equal '(2013 5 19) (meetup:meetup 5 2013 :sunday :teenth)))
(define-test sunteenth-of-june-2013
  (assert-equal '(2013 6 16) (meetup:meetup 6 2013 :sunday :teenth)))
(define-test sunteenth-of-october-2013
  (assert-equal '(2013 10 13) (meetup:meetup 10 2013 :sunday :teenth)))

(define-test first-monday-of-march-2013
  (assert-equal '(2013 3 4) (meetup:meetup 3 2013 :monday :first)))
(define-test first-monday-of-april-2013
  (assert-equal '(2013 4 1) (meetup:meetup 4 2013 :monday :first)))

(define-test first-tuesday-of-may-2013
  (assert-equal '(2013 5 7) (meetup:meetup 5 2013 :tuesday :first)))
(define-test first-tuesday-of-june-2013
  (assert-equal '(2013 6 4) (meetup:meetup 6 2013 :tuesday :first)))

(define-test first-wednesday-of-july-2013
  (assert-equal '(2013 7 3) (meetup:meetup 7 2013 :wednesday :first)))
(define-test first-wednesday-of-august-2013
  (assert-equal '(2013 8 7) (meetup:meetup 8 2013 :wednesday :first)))

(define-test first-thursday-of-september-2013
  (assert-equal '(2013 9 5) (meetup:meetup 9 2013 :thursday :first)))
(define-test first-thursday-of-october-2013
  (assert-equal '(2013 10 3) (meetup:meetup 10 2013 :thursday :first)))

(define-test first-friday-of-november-2013
  (assert-equal '(2013 11 1) (meetup:meetup 11 2013 :friday :first)))
(define-test first-friday-of-december-2013
  (assert-equal '(2013 12 6) (meetup:meetup 12 2013 :friday :first)))

(define-test first-saturday-of-january-2013
  (assert-equal '(2013 1 5) (meetup:meetup 1 2013 :saturday :first)))
(define-test first-saturday-of-january-2013
  (assert-equal '(2013 2 2) (meetup:meetup 2 2013 :saturday :first)))

(define-test first-sunday-of-march-2013
  (assert-equal '(2013 3 3) (meetup:meetup 3 2013 :sunday :first)))
(define-test first-sunday-of-april-2013
  (assert-equal '(2013 4 7) (meetup:meetup 4 2013 :sunday :first)))

(define-test second-monday-of-march-2013
  (assert-equal '(2013 3 11) (meetup:meetup 3 2013 :monday :second)))
(define-test second-monday-of-april-2013
  (assert-equal '(2013 4 8) (meetup:meetup 4 2013 :monday :second)))

(define-test second-tuesday-of-may-2013
  (assert-equal '(2013 5 14) (meetup:meetup 5 2013 :tuesday :second)))
(define-test second-tuesday-of-june-2013
  (assert-equal '(2013 6 11) (meetup:meetup 6 2013 :tuesday :second)))

(define-test second-wednesday-of-july-2013
  (assert-equal '(2013 7 10) (meetup:meetup 7 2013 :wednesday :second)))
(define-test second-wednesday-of-august-2013
  (assert-equal '(2013 8 14) (meetup:meetup 8 2013 :wednesday :second)))

(define-test second-thursday-of-september-2013
  (assert-equal '(2013 9 12) (meetup:meetup 9 2013 :thursday :second)))
(define-test second-thursday-of-october-2013
  (assert-equal '(2013 10 10) (meetup:meetup 10 2013 :thursday :second)))

(define-test second-friday-of-november-2013
  (assert-equal '(2013 11 8) (meetup:meetup 11 2013 :friday :second)))
(define-test second-friday-of-december-2013
  (assert-equal '(2013 12 13) (meetup:meetup 12 2013 :friday :second)))

(define-test second-saturday-of-january-2013
  (assert-equal '(2013 1 12) (meetup:meetup 1 2013 :saturday :second)))
(define-test second-saturday-of-february-2013
  (assert-equal '(2013 2 9) (meetup:meetup 2 2013 :saturday :second)))

(define-test second-sunday-of-march-2013
  (assert-equal '(2013 3 10) (meetup:meetup 3 2013 :sunday :second)))
(define-test second-sunday-of-april-2013
  (assert-equal '(2013 4 14) (meetup:meetup 4 2013 :sunday :second)))

(define-test third-monday-of-march-2013
  (assert-equal '(2013 3 18) (meetup:meetup 3 2013 :monday :third)))
(define-test third-monday-of-april-2013
  (assert-equal '(2013 4 15) (meetup:meetup 4 2013 :monday :third)))

(define-test third-tuesday-of-may-2013
  (assert-equal '(2013 5 21) (meetup:meetup 5 2013 :tuesday :third)))
(define-test third-tuesday-of-june-2013
  (assert-equal '(2013 6 18) (meetup:meetup 6 2013 :tuesday :third)))

(define-test third-wednesday-of-july-2013
  (assert-equal '(2013 7 17) (meetup:meetup 7 2013 :wednesday :third)))
(define-test third-wednesday-of-august-2013
  (assert-equal '(2013 8 21) (meetup:meetup 8 2013 :wednesday :third)))

(define-test third-thursday-of-september-2013
  (assert-equal '(2013 9 19) (meetup:meetup 9 2013 :thursday :third)))
(define-test third-thursday-of-october-2013
  (assert-equal '(2013 10 17) (meetup:meetup 10 2013 :thursday :third)))

(define-test third-friday-of-november-2013
  (assert-equal '(2013 11 15) (meetup:meetup 11 2013 :friday :third)))
(define-test third-friday-of-december-2013
  (assert-equal '(2013 12 20) (meetup:meetup 12 2013 :friday :third)))

(define-test third-saturday-of-january-2013
  (assert-equal '(2013 1 19) (meetup:meetup 1 2013 :saturday :third)))
(define-test third-saturday-of-february-2013
  (assert-equal '(2013 2 16) (meetup:meetup 2 2013 :saturday :third)))

(define-test third-sunday-of-march-2013
  (assert-equal '(2013 3 17) (meetup:meetup 3 2013 :sunday :third)))
(define-test third-sunday-of-april-2013
  (assert-equal '(2013 4 21) (meetup:meetup 4 2013 :sunday :third)))

(define-test fourth-monday-of-march-2013
  (assert-equal '(2013 3 25) (meetup:meetup 3 2013 :monday :fourth)))
(define-test fourth-monday-of-april-2013
  (assert-equal '(2013 4 22) (meetup:meetup 4 2013 :monday :fourth)))

(define-test fourth-tuesday-of-may-2013
  (assert-equal '(2013 5 28) (meetup:meetup 5 2013 :tuesday :fourth)))
(define-test fourth-tuesday-of-june-2013
  (assert-equal '(2013 6 25) (meetup:meetup 6 2013 :tuesday :fourth)))

(define-test fourth-wednesday-of-july-2013
  (assert-equal '(2013 7 24) (meetup:meetup 7 2013 :wednesday :fourth)))
(define-test fourth-wednesday-of-august-2013
  (assert-equal '(2013 8 28) (meetup:meetup 8 2013 :wednesday :fourth)))

(define-test fourth-thursday-of-september-2013
  (assert-equal '(2013 9 26) (meetup:meetup 9 2013 :thursday :fourth)))
(define-test fourth-thursday-of-october-2013
  (assert-equal '(2013 10 24) (meetup:meetup 10 2013 :thursday :fourth)))

(define-test fourth-friday-of-november-2013
  (assert-equal '(2013 11 22) (meetup:meetup 11 2013 :friday :fourth)))
(define-test fourth-friday-of-december-2013
  (assert-equal '(2013 12 27) (meetup:meetup 12 2013 :friday :fourth)))

(define-test fourth-saturday-of-january-2013
  (assert-equal '(2013 1 26) (meetup:meetup 1 2013 :saturday :fourth)))
(define-test fourth-saturday-of-february-2013
  (assert-equal '(2013 2 23) (meetup:meetup 2 2013 :saturday :fourth)))

(define-test fourth-sunday-of-march-2013
  (assert-equal '(2013 3 24) (meetup:meetup 3 2013 :sunday :fourth)))
(define-test fourth-sunday-of-april-2013
  (assert-equal '(2013 4 28) (meetup:meetup 4 2013 :sunday :fourth)))

(define-test last-monday-of-march-2013
  (assert-equal '(2013 3 25) (meetup:meetup 3 2013 :monday :last)))
(define-test last-monday-of-april-2013
  (assert-equal '(2013 4 29) (meetup:meetup 4 2013 :monday :last)))

(define-test last-tuesday-of-may-2013
  (assert-equal '(2013 5 28) (meetup:meetup 5 2013 :tuesday :last)))
(define-test last-tuesday-of-june-2013
  (assert-equal '(2013 6 25) (meetup:meetup 6 2013 :tuesday :last)))

(define-test last-wednesday-of-july-2013
  (assert-equal '(2013 7 31) (meetup:meetup 7 2013 :wednesday :last)))
(define-test last-wednesday-of-august-2013
  (assert-equal '(2013 8 28) (meetup:meetup 8 2013 :wednesday :last)))

(define-test last-thursday-of-september-2013
  (assert-equal '(2013 9 26) (meetup:meetup 9 2013 :thursday :last)))
(define-test last-thursday-of-october-2013
  (assert-equal '(2013 10 31) (meetup:meetup 10 2013 :thursday :last)))

(define-test last-friday-of-november-2013
  (assert-equal '(2013 11 29) (meetup:meetup 11 2013 :friday :last)))
(define-test last-friday-of-december-2013
  (assert-equal '(2013 12 27) (meetup:meetup 12 2013 :friday :last)))

(define-test last-saturday-of-january-2013
  (assert-equal '(2013 1 26) (meetup:meetup 1 2013 :saturday :last)))
(define-test last-saturday-of-february-2013
  (assert-equal '(2013 2 23) (meetup:meetup 2 2013 :saturday :last)))

(define-test last-sunday-of-march-2013
  (assert-equal '(2013 3 31) (meetup:meetup 3 2013 :sunday :last)))
(define-test last-sunday-of-april-2013
  (assert-equal '(2013 4 28) (meetup:meetup 4 2013 :sunday :last)))

#-xlisp-test
(let ((*print-errors* t)
      (*print-failures* t))
  (run-tests :all :meetup-test))
(in-package #:cl-user)
(defpackage #:meetup
  (:use #:common-lisp)
  (:export #:meetup))

(in-package #:meetup)

(defconstant +weekdays+
  '(:monday :tuesday :wednesday :thursday :friday :saturday :sunday))

(defun universal-time-component (universal-time component)
  (let ((components (multiple-value-list (decode-universal-time universal-time 0))))
    (ecase component
      (:second (nth 0 components))
      (:minute (nth 1 components))
      (:hour (nth 2 components))
      (:date (nth 3 components))
      (:month (nth 4 components))
      (:year (nth 5 components))
      (:day (nth 6 components))
      (:daylight-p (nth 7 components))
      (:zone (nth 8 components)))))

(defun valid-date-p (date month year)
  (let ((ut (handler-case (encode-universal-time 0 0 0 date month year 0)
              (t () nil))))
    (and ut
         (= date (universal-time-component ut :date))
         (= month (universal-time-component ut :month))
         (= year (universal-time-component ut :year)))))

(defun weekday-distance (a b)
  "Gets the difference in days between A weekday and the next B weekday.
A and B are given as numbers representing their positions in +weekdays+"
  (1+ (mod (- a b) 7)))

(defun meetup (month year weekday nth)
  (let ((first-occurr (weekday-distance
                       (position weekday +weekdays+)
                       (universal-time-component
                        (encode-universal-time 0 0 0 1 month year 0)
                        :day))))
    (ecase nth
      (:first (list year month first-occurr))
      (:second (list year month (+ 7 first-occurr)))
      (:third (list year month (+ 14 first-occurr)))
      (:fourth (list year month (+ 21 first-occurr)))
      (:fifth (list year month (+ 28 first-occurr)))
      (:last (if (valid-date-p (+ 28 first-occurr) month year)
                 (list year month (+ 28 first-occurr))
                 (list year month (+ 21 first-occurr))))
      (:teenth (list year month (if (>= (+ 14 first-occurr) 20)
                                    (+ 7 first-occurr)
                                    (+ 14 first-occurr)))))))

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?