Recap - Discount Rules
Part of the Logic & Flow section of Coddy's R journey. Lesson 31 of 64.
Challenge
MediumA 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 2minus5: subtract 5, but never go below 0tax: add 10%round: round to a whole number withround()
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
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