Skip to content

Commit ede0294

Browse files
Add meetup exercise (#75)
1 parent 6a852c3 commit ede0294

File tree

8 files changed

+962
-0
lines changed

8 files changed

+962
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,14 @@
513513
"prerequisites": [],
514514
"difficulty": 8
515515
},
516+
{
517+
"slug": "meetup",
518+
"name": "Meetup",
519+
"uuid": "6dcd24ed-902b-4fa4-9816-e28b87c157f0",
520+
"practices": [],
521+
"prerequisites": [],
522+
"difficulty": 8
523+
},
516524
{
517525
"slug": "wordy",
518526
"name": "Wordy",
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Instructions
2+
3+
Your task is to find the exact date of a meetup, given a month, year, weekday and week.
4+
5+
There are six week values to consider: `first`, `second`, `third`, `fourth`, `last`, `teenth`.
6+
7+
For example, you might be asked to find the date for the meetup on the first Monday in January 2018 (January 1, 2018).
8+
9+
Similarly, you might be asked to find:
10+
11+
- the third Tuesday of August 2019 (August 20, 2019)
12+
- the teenth Wednesday of May 2020 (May 13, 2020)
13+
- the fourth Sunday of July 2021 (July 25, 2021)
14+
- the last Thursday of November 2022 (November 24, 2022)
15+
- the teenth Saturday of August 1953 (August 15, 1953)
16+
17+
## Teenth
18+
19+
The teenth week refers to the seven days in a month that end in '-teenth' (13th, 14th, 15th, 16th, 17th, 18th and 19th).
20+
21+
If asked to find the teenth Saturday of August, 1953, we check its calendar:
22+
23+
```plaintext
24+
August 1953
25+
Su Mo Tu We Th Fr Sa
26+
1
27+
2 3 4 5 6 7 8
28+
9 10 11 12 13 14 15
29+
16 17 18 19 20 21 22
30+
23 24 25 26 27 28 29
31+
30 31
32+
```
33+
34+
From this we find that the teenth Saturday is August 15, 1953.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Introduction
2+
3+
Every month, your partner meets up with their best friend.
4+
Both of them have very busy schedules, making it challenging to find a suitable date!
5+
Given your own busy schedule, your partner always double-checks potential meetup dates with you:
6+
7+
- "Can I meet up on the first Friday of next month?"
8+
- "What about the third Wednesday?"
9+
- "Maybe the last Sunday?"
10+
11+
In this month's call, your partner asked you this question:
12+
13+
- "I'd like to meet up on the teenth Thursday; is that okay?"
14+
15+
Confused, you ask what a "teenth" day is.
16+
Your partner explains that a teenth day, a concept they made up, refers to the days in a month that end in '-teenth':
17+
18+
- 13th (thirteenth)
19+
- 14th (fourteenth)
20+
- 15th (fifteenth)
21+
- 16th (sixteenth)
22+
- 17th (seventeenth)
23+
- 18th (eighteenth)
24+
- 19th (nineteenth)
25+
26+
As there are also seven weekdays, it is guaranteed that each day of the week has _exactly one_ teenth day each month.
27+
28+
Now that you understand the concept of a teenth day, you check your calendar.
29+
You don't have anything planned on the teenth Thursday, so you happily confirm the date with your partner.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"meetup.fut"
8+
],
9+
"test": [
10+
"test.fut"
11+
],
12+
"example": [
13+
".meta/example.fut"
14+
]
15+
},
16+
"blurb": "Calculate the date of meetups.",
17+
"source": "Jeremy Hinegardner mentioned a Boulder meetup that happens on the Wednesteenth of every month"
18+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
2+
def equal [m] [n] (first: [m]u8) (second: [n]u8): bool =
3+
if m != n then false else
4+
let same = loop same = true for i < n do
5+
if first[i] == second[i] then same else
6+
false
7+
in
8+
same
9+
10+
def write (start: i64) (len: i64) (value: i32) (buffer: *[]u8): *[]u8 =
11+
let (buffer, _) = loop (buffer, value) = (buffer, value) for i < len do
12+
let digit = '0' + u8.i32 (value % 10)
13+
in
14+
(buffer with [start + len - 1 - i] = digit, value / 10)
15+
in
16+
buffer
17+
18+
def is_leap (year: i32): bool =
19+
(year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))
20+
21+
def days_in_month (year: i32) (month: i32): i32 =
22+
match month
23+
case 1 -> 31
24+
case 2 -> if is_leap year then 29 else 28
25+
case 3 -> 31
26+
case 4 -> 30
27+
case 5 -> 31
28+
case 6 -> 30
29+
case 7 -> 31
30+
case 8 -> 31
31+
case 9 -> 30
32+
case 10 -> 31
33+
case 11 -> 30
34+
case 12 -> 31
35+
case _ -> assert false 0
36+
37+
def week_concludes (year: i32) (month: i32) (week: []u8): i32 =
38+
if equal week "first" then 7 else
39+
if equal week "second" then 14 else
40+
if equal week "third" then 21 else
41+
if equal week "fourth" then 28 else
42+
if equal week "teenth" then 19 else
43+
if equal week "last" then days_in_month year month else
44+
assert false 0
45+
46+
def required_day (dayofweek: []u8): i32 =
47+
if equal dayofweek "Monday" then 0 else
48+
if equal dayofweek "Tuesday" then 1 else
49+
if equal dayofweek "Wednesday" then 2 else
50+
if equal dayofweek "Thursday" then 3 else
51+
if equal dayofweek "Friday" then 4 else
52+
if equal dayofweek "Saturday" then 5 else
53+
if equal dayofweek "Sunday" then 6 else
54+
assert false 0
55+
56+
def month_offset (month: i32): i32 =
57+
match month
58+
case 1 -> 307 -- offset from the end of February of previous year
59+
case 2 -> 338
60+
case 3 -> 1
61+
case 4 -> 32
62+
case 5 -> 62
63+
case 6 -> 93
64+
case 7 -> 123
65+
case 8 -> 154
66+
case 9 -> 185
67+
case 10 -> 215
68+
case 11 -> 246
69+
case 12 -> 276
70+
case _ -> assert false 0
71+
72+
def concluding_day (year: i32) (month: i32) (dayofmonth: i32): i32 =
73+
let year = if month < 3 then year - 1 else year
74+
in
75+
(year + (year / 4) - (year / 100) + (year / 400) + (month_offset month) + dayofmonth) % 7
76+
77+
def meetup_dayofmonth (year: i32) (month: i32) (week: []u8) (dayofweek: []u8): i32 =
78+
let day = week_concludes year month week
79+
let concluding = concluding_day year month day
80+
let required = required_day dayofweek
81+
let adjustment = if concluding < required then 7 else 0
82+
in
83+
day + required - (concluding + adjustment)
84+
85+
def meetup (year: i32) (month: i32) (week: []u8) (dayofweek: []u8): []u8 =
86+
let dayofmonth = meetup_dayofmonth year month week dayofweek
87+
in
88+
(write 0 4 year (write 5 2 month (write 8 2 dayofmonth (replicate 10 '-'))))

0 commit comments

Comments
 (0)