Menu
Coddy logo textTech

Recap - Stock Desk

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

challenge icon

Challenge

Medium

Complete process_orders(stock, orders). Handle the orders in order and return one line for each:

  • unknown <item> when the item is not in stock
  • out of <item> when its quantity is 0
  • sold <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

Practice on your own: Online R compiler