-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHighest_Scoring_Word.R
35 lines (26 loc) · 959 Bytes
/
Highest_Scoring_Word.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Given a string of words, you need to find the highest scoring word.
#
# Each letter of a word scores points according to its position in the
# alphabet: a = 1, b = 2, c = 3 etc.
#
# You need to return the highest scoring word as a string.
#
# If two words score the same, return the word that appears earliest in the
# original string.
#
# All letters will be lowercase and all inputs will be valid.
# Solution: ----
high <- function(x) {
lookup_num <- c(factor(letters))
names(lookup_num) <- c(letters)
score <- sapply(unlist(strsplit(x, " ")), function(x) sum(lookup_num[unlist(strsplit(x, ""))]))
rk <- rank(score, ties.method = "last")
names(rk[match(max(rk), rk)])
}
# Sample Tests: ----
library(testthat)
test_that("Sample Tests", {
expect_equal(high('man i need a taxi up to ubud'), 'taxi')
expect_equal(high('what time are we climbing up the volcano'), 'volcano')
expect_equal(high('take me to semynak'), 'semynak')
})