Recap - Price Vector
Part of the Fundamentals section of Coddy's R journey — lesson 60 of 78.
Challenge
EasyYou will receive two lines of input:
- A comma-separated list of original prices (e.g.,
100,200,150,80) - A discount percentage as a whole number (e.g.,
20for 20%)
Perform the following operations:
- Create a vector from the comma-separated prices
- The store manager noticed the third item was mispriced. Change it to
125 - Apply the discount to all prices (multiply by
(100 - discount) / 100) - Print the discounted prices vector
- Print the total of all discounted prices using
sum() - Print the average discounted price using
mean()
For example, if the inputs are:
100,200,150,80
20The output should be:
[1] 80 160 100 64
[1] 404
[1] 101Explanation: The third price is changed to 125, then a 20% discount is applied (multiply by 0.8), resulting in prices 80, 160, 100, and 64.
Try it yourself
# Read input
con <- file("stdin", "r")
prices_input <- suppressWarnings(readLines(con, n = 1))
discount <- suppressWarnings(readLines(con, n = 1))
close(con)
# Convert inputs
original_prices <- as.numeric(unlist(strsplit(prices_input, ",")))
discount_percent <- as.numeric(discount)
# TODO: Write your code below
# 1. Change the third item to 125
# 2. Apply the discount to all prices
# 3. Print the discounted prices vector
# 4. Print the total using sum()
# 5. Print the average using mean()All lessons in Fundamentals
4Operators Part 2
Logical Operators (AND, OR)Logical Operators Part 2 (NOT)Recap - Simple LogicVectorized Logic Part 1Vectorized Logic Part 22Variables and Data Types
Numeric Data TypeInteger Data TypeCharacter Data TypeLogical Data TypeChecking Data TypesNaming ConventionsMissing Values: NARecap - Variable Creation8Loops
For LoopWhile LoopBreakNext (Continue)Recap - FactorialSequence Generation (seq, :)Nested LoopsRecap - Dynamic Input3Operators Part 1
Arithmetic OperatorsInteger Division and ModuloAssignment OperatorsRecap - Simple MathComparison Operators6Basic IO
Print OutputCat for OutputOutput With VariablesReading Input with readline()Type Conversion BasicsRecap - Age CalculatorRecap - True or False9Functions
Declaring a FunctionFunction ArgumentsReturn ValuesRecap - Sigma FunctionRecap - Validation FunctionDefault Parameter Values