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

katrinleinweber's solution

to Anagram in the R Track

Published at Jul 13 2018 · 0 comments
Instructions
Test suite
Solution

Given a word and a list of possible anagrams, select the correct sublist.

Given "listen" and a list of candidates like "enlists" "google" "inlets" "banana" the program should return a list containing "inlets".

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

Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

Submitting Incomplete Solutions

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

test_anagram.R

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

context("anagram")

test_that("no matches", {
  subject <- "diaper"
  candidates <- c("hello", "world", "zombies", "pants")
  expect_equal(anagram(subject, candidates),
               c())
})

test_that("detects simple anagram", {
  subject <- "ant"
  candidates <- c("tan", "stand", "at")
  expect_equal(anagram(subject, candidates),
               c("tan"))
})

test_that("does not detect false positives", {
  subject <- "galea"
  candidates <- c("eagle")
  expect_equal(anagram(subject, candidates),
               c())
})

test_that("detects multiple anagrams", {
  subject <- "master"
  candidates <- c("stream", "pigeon", "maters")
  expect_equal(anagram(subject, candidates),
               c("stream", "maters"))
})

test_that("does not detect anagram subsets", {
  subject <- "good"
  candidates <- c("dog", "goody")
  expect_equal(anagram(subject, candidates),
               c())
})

test_that("detects anagram", {
  subject <- "listen"
  candidates <- c("enlists", "google", "inlets", "banana")
  expect_equal(anagram(subject, candidates),
               c("inlets"))
})

test_that("detects multiple anagrams", {
  subject <- "allergy"
  candidates <-
    c("gallery", "ballerina", "regally", "clergy", "largely", "leading")
  expect_equal(anagram(subject, candidates),
               c("gallery", "regally", "largely"))
})

test_that("does not detect indentical words", {
  subject <- "corn"
  candidates <- c("corn", "dark", "Corn", "rank", "CORN", "cron", "park")
  expect_equal(anagram(subject, candidates),
               c("cron"))
})

test_that("does not detect non-anagrams with identical checksum", {
  subject <- "mass"
  candidates <- c("last")
  expect_equal(anagram(subject, candidates),
               c())
})

test_that("detects anagrams case-insensitively", {
  subject <- "Orchestra"
  candidates <- c("cashregister", "Carthorse", "radishes")
  expect_equal(anagram(subject, candidates),
               c("Carthorse"))
})

test_that("detects anagrams using case-insensitive subject", {
  subject <- "Orchestra"
  candidates <- c("cashregister", "carthorse", "radishes")
  expect_equal(anagram(subject, candidates),
               c("carthorse"))
})

test_that("detects anagrams using case-insensitve possible matches", {
  subject <- "orchestra"
  candidates <- c("cashregister", "Carthorse", "radishes")
  expect_equal(anagram(subject, candidates),
               c("Carthorse"))
})

test_that("does not detect a word as its own anagram", {
  subject <- "banana"
  candidates <- c("Banana")
  expect_equal(anagram(subject, candidates),
               c())
})

test_that("does not detect a anagram if the original word is repeated", {
  subject <- "go"
  candidates <- c("go Go GO")
  expect_equal(anagram(subject, candidates),
               c())
})

test_that("anagrams must use all letters exactly once", {
  subject <- "tapper"
  candidates <- c("patter")
  expect_equal(anagram(subject, candidates),
               c())
})

test_that("eliminates anagrams with the same checksum", {
  subject <- "mass"
  candidates <- c("last")
  expect_equal(anagram(subject, candidates),
               c())
})

test_that("capital word is not own anagram", {
  subject <- "BANANA"
  candidates <- c("Banana")
  expect_equal(anagram(subject, candidates),
               c())
})

test_that("anagrams must use all letters exactly once", {
  subject <- "patter"
  candidates <- c("tapper")
  expect_equal(anagram(subject, candidates),
               c())
})

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

options(encoding = "UTF-8")

anagram <- function(subject, candidates) {

  # buffer
  anagrams <- c()
  
  # catch false positives 
  if (length(candidates) <= 1)
    return(anagrams)
  
  # prepare comparison
  candidates_lengths <- stri_length(candidates)
  length_subject <- stri_length(subject)
  
  for (x in 1:length(candidates)) {
    
    if (candidates_lengths[x] == length_subject) {
      
      candidates[x] %>%
        stri_extract_all_regex(pattern = paste0("[", tolower(subject), toupper(subject), "]")) %>%
        unlist() %>% 
        paste(collapse = "") ->
        anagram
      
      # discard anagrams that are not different from subject
      # not necessary to OR `identical(anagram, subject)` here
      if (identical(tolower(anagram), tolower(subject)))  
          anagram <- c()
         
      # store true anagrams in buffer 
      if (identical(stri_length(anagram), length_subject))
        anagrams <- append(anagrams, anagram)
    }
  }
  
  return(anagrams)
}

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?