|
| 1 | +findPalindrome <- function(input) { |
| 2 | + |
| 3 | + is.palindrome <- function(input) { |
| 4 | + if (is.numeric(input)) { # checks if input is numeric |
| 5 | + # convert the numeric input value to character |
| 6 | + input_con <- as.character(input) |
| 7 | + # split the string into characters |
| 8 | + input_split <- c(unlist(strsplit(input_con, split = ""))) |
| 9 | + # reverse the characters |
| 10 | + input_rev <- rev(input_split) |
| 11 | + # conditional statement to compare split string |
| 12 | + # with the reversed string |
| 13 | + if (all(input_split == input_rev) != TRUE) { |
| 14 | + return(FALSE) |
| 15 | + } else { |
| 16 | + return(TRUE) |
| 17 | + } |
| 18 | + } else if (is.character(input)) { # checks if input is character |
| 19 | + # split the string into characters |
| 20 | + input_split <- c(unlist(strsplit(tolower(input), split = ""))) |
| 21 | + # reverse the characters |
| 22 | + input_rev <- rev(input_split) |
| 23 | + # conditional statement to compare split string |
| 24 | + # with the reversed string |
| 25 | + if (all(input_split == input_rev) != TRUE) { |
| 26 | + return(FALSE) |
| 27 | + } else { |
| 28 | + return(TRUE) |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + if(is.character(input)) { |
| 34 | + # clean out punctuation |
| 35 | + input_clean <- tm::removePunctuation(input) |
| 36 | + |
| 37 | + # split the sentence into individual words |
| 38 | + input_split <- c(unlist(strsplit(input_clean, split = " "))) |
| 39 | + |
| 40 | + # loop every word in the text through the is.palindrome() function |
| 41 | + # and return their boolean values |
| 42 | + check_palin <- c() |
| 43 | + for (i in input_split) { |
| 44 | + result <- is.palindrome(i) |
| 45 | + check_palin <- append(check_palin, result) |
| 46 | + } |
| 47 | + |
| 48 | + # check and return index positions of TRUE |
| 49 | + indx <- which(check_palin == TRUE) |
| 50 | + |
| 51 | + # use the index positions to filter input_split |
| 52 | + palindromes <- input_split[indx] |
| 53 | + |
| 54 | + # filter out words that contain less than one character |
| 55 | + palin <- palindromes[nchar(palindromes) > 1] |
| 56 | + |
| 57 | + return(noquote(palin)) |
| 58 | + |
| 59 | + } else if(is.numeric(input)) { |
| 60 | + # convert numeric input to character |
| 61 | + num_con <- as.character(input) |
| 62 | + |
| 63 | + # clean out punctuation |
| 64 | + input_clean <- tm::removePunctuation(num_con) |
| 65 | + |
| 66 | + # split the sentence into individual words |
| 67 | + input_split <- c(unlist(strsplit(input_clean, split = " "))) |
| 68 | + |
| 69 | + # loop every word in the text through the is.palindrome() function |
| 70 | + # and return their boolean values |
| 71 | + check_palin <- c() |
| 72 | + for (i in input_split) { |
| 73 | + result <- is.palindrome(i) |
| 74 | + check_palin <- append(check_palin, result) |
| 75 | + } |
| 76 | + |
| 77 | + # check and return index positions of TRUE |
| 78 | + indx <- which(check_palin == TRUE) |
| 79 | + |
| 80 | + # use the index positions to filter input_split |
| 81 | + palindromes <- input_split[indx] |
| 82 | + |
| 83 | + # filter out numbers that contain less than one character |
| 84 | + palin <- palindromes[nchar(palindromes) > 1] |
| 85 | + |
| 86 | + return(noquote(palin)) |
| 87 | + |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +my_text <- "Bob works in a shop on a street called Pop. His work ID is 444, and his manager owns a racecar." |
| 92 | + |
| 93 | +findPalindrome(my_text) |
0 commit comments