# tvirolai's solution

## to Meetup in the Common Lisp Track

Published at Mar 14 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.

## 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")

(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)

(defun day-of-week (day)
(case day
(0 :monday)
(1 :tuesday)
(2 :wednesday)
(3 :thursday)
(4 :friday)
(5 :saturday)
(6 :sunday)))

(defun days-in-month (year month)
"Return a list of the days in a month."
(let ((days (remove-if-not #'(lambda (date)
(map 'list #'(lambda (day)
(multiple-value-bind
(second minute hour date month year weekday dst-p tz)
(decode-universal-time
(encode-universal-time 0 0 0 day month year 0)
0)
(list year month date (day-of-week weekday))))
(range 32)))))
days))

(defun parse-res (res)
(subseq res 0 3))

(defun meetup (month year weekday ordinal)
(let* ((days (days-in-month year month))
(fitting-days (remove-if-not #'(lambda (date)
(string= weekday (fourth date))) days)))
(cond
((string= "teenth" (string-downcase ordinal)) (let ((res (remove-if-not #'(lambda (date)
(>= 19 (third date) 13)) fitting-days)))
(parse-res (car res))))
((string= "first" (string-downcase ordinal)) (parse-res (first fitting-days)))
((string= "second" (string-downcase ordinal)) (parse-res (second fitting-days)))
((string= "third" (string-downcase ordinal)) (parse-res (third fitting-days)))
((string= "fourth" (string-downcase ordinal)) (parse-res (fourth fitting-days)))
((string= "last" (string-downcase ordinal)) (parse-res (car (last fitting-days)))))))``````

### 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?