Recap - Reversed Vector
Part of the Fundamentals section of Coddy's R journey — lesson 61 of 78.
Challenge
EasyYou will receive a single line of input containing comma-separated numbers (e.g., 10,20,30,40,50).
Perform the following operations:
- Create a vector from the comma-separated input
- Reverse the vector so the last element becomes first
- Print the reversed vector
- Print the sum of the first and last elements of the reversed vector
For example, if the input is:
5,15,25,35,45The output should be:
[1] 45 35 25 15 5
[1] 50Explanation: The original vector c(5, 15, 25, 35, 45) is reversed to c(45, 35, 25, 15, 5). The sum of the first element (45) and last element (5) of the reversed vector is 50.
Try it yourself
# Read input
con <- file("stdin", "r")
input <- suppressWarnings(readLines(con, n = 1))
# Parse the comma-separated numbers into a vector
numbers <- as.numeric(strsplit(input, ",")[[1]])
# TODO: Write your code below
# 1. Reverse the vector
# 2. Print the reversed vector
# 3. Calculate and print the sum of first and last elements of the reversed vectorAll 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 Input11Vectors
Creating Vectors with c()Accessing Vector ElementsModifying VectorsVector OperationsRecap - Price VectorRecap - Reversed VectorNamed Vectors3Operators 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