Menu
Coddy logo textTech

Recap - Task Queue

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

challenge icon

Challenge

Medium

Build a task queue from two Reference Classes. Both definitions are wrapped in suppressMessages().

  • Task in Task.R: fields title, priority (numeric) and done (logical); initialize makes done FALSE when it is not given; label() returns [x] fix (3) for a finished task or [ ] fix (3).
  • TaskQueue in TaskQueue.R: the field tasks (a list); add(title, priority) appends a new Task and returns invisible(.self); next_task() returns the title of the open task with the highest priority (the earliest added on a tie), or nothing; complete(title) marks the first open task with that title as done and returns TRUE, or FALSE; report() returns one label per task in the order added, then open: 2.

The supplied code reads add fix 3, next and done fix, then adds two tasks in one chained call and prints the report.

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

Try it yourself

source("Task.R")
source("TaskQueue.R")

# Supplied input/output code: keep it as it is
input <- suppressWarnings(readLines(file("stdin")))
q <- TaskQueue$new()
for (line in input) {
  p <- strsplit(line, " ")[[1]]
  if (p[1] == "add") {
    q$add(p[2], as.numeric(p[3]))
  } else if (p[1] == "next") {
    cat("next: ", q$next_task(), "\n", sep = "")
  } else if (p[1] == "done") {
    cat(if (q$complete(p[2])) "completed " else "no open task ", p[2], "\n", sep = "")
  }
}
q$add("late", 1)$add("later", 1)
writeLines(q$report())

All lessons in Object Oriented Programming

Practice on your own: Online R compiler