-
Notifications
You must be signed in to change notification settings - Fork 0
/
Camel Case 4.r
65 lines (57 loc) · 2.03 KB
/
Camel Case 4.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
camel_case <- function(input) {
results <- character(length(input))
for (line in input) {
parts <- strsplit(line, ";")[[1]]
operation <- parts[1]
type <- parts[2]
words <- parts[3]
if (operation == "S") {
if (type == "M") {
words <- gsub("(?<=[a-z])(?=[A-Z])", " ", words, perl = TRUE)
words <- tolower(words)
words <- sub("\\(\\)$", "", words)
results[which(input == line)] <- words
} else if (type == "C"){
words <- gsub("(?<=[a-z])(?=[A-Z])", " ", words, perl = TRUE)
words <- tolower(words)
results[which(input == line)] <- words
} else {
words <- gsub("(?<=[a-z])(?=[A-Z])", " ", words, perl = TRUE)
words <- tolower(words)
results[which(input == line)] <- words
}
} else if (operation == "C") {
if (type == "M") {
words <- strsplit(tolower(words), " ")[[1]]
words <- paste0(c(substr(words[1], 1, 1), toupper(substr(words[-1], 1, 1))),
substr(words, 2, nchar(words)))
words <- paste(words, collapse = "")
words <- paste0(words, "()")
results[which(input == line)] <- words
} else if (type == "C"){
words <- strsplit(tolower(words), " ")[[1]]
words <- paste0(toupper(substr(words, 1, 1)),substr(words, 2, nchar(words)))
words <- paste(words, collapse = "")
results[which(input == line)] <- words
} else {
words <- strsplit(tolower(words), " ")[[1]]
words <- paste0(c(substr(words[1], 1, 1), toupper(substr(words[-1], 1, 1))),
substr(words, 2, nchar(words)))
words <- paste(words, collapse = "")
results[which(input == line)] <- words
}
} else {
results[which(input == line)] <- "Error"
}
}
return(noquote(results))
}
stdin <- file('stdin')
open(stdin)
fptr <- file(Sys.getenv("OUTPUT_PATH"))
open(fptr, open = "w")
s <- readLines(stdin, warn = FALSE)
result <- camel_case(s)
writeLines(result, con = fptr)
close(stdin)
close(fptr)