Recap - Seating Chart
Part of the Logic & Flow section of Coddy's R journey. Lesson 21 of 64.
Challenge
MediumComplete 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: rowfollowed 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
1Strings In Depth
Substrings with substr()Formatting with sprintf()Splitting and JoiningSearching StringsReplacing TextRecap - Username Builder4Matrices
Creating MatricesIndexing MatricesRow and Column SummariesMatrix ArithmeticRecap - Seating Chart2Key-Value Lookups
Named Vector LookupsChecking KeysAdding and Removing KeysLooping Over NamesRecap - Stock Desk3Sets and Counting
Unique ValuesSet OperationsMembership TestsCounting with table()Recap - Event Guests6Functions as Values
Anonymous FunctionsPassing FunctionsReturning FunctionsClosures with StateRecap - Discount Rules9Data Frames
Creating Data FramesColumns and RowsFiltering RowsAdding and SortingRecap - Sales Report12Project - Expense Tracker
Recording ExpensesTotal SpendingPractice on your own: Online R compiler