Challenge - 5 Problems
Vector Arithmetic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this vector addition?
Consider the following R code that adds two vectors element-wise. What is the output?
R Programming
v1 <- c(2, 4, 6) v2 <- c(1, 3, 5) v_sum <- v1 + v2 print(v_sum)
Attempts:
2 left
💡 Hint
Add each element of v1 to the corresponding element of v2.
✗ Incorrect
Element-wise addition sums each pair of elements: 2+1=3, 4+3=7, 6+5=11. The correct sum is 3, 7, 11.
❓ Predict Output
intermediate2:00remaining
What is the output of this vector multiplication?
What will this R code print when multiplying two vectors element-wise?
R Programming
a <- c(3, 0, 2) b <- c(4, 5, 6) result <- a * b print(result)
Attempts:
2 left
💡 Hint
Multiply each element of 'a' by the corresponding element of 'b'.
✗ Incorrect
Element-wise multiplication: 3*4=12, 0*5=0, 2*6=12.
🔧 Debug
advanced2:30remaining
Why does this vector subtraction code produce a warning?
Examine the code below. Why does R give a warning when subtracting these vectors?
R Programming
x <- c(10, 20, 30, 40, 50) y <- c(1, 2) z <- x - y print(z)
Attempts:
2 left
💡 Hint
Check the lengths of the vectors and how R handles recycling.
✗ Incorrect
R recycles the shorter vector y to match the length of x but warns if the longer vector length is not a multiple of the shorter one.
❓ Predict Output
advanced2:00remaining
What is the output of this vector division with recycling?
What will this R code print when dividing vectors of different lengths?
R Programming
v1 <- c(8, 16, 24, 32, 40) v2 <- c(2, 4) result <- v1 / v2 print(result)
Attempts:
2 left
💡 Hint
Remember how R recycles the shorter vector to match the longer one.
✗ Incorrect
v2 is recycled as (2,4,2,4,2). Division: 8/2=4, 16/4=4, 24/2=12, 32/4=8, 40/2=20. A warning occurs because 5 is not a multiple of 2; R warns when the length of the longer vector is not a multiple of the shorter vector.
❓ Predict Output
expert3:00remaining
What is the value of 'result' after this complex vector operation?
Given the code below, what is the value of the variable 'result'?
R Programming
v <- c(1, 2, 3, 4, 5) result <- (v * 2) - (v %% 2) + c(5, 4, 3, 2, 1) print(result)
Attempts:
2 left
💡 Hint
Calculate each part step-by-step: multiply, modulo, then add.
✗ Incorrect
Step 1: v*2 = c(2,4,6,8,10)
Step 2: v%%2 = c(1,0,1,0,1)
Step 3: (v*2) - (v%%2) = c(1,4,5,8,9)
Step 4: + c(5,4,3,2,1) = c(6,8,8,10,10)