From 8bc030bc55e3859c3f666cd593a04aea5bf8eaf8 Mon Sep 17 00:00:00 2001 From: Zacchaeus Date: Tue, 12 Jun 2018 13:22:11 +0100 Subject: [PATCH 1/2] Add acronym exercise --- config.json | 11 +++++++++++ exercises/acronym/README.md | 24 ++++++++++++++++++++++++ exercises/acronym/acronym.R | 3 +++ exercises/acronym/example.R | 9 +++++++++ exercises/acronym/test_acronym.R | 31 +++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+) create mode 100644 exercises/acronym/README.md create mode 100644 exercises/acronym/acronym.R create mode 100644 exercises/acronym/example.R create mode 100644 exercises/acronym/test_acronym.R diff --git a/config.json b/config.json index 5ed87b99..c26cd039 100644 --- a/config.json +++ b/config.json @@ -358,6 +358,17 @@ "control_flow_loops", "text_formatting" ] + }, + { + "slug": "acronym", + "uuid": "54e60174-4833-418c-b9af-2fcb5722b1d6", + "core": false, + "unlocked_by": null, + "difficulty": 1, + "topics": [ + "strings", + "filtering" + ] } ] } diff --git a/exercises/acronym/README.md b/exercises/acronym/README.md new file mode 100644 index 00000000..9f15c706 --- /dev/null +++ b/exercises/acronym/README.md @@ -0,0 +1,24 @@ +# Acronym + +Convert a phrase to its acronym. + +Techies love their TLA (Three Letter Acronyms)! + +Help generate some jargon by writing a program that converts a long name +like Portable Network Graphics to its acronym (PNG). + +## Installation +See [this guide](https://github.com/exercism/xr/blob/master/docs/INSTALLATION.md) for instructions on how to setup your local R environment. + +## How to implement your solution +In each problem folder, there is a file named `.R` containing a function that returns a `NULL` value. Place your implementation inside the body of the function. + +## How to run tests +Inside of RStudio, simply execute the `test_.R` script. This can be conveniently done with [testthat's `auto_test` function](https://www.rdocumentation.org/packages/testthat/topics/auto_test). Because exercism code and tests are in the same folder, use this same path for both `code_path` and `test_path` parameters. On the command-line, you can also run `Rscript test_.R`. + +## Source + +Julien Vanier [https://github.com/monkbroc](https://github.com/monkbroc) + +## Submitting Incomplete Solutions +It's possible to submit an incomplete solution so you can see how others have completed the exercise. diff --git a/exercises/acronym/acronym.R b/exercises/acronym/acronym.R new file mode 100644 index 00000000..ec6a2a66 --- /dev/null +++ b/exercises/acronym/acronym.R @@ -0,0 +1,3 @@ +acronym <- function(input) { + +} \ No newline at end of file diff --git a/exercises/acronym/example.R b/exercises/acronym/example.R new file mode 100644 index 00000000..a5c6ae3f --- /dev/null +++ b/exercises/acronym/example.R @@ -0,0 +1,9 @@ +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) + + return(paste(toupper(first_letters), collapse="")) +} \ No newline at end of file diff --git a/exercises/acronym/test_acronym.R b/exercises/acronym/test_acronym.R new file mode 100644 index 00000000..fb921dd0 --- /dev/null +++ b/exercises/acronym/test_acronym.R @@ -0,0 +1,31 @@ +source("./acronym.R") +library(testthat) + +context("acronym") + +test_that("Abbreviate a phrase", { + input <- "Portable Network Graphics" + expect_equal(acronym(input), "PNG") +}) + +test_that("Lowercase words", { + input <- "Ruby on Rails" + expect_equal(acronym(input), "ROR") +}) + +test_that("Punctuation", { + input <- "First In, First Out" + expect_equal(acronym(input), "FIFO") +}) + +test_that("All caps word", { + input <- "GNU Image Manipulation Program" + expect_equal(acronym(input), "GIMP") +}) + +test_that("Punctuation without whitespace", { + input <- "Complementary metal-oxide semiconductor" + expect_equal(acronym(input), "CMOS") +}) + +message("All tests passed for exercise: acronym") \ No newline at end of file From f4beddfb9f31159b9a298ecfda5de06ccf2305f4 Mon Sep 17 00:00:00 2001 From: Zacchaeus Date: Tue, 12 Jun 2018 13:33:59 +0100 Subject: [PATCH 2/2] Fix linting & improve readability --- exercises/acronym/example.R | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/exercises/acronym/example.R b/exercises/acronym/example.R index a5c6ae3f..1a465d5e 100644 --- a/exercises/acronym/example.R +++ b/exercises/acronym/example.R @@ -2,8 +2,11 @@ acronym <- function(input) { # split the string by spaces or hyphens split <- strsplit(input, " |-") - #get the first letter of each substring + # 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(paste(toupper(first_letters), collapse="")) + return(acronym) } \ No newline at end of file