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

katrinleinweber's solution

to Isogram in the R Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

Determine if a word or phrase is an isogram.

An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter, however spaces and hyphens are allowed to appear multiple times.

Examples of isograms:

  • lumberjacks
  • background
  • downstream
  • six-year-old

The word isograms, however, is not an isogram, because the s repeats.

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

Wikipedia https://en.wikipedia.org/wiki/Isogram

Submitting Incomplete Solutions

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

test_isogram.R

source("./isogram.R")
library(testthat)

context("isogram")

test_that("empty string", {
  word <- ""
  expect_equal(is_isogram(word), TRUE)
})

test_that("isogram with only lower case characters", {
  word <- "isogram"
  expect_equal(is_isogram(word), TRUE)
})

test_that("word with one duplicated character", {
  word <- "eleven"
  expect_equal(is_isogram(word), FALSE)
})

test_that("longest reported english isogram", {
  word <- "subdermatoglyphic"
  expect_equal(is_isogram(word), TRUE)
})

test_that("word with duplicated character in mixed case", {
  word <- "Alphabet"
  expect_equal(is_isogram(word), FALSE)
})

test_that("hypothetical isogrammic word with hyphen", {
  word <- "thumbscrew-japingly"
  expect_equal(is_isogram(word), TRUE)
})

test_that("isogram with duplicated non letter character", {
  word <- "Hjelmqvist-Gryb-Zock-Pfund-Wax"
  expect_equal(is_isogram(word), TRUE)
})

test_that("made-up name that is an isogram", {
  word <- "Emily Jung Schwartzkopf"
  expect_equal(is_isogram(word), TRUE)
})

message("All tests passed for exercise: isogram")
library(magrittr)

is_isogram <- function(word) {
  
  word %>% 
    gsub(x = ., pattern = "\\W", replacement = "") %>%
    strsplit("") %>% 
    unlist() %>% 
    tolower() %>% 
    identical(., unique(.))
  # compare letter vector before & after unique()
}

Community comments

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

Thanks for showing me identical. I like the way you use named parameters for your gsub.

Take note that the \w character class includes the underscore, so that kind of goes against the spirit of the "isogram with duplicated non letter character" unit test. I used the pattern [^[:alpha:]] which I think is more precise.

Is there a reason you include the empty parentheses in the pipeline? I didn't think those were necessary.

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?