Skip to content

Meetup

Intro

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.

Task

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.

The Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "meetup.h"
#include "string.h"
#include "time.h"
#include <stdio.h>
#include <time.h>

int dayOfWeek(int y, int m, int d);

int dayOfWeek(int y, int m, int d) {
struct tm tm = {.tm_year = y, .tm_mon = m, .tm_mday = d};
time_t t = mktime(&tm);
int eval = localtime(&t)->tm_wday;
return eval;
}

int meetup_day_of_month(unsigned int year, unsigned int month, const char *week,
                        const char *day_of_week) {
int rev = 0;
int date = -1;
if (strcmp(week, "teenth") == 0)
    date = 13;
else if (strcmp(week, "first") == 0)
    date = 1;
else if (strcmp(week, "second") == 0)
    date = 8;
else if (strcmp(week, "third") == 0)
    date = 15;
else if (strcmp(week, "fourth") == 0)
    date = 22;
else if (strcmp(week, "last") == 0) {
    switch (month) {
    case 4:
    case 6:
    case 9:
    case 11:
    date = 30;
    rev = 1;
    break;
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
    date = 31;
    rev = 1;
    break;
    case 2:
    if (year % 4 == 0)
        date = 29;
    else
        date = 28;
    rev = 1;
    break;
    }
}

int weekday02 = -1;
if (strcmp(day_of_week, "Monday") == 0)
    weekday02 = 1;
else if (strcmp(day_of_week, "Tuesday") == 0)
    weekday02 = 2;
else if (strcmp(day_of_week, "Wednesday") == 0)
    weekday02 = 3;
else if (strcmp(day_of_week, "Thursday") == 0)
    weekday02 = 4;
else if (strcmp(day_of_week, "Friday") == 0)
    weekday02 = 5;
else if (strcmp(day_of_week, "Saturday") == 0)
    weekday02 = 6;
else if (strcmp(day_of_week, "Sunday") == 0)
    weekday02 = 0;

int i = 0;
if (rev == 0) {
    for (i = date; dayOfWeek(year - 1900, month - 1, i) != weekday02; i++) {
    }
} else {
    for (i = date; dayOfWeek(year - 1900, month - 1, i) != weekday02; i--) {
    }
}
return i;
}
1
2
3
4
5
6
7
#ifndef MEETUP_H
#define MEETUP_H

int meetup_day_of_month(unsigned int year, unsigned int month, const char *week,
                        const char *day_of_week);

#endif

Last update: February 8, 2021

Comments