Recap - Stock Desk
Part of the Logic & Flow section of Coddy's R journey. Lesson 11 of 64.
Challenge
MediumComplete process_orders(stock, orders). Handle the orders in order and return one line for each:
unknown <item>when the item is not instockout of <item>when its quantity is 0sold <item>otherwise, and its quantity goes down by 1
After the orders, add one line per stocked item in alphabetical order: <item>: <quantity>.
The supplied code reads the stock as name=quantity pairs and the orders as a comma-separated line, and prints each returned line.
Try it yourself
process_orders <- function(stock, orders) {
# Write your code here
character(0)
}
# Supplied input/output code: keep it as it is
input <- suppressWarnings(readLines(file("stdin")))
stock <- c()
for (pair in strsplit(input[1], ",")[[1]]) {
kv <- strsplit(pair, "=")[[1]]
stock[kv[1]] <- as.numeric(kv[2])
}
cat(process_orders(stock, strsplit(input[2], ",")[[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