-
Notifications
You must be signed in to change notification settings - Fork 0
/
anagram.R
47 lines (42 loc) · 943 Bytes
/
anagram.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
anagram <- function(subject, candidates) {
len <- length(candidates)
subject <- tolower(subject)
lev <- tolower(candidates)
lev <- lev[! lev %in% subject]
if (length(lev) == 0){
stop()
}
subject <- tolower(paste0(sort(unlist(strsplit(subject,split=""))),collapse=""))
lst <- c()
for (i in 1:len) {
can_iter <- tolower(paste0((sort(unlist(strsplit(lev[i],split="")))),collapse=""))
lst <- c(lst,can_iter)
}
values <- c()
for (i in 1:len){
if (subject == lst[i]){
values <- c(values,i)
}
}
if (length(values) == 0){
return(NULL)
}
fi <- length(values)
val <- c()
gal <- c()
for (i in 1:fi){
val <- values[i]
gal <- c(gal,(lev[val]))
}
final <- tolower(candidates)
result <- final %in% gal
call <- which(result == TRUE)
ji <- length(call)
cal <- c()
sol <- c()
for (i in 1:ji){
cal <- call[i]
sol <- c(sol,(candidates[cal]))
}
return(sol)
}