Menu
Coddy logo textTech

Recap - Discount Rules

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

challenge icon

Challenge

Medium

A shop applies pricing rules to a price one after another. Complete the list rules with these anonymous functions of one price:

  • half: divide the price by 2
  • minus5: subtract 5, but never go below 0
  • tax: add 10%
  • round: round to a whole number with round()

Then complete apply_rules(price, names): apply the rules named in names in order and return one line per rule, after half: 20.00, followed by final: and the final price. Show every price with two decimals.

The supplied code reads the price and a comma-separated list of rule names and prints each returned line.

Try it yourself

rules <- list(
  # Write your code here
)

apply_rules <- function(price, names) {
  # Write your code here
  character(0)
}

# Supplied input/output code: keep it as it is
input <- suppressWarnings(readLines(file("stdin")))
cat(apply_rules(as.numeric(input[1]), strsplit(input[2], ",")[[1]]), sep = "\n")

All lessons in Logic & Flow

Practice on your own: Online R compiler