🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of katrinleinweber

katrinleinweber's solution

to Two Fer in the R Track

Published at Feb 20 2019 · 0 comments
Instructions
Test suite
Solution

Two-fer or 2-fer is short for two for one. One for you and one for me.

"One for X, one for me."

When X is a name or "you".

If the given name is "Alice", the result should be "One for Alice, one for me." If no name is given, the result should be "One for you, one for me."

Installation

See this guide for instructions on how to setup your local R environment.

How to implement your solution

In each problem folder, there is a file named <exercise_name>.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_<exercise_name>.R script. This can be conveniently done with testthat's auto_test function. 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_<exercise_name>.R.

Source

https://en.wikipedia.org/wiki/Two-fer

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

test_two-fer.R

source("./two-fer.R")
library(testthat)

context("two-fer")

test_that("Abbreviate a phrase", {
  expect_equal(two_fer(), "One for you, one for me.")
})

test_that("Abbreviate a phrase", {
  expect_equal(two_fer("Alice"), "One for Alice, one for me.")
})

test_that("Abbreviate a phrase", {
  expect_equal(two_fer("Bob"), "One for Bob, one for me.")
})

message("All tests passed for exercise: two-fer")
two_fer <- function(input = 'you') {
  sprintf('One for %s, one for me.', input)
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?