ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# katrinleinweber's solution

## to Scrabble Score in the R Track

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

Given a word, compute the scrabble score for that word.

## Letter Values

You'll need these:

``````Letter                           Value
A, E, I, O, U, L, N, R, S, T       1
D, G                               2
B, C, M, P                         3
F, H, V, W, Y                      4
K                                  5
J, X                               8
Q, Z                               10
``````

## Examples

"cabbage" should be scored as worth 14 points:

• 3 points for C
• 1 point for A, twice
• 3 points for B, twice
• 2 points for G
• 1 point for E

And to total:

• `3 + 2*1 + 2*3 + 2 + 1`
• = `3 + 2 + 6 + 3`
• = `5 + 9`
• = 14

## Extensions

• You can play a double or a triple letter.
• You can play a double or a triple word.

## 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_scrabble-score.R

``````source("./scrabble-score.R")
library(testthat)

context("scrabble score")

test_that("lowercase letter", {
input <- "a"
expect_equal(scrabble_score(input), 1)
})

test_that("uppercase letter", {
input <- "A"
expect_equal(scrabble_score(input), 1)
})

test_that("valuable letter", {
input <- "f"
expect_equal(scrabble_score(input), 4)
})

test_that("short word", {
input <- "at"
expect_equal(scrabble_score(input), 2)
})

test_that("short, valuable word", {
input <- "zoo"
expect_equal(scrabble_score(input), 12)
})

test_that("medium word", {
input <- "street"
expect_equal(scrabble_score(input), 6)
})

test_that("medium, valuable word", {
input <- "quirky"
expect_equal(scrabble_score(input), 22)
})

test_that("long, mixed-case word", {
input <- "OxyphenButazone"
expect_equal(scrabble_score(input), 41)
})

test_that("english-like word", {
input <- "pinata"
expect_equal(scrabble_score(input), 8)
})

test_that("empty input", {
input <- ""
expect_equal(scrabble_score(input), 0)
})

test_that("entire alphabet available", {
input <- "abcdefghijklmnopqrstuvwxyz"
expect_equal(scrabble_score(input), 87)
})

message("All tests passed for exercise: scrabble-score")``````
``````library(magrittr)

scrabble_score <- function(input){

# match of letter to score
score <- function(letter) {
dplyr::case_when(
letter %in% c("A", "E", "I", "O", "U", "L", "N", "R", "S", "T") ~ 1,
letter %in% c("D", "G") ~ 2,
letter %in% c("B", "C", "M", "P") ~ 3,
letter %in% c("F", "H", "V", "W", "Y") ~ 4,
letter %in% c("K") ~ 5,
letter %in% c("J", "X") ~ 8,
letter %in% c("Q", "Z") ~ 10
)}

# split input into letters
# sum scores ups
input %>%
toupper() %>%
strsplit("") %>%
purrr::map(.f = score) %>%
unlist() %>%
sum()
}``````