diff --git a/exercises/acronym/example.R b/exercises/acronym/example.R index e790915d..2d5112c8 100644 --- a/exercises/acronym/example.R +++ b/exercises/acronym/example.R @@ -1,12 +1,6 @@ acronym <- function(input) { - # split the string by spaces or hyphens - split <- strsplit(input, " |-") - - # get the first letter of each substring - first_letters <- substring(split[[1]], 1, 1) - - # join the acronym back together & make uppercase - acronym <- paste(toupper(first_letters), collapse = "") - - return(acronym) + words <- strsplit(input, " |-")[[1]] + words <- gsub("[[:punct:]]", "", words) + first_letters <- lapply(words, substring, 1, 1) + toupper(paste(first_letters, collapse = "")) } diff --git a/exercises/acronym/test_acronym.R b/exercises/acronym/test_acronym.R index 8cc69f03..6320e112 100644 --- a/exercises/acronym/test_acronym.R +++ b/exercises/acronym/test_acronym.R @@ -36,5 +36,19 @@ test_that("Very long abbreviation", { expect_equal(acronym(input), "ROTFLSHTMDCOALM") }) -message("All tests passed for exercise: acronym") +test_that("Consecutive delimiters", { + input <- "Something - I made up from thin air" + expect_equal(acronym(input), "SIMUFTA") +}) + +test_that("Apostrophes", { + input <- "Halley's Comet" + expect_equal(acronym(input), "HC") +}) +test_that("Underscore emphasis", { + input <- "The Road _Not_ Taken" + expect_equal(acronym(input), "TRNT") +}) + +message("All tests passed for exercise: acronym")