6, "I" -> "IIIIII"
5, "Hello" -> "HelloHelloHelloHelloHello"
13 String repeat
8kyu Tantangan #13/366 - 27 Feb 2024
https://www.codewars.com/kata/57a0e5c372292dd76d000d7e
13.1 Instruction
Write a function that accepts an integer n
and a string s
as parameters, and returns a string of s
repeated exactly n
times.
Examples (input -> output)
13.2 YouTube Video
13.3 Solution Code
<- function(count, str) {
repeat_string <- str
str_c for(i in 2:count){
<- paste0(str_c, str)
str_c
}return(str_c)
}
<- function(count, str) {
repeat_string paste(rep(str, count), collapse = "")
}
<- function(count, str) strrep(str, count) repeat_string
13.4 Test
library(testthat)
::test_that("Example tests", {
testthat::expect_equal(repeat_string(4, 'a'), 'aaaa')
testthat::expect_equal(repeat_string(3, 'hello '), 'hello hello hello ')
testthat::expect_equal(repeat_string(2, 'abc'), 'abcabc')
testthat })
Test passed 🥳