Published at Mar 03 2020
·
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.

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

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.

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 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.

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

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

```
(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 %SECONDS-IN-DAY% 86400)
(defun search-date (start-date callable &optional backward)
(flet ((applier (encoded-date)
(multiple-value-bind (second minute hour day month year day-of-the-week dst timezone)
(decode-universal-time encoded-date)
(declare (ignore second minute hour year month dst timezone))
(funcall callable day day-of-the-week))))
(do ((repeater 1 (1+ repeater))
(encoded-date start-date (+ encoded-date (if backward (- %SECONDS-IN-DAY%) %SECONDS-IN-DAY%))))
((or (> repeater 30) (applier encoded-date)) encoded-date))))
(defvar *WEEKDAYS* '(:monday :tuesday :wednesday :thursday :friday :saturday :sunday))
(defun get-start-of-month (month year)
(encode-universal-time 0 0 0 1 month year 0))
(defun get-end-of-month (month year)
(let ((corrected-year (if (eq month 12) (1+ year) year))
(corrected-month (if (eq month 12) 1 (1+ month))))
(- (encode-universal-time 0 0 0 1 corrected-month corrected-year 0) %SECONDS-IN-DAY%)))
(defun meetup (month year weekday position)
(let* ((start-of-month (get-start-of-month month year))
(end-of-month (get-end-of-month month year))
(weekday-num (position weekday *WEEKDAYS*))
(lambda-counter 0)
(get-day-lambda (lambda (day day-of-week)
(cond
((and (eq day-of-week weekday-num) (zerop lambda-counter)) t)
((eq day-of-week weekday-num) (decf lambda-counter) nil)
(t nil))))
(get-teenth-lambda (lambda (day day-of-week)
(and (eq day-of-week weekday-num)
(<= 13 day 19))))
(callable
(ecase position
(:first get-day-lambda)
(:second (setf lambda-counter 1) get-day-lambda)
(:third (setf lambda-counter 2) get-day-lambda)
(:fourth (setf lambda-counter 3) get-day-lambda)
(:fifth (setf lambda-counter 4)get-day-lambda)
(:teenth get-teenth-lambda)
(:last get-day-lambda)))
(found-date
(search-date (if (eq position :last) end-of-month start-of-month)
callable
(eq position :last))))
(multiple-value-bind (second minute hour day month year day-of-the-week dst timezone)
(decode-universal-time found-date)
(list year month day))))
```

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?

Level up your programming skills with 3,373 exercises across 50 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments