Solution Review: Append Hearts
In this review, we provide a detailed analysis of the solution to the given problem
Solution #1: Using rep()
Press + to interact
R
hearts <- function(testVariable){heart <- "<3"vectorOfHearts <- vector("character", 0)for(i in testVariable){x <- rep(heart, i)x <- paste(x, collapse = "")vectorOfHearts <-c(vectorOfHearts, x)}return(vectorOfHearts)}# Driver CodetestVariable1 <- c(5, 2)testVariable2 <- c(3, 3)hearts(testVariable1)hearts(testVariable2)
Explanation
In the code snippet above, we first create a function:
hearts <-
...