I need some help trying to come up with a real functional solution to the below problem
The boxes will have IDs which differ by exactly one character at the same position in both strings. For example, given the following box IDs:
abcde
fghij
klmno
pqrst
fguij
axcye
wvxyz
The IDs abcde
and axcye
are close, but they differ by two characters (the second and fourth). However, the IDs fghij
and fguij
differ by exactly one character, the third ( h
and u
). Those must be the correct boxes.
What letters are common between the two correct box IDs? (In the example above, this is found by removing the differing character from either ID, producing fgij
.)
So far my code is as follows (leeched from github user wittmaan)
input <- c("abcde", "fghij", "klmno", "pqrst", "fguij", "axcye", "wvxyz")
input <- strsplit(sort(input), "")
for (i in 2:length(input)) {
ident <- input[[i-1]] == input[[i]]
equal_char <- sum(ident)
if (length(input[[i]]) - 1 == equal_char) {
print(paste0(input[[i]][ident], collapse = ""))
}
}
Which works but I believe I can approach this problem in a more functional manner. I am thinking it would be possible to use map()
inside of a mutate to iterate through each value in the list.