-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.R
70 lines (63 loc) · 1.43 KB
/
functions.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#
# Helper functions
#
#
# git diff system call with arguments needed to make the output easily parsable
# the key arg is --word-diff=porcelain, which separates edits into additions (+) and deletions (-)
#
git_diff <- Vectorize(function(old, new) {
system2(
command = "git",
args = c(
"diff",
"--no-prefix",
"--no-index",
"--word-diff=porcelain",
"--unified=0",
old,
new
),
stdout = TRUE
)
})
#
# binary search implementation based on:
# https://en.wikipedia.org/wiki/Binary_search_algorithm#Algorithm
#
bin_search <- function(vec, key) {
l <- 1
r <- length(vec)
while (l <= r) {
m <- as.integer(floor((l + r) / 2))
if (vec[m] < key) {
l <- m + 1
} else if (vec[m] > key) {
r <- m - 1
} else {
return(m)
}
}
return(-1)
}
# quick sanity tests for bin_search
empty <- c()
chars <- c("a", "b", "c")
stopifnot(bin_search(empty, "b") < 0)
stopifnot(bin_search(chars, "b") == 2)
stopifnot(bin_search(chars, "d") < 0)
rm(empty, chars)
#
# given a char vector, find the number of unique chars for each one
#
num_unique_chars <- function(s) {
chars_list <- str_split(s, pattern = "")
map_int(chars_list, function(chars) {
length(unique(chars))
})
}
# sanity tests for num_unique_chars
empty <- c()
strings <- c("a", "aa", "abcdefg", "")
stopifnot(num_unique_chars(empty) == 0)
stopifnot(num_unique_chars(strings) == c(1, 1, 7, 0))
rm(empty, strings)