Menu
Coddy logo textTech

Recap - Seating Chart

Part of the Logic & Flow section of Coddy's R journey. Lesson 21 of 64.

challenge icon

Challenge

Medium

Complete seat_report(seats). seats is a matrix of 0s (free seats) and 1s (taken seats), one row per row of the theatre. Return:

  • one line per row, Row 1: 2 free
  • Most free: row followed by the row with the most free seats (the first one when several tie)
  • Occupied: followed by the percentage of taken seats, rounded to a whole number, and %

The supplied code reads a matrix from one line: rows are separated by ; and the values of a row by ,, so 1,2;3,4 is a matrix with the rows 1 2 and 3 4. The code prints each returned line.

Try it yourself

seat_report <- function(seats) {
  # Write your code here
  character(0)
}

# Supplied input/output code: keep it as it is
input <- suppressWarnings(readLines(file("stdin")))
parse_matrix <- function(line) {
  rows <- strsplit(line, ";")[[1]]
  matrix(as.numeric(unlist(strsplit(rows, ","))), nrow = length(rows), byrow = TRUE)
}
cat(seat_report(parse_matrix(input[1])), sep = "\n")

All lessons in Logic & Flow

Practice on your own: Online R compiler