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)

6, "I"     -> "IIIIII"
5, "Hello" -> "HelloHelloHelloHelloHello"

13.2 YouTube Video

13.3 Solution Code

repeat_string <- function(count, str) {
  str_c <- str
  for(i in 2:count){
    str_c <- paste0(str_c, str)
  }
  return(str_c)
}
repeat_string <- function(count, str) {
  paste(rep(str, count), collapse = "")
}
repeat_string <- function(count, str) strrep(str, count)

13.4 Test

library(testthat)

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')
})
Test passed 🥳

13.5 Supported by

StarCore Analytics