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

katrinleinweber's solution

to Bob in the R Track

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

Bob is a lackadaisical teenager. In conversation, his responses are very limited.

Bob answers 'Sure.' if you ask him a question.

He answers 'Whoa, chill out!' if you yell at him.

He answers 'Calm down, I know what I'm doing!' if you yell a question at him.

He says 'Fine. Be that way!' if you address him without actually saying anything.

He answers 'Whatever.' to anything else.

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 'Deaf Grandma' exercise in Chris Pine's Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=06

Submitting Incomplete Solutions

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

test_bob.R

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

context("bob")

test_that("stating something", {
  input <- "Tom-ay-to, tom-aaaah-to."
  expect_equal(bob(input),
               "Whatever.")
})

test_that("shouting", {
  input <- "WATCH OUT!"
  expect_equal(bob(input),
               "Whoa, chill out!")
})

test_that("shouting gibberish", {
  input <- "FCECDFCAAB"
  expect_equal(bob(input),
               "Whoa, chill out!")
})

test_that("asking a question", {
  input <- "Does this cryogenic chamber make me look fat?"
  expect_equal(bob(input),
               "Sure.")
})

test_that("asking a numeric question", {
  input <- "You are, what, like 15?"
  expect_equal(bob(input),
               "Sure.")
})

test_that("asking gibberish", {
  input <- "fffbbcbeab?"
  expect_equal(bob(input),
               "Sure.")
})

test_that("talking forcefully", {
  input <- "Let's go make out behind the gym!"
  expect_equal(bob(input),
  "Whatever."
  )
})
  
  test_that("using acronyms in regular speech", {
  input <- "It's OK if you don't want to go to the DMV."
  expect_equal(bob(input),
  "Whatever."
  )
  })
  
  test_that("forceful question", {
  input <- "WHAT THE HELL WERE YOU THINKING?"
  expect_equal(bob(input),
  "Calm down, I know what I'm doing!"
  )
  })
  
  test_that("shouting numbers", {
  input <- "1, 2, 3 GO!"
  expect_equal(bob(input),
  "Whoa, chill out!"
  )
  })
  
  test_that("only numbers", {
  input <- "1, 2, 3"
  expect_equal(bob(input),
  "Whatever."
  )
  })
  
  test_that("question with only numbers", {
  input <- "4?"
  expect_equal(bob(input),
  "Sure."
  )
  })
  
  test_that("shouting with special characters", {
  input <- "ZOMG THE % ^  * @#$(*^ ZOMBIES ARE COMING!!11!!1!"
    expect_equal(bob(input),
                 "Whoa, chill out!")
  })

test_that("shouting with no exclamation mark", {
  input <- "I HATE YOU"
  expect_equal(bob(input),
               "Whoa, chill out!")
})

test_that("statement containing question mark", {
  input <- "Ending with ? means a question."
  expect_equal(bob(input),
               "Whatever.")
})

test_that("non-letters with question", {
  input <- ":) ?"
  expect_equal(bob(input),
               "Sure.")
})

test_that("prattling on", {
  input <- "Wait! Hang on. Are you going to be OK?"
  expect_equal(bob(input),
               "Sure.")
})

test_that("silence", {
  input <- ""
  expect_equal(bob(input),
               "Fine. Be that way!")
})

test_that("prolonged silence", {
  input <- "          "
  expect_equal(bob(input),
               "Fine. Be that way!")
})

test_that("alternate silence", {
  input <- "										"
  expect_equal(bob(input),
               "Fine. Be that way!")
})

test_that("multiple line question", {
  input <- "
  Does this cryogenic chamber make me look fat?
  no"
  expect_equal(bob(input),
               "Whatever.")
})

test_that("starting with whitespace", {
  input <- "         hmmmmmmm..."
  expect_equal(bob(input),
               "Whatever.")
})

test_that("ending with whitespace", {
  input <- "Okay if like my  spacebar  quite a bit?   "
  expect_equal(bob(input),
               "Sure.")
})

test_that("other whitespace", {
  input <- "
  
  "
  expect_equal(bob(input),
               "Fine. Be that way!")
})

test_that("non-question ending with whitespace", {
  input <- "This is a statement ending with whitespace      "
  expect_equal(bob(input),
               "Whatever.")
})

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

bob <- function(input) {
  
  # remove all white-space from both sides and multiple in between
  input <- stri_replace_all(input, regex = "^\\s$|\\s+", "")
  
  dplyr::case_when(
    
    # yelling := more upper- than lowercase OR exclamation
    stri_count_regex(input, "[A-Z]") > 
      stri_count_regex(input, "[a-z]") |
      stri_detect_regex(input, "[A-Z]\\!") ~
      paste("Whoa, chill out!"),
    
    # question
    stri_endswith(input, fixed = "?") ~ 
      paste("Sure."),
    
    # not saying anything
    input == "" | input == " " |
      stri_count_regex(input, "[[:punct:]]") > 
      stri_count_regex(input, "[[:alnum:]]") ~ 
      paste("Fine. Be that way!"),
    
    # everything else
    TRUE ~ paste("Whatever.")
  )
}

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?