Menu
Coddy logo textTech

Recap - Price Vector

Part of the Fundamentals section of Coddy's R journey — lesson 60 of 78.

challenge icon

Challenge

Easy

You will receive two lines of input:

  1. A comma-separated list of original prices (e.g., 100,200,150,80)
  2. A discount percentage as a whole number (e.g., 20 for 20%)

Perform the following operations:

  1. Create a vector from the comma-separated prices
  2. The store manager noticed the third item was mispriced. Change it to 125
  3. Apply the discount to all prices (multiply by (100 - discount) / 100)
  4. Print the discounted prices vector
  5. Print the total of all discounted prices using sum()
  6. Print the average discounted price using mean()

For example, if the inputs are:

100,200,150,80
20

The output should be:

[1]  80 160 100  64
[1] 404
[1] 101

Explanation: 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