Menu
Coddy logo textTech

Event Scheduler

Part of the Object Oriented Programming section of Coddy's R journey. Lesson 56 of 57.

challenge icon

Challenge

Hard

Build an event scheduler over two files.

  • Event.R: the S3 class event with the fields title, day, start, end (hours). Write new_event(); validate_event(), which stops with day must be from 1 to 31 or hours must be within 0 to 24 and start before end; the helper event(title, day, start, end), which converts the text arguments to numbers; format.event(), which returns day 3 09-11 standup; and overlaps(a, b), TRUE when two events are on the same day and each starts before the other ends.
  • Scheduler.R: make_scheduler() keeps its events private and returns add(e), which replies duplicate for the same title on the same day, conflicts with review when the event overlaps an existing one, or added; agenda(), the formatted events sorted by day, start and title; and busiest_day(), busiest: day 3 (4 hours) by total hours (the earlier day on a tie), or no events.

Use call. = FALSE. The supplied code reads lines title,day,start,end, prints the reply for each, then the agenda and the busiest day.

Your code goes in Event.R and Scheduler.R. main.R holds the supplied input/output code and cannot be edited.

Try it yourself

source("Event.R")
source("Scheduler.R")

# Supplied input/output code: keep it as it is
input <- suppressWarnings(readLines(file("stdin")))
s <- make_scheduler()
for (line in input) {
  p <- strsplit(line, ",")[[1]]
  reply <- tryCatch(s$add(event(p[1], p[2], p[3], p[4])), error = function(e) paste("invalid:", conditionMessage(e)))
  cat(p[1], ": ", reply, "\n", sep = "")
}
cat("agenda:\n")
for (l in s$agenda()) cat("  ", l, "\n", sep = "")
cat(s$busiest_day(), "\n", sep = "")

All lessons in Object Oriented Programming

Practice on your own: Online R compiler