Skip to content

Commit

Permalink
Add an algorithm that finds palindromes in text (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ifeanyi55 authored Oct 23, 2024
1 parent 898a0a1 commit 61eed38
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions text_manipulation/findPalindrome.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
findPalindrome <- function(input) {

is.palindrome <- function(input) {
if (is.numeric(input)) { # checks if input is numeric
# convert the numeric input value to character
input_con <- as.character(input)
# split the string into characters
input_split <- c(unlist(strsplit(input_con, split = "")))
# reverse the characters
input_rev <- rev(input_split)
# conditional statement to compare split string
# with the reversed string
if (all(input_split == input_rev) != TRUE) {
return(FALSE)
} else {
return(TRUE)
}
} else if (is.character(input)) { # checks if input is character
# split the string into characters
input_split <- c(unlist(strsplit(tolower(input), split = "")))
# reverse the characters
input_rev <- rev(input_split)
# conditional statement to compare split string
# with the reversed string
if (all(input_split == input_rev) != TRUE) {
return(FALSE)
} else {
return(TRUE)
}
}
}

if(is.character(input)) {
# clean out punctuation
input_clean <- tm::removePunctuation(input)

# split the sentence into individual words
input_split <- c(unlist(strsplit(input_clean, split = " ")))

# loop every word in the text through the is.palindrome() function
# and return their boolean values
check_palin <- c()
for (i in input_split) {
result <- is.palindrome(i)
check_palin <- append(check_palin, result)
}

# check and return index positions of TRUE
indx <- which(check_palin == TRUE)

# use the index positions to filter input_split
palindromes <- input_split[indx]

# filter out words that contain less than one character
palin <- palindromes[nchar(palindromes) > 1]

return(noquote(palin))

} else if(is.numeric(input)) {
# convert numeric input to character
num_con <- as.character(input)

# clean out punctuation
input_clean <- tm::removePunctuation(num_con)

# split the sentence into individual words
input_split <- c(unlist(strsplit(input_clean, split = " ")))

# loop every word in the text through the is.palindrome() function
# and return their boolean values
check_palin <- c()
for (i in input_split) {
result <- is.palindrome(i)
check_palin <- append(check_palin, result)
}

# check and return index positions of TRUE
indx <- which(check_palin == TRUE)

# use the index positions to filter input_split
palindromes <- input_split[indx]

# filter out numbers that contain less than one character
palin <- palindromes[nchar(palindromes) > 1]

return(noquote(palin))

}
}

my_text <- "Bob works in a shop on a street called Pop. His work ID is 444, and his manager owns a racecar."

findPalindrome(my_text)

0 comments on commit 61eed38

Please sign in to comment.