Skip to content

Added an algorithm that finds palindromes in text #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Loading