Palindrome detection: Difference between revisions

Content added Content deleted
(add BQN)
m (→‎{{header|R}}: Syntax highlighting.)
Line 4,469: Line 4,469:
R will assume an infinite recursion if a recursion nests deeper than 5,000.
R will assume an infinite recursion if a recursion nests deeper than 5,000.
Options may be set in the environment to increase this to 500,000.
Options may be set in the environment to increase this to 500,000.
<lang R>palindro <- function(p) {
<lang rsplus>palindro <- function(p) {
if ( nchar(p) == 1 ) {
if ( nchar(p) == 1 ) {
return(TRUE)
return(TRUE)
Line 4,484: Line 4,484:


'''Iterative'''
'''Iterative'''
<lang R>palindroi <- function(p) {
<lang rsplus>palindroi <- function(p) {
for(i in 1:floor(nchar(p)/2) ) {
for(i in 1:floor(nchar(p)/2) ) {
r <- nchar(p) - i + 1
r <- nchar(p) - i + 1
Line 4,498: Line 4,498:
Note that this method incorrectly regards an empty string as not a palindrome.
Note that this method incorrectly regards an empty string as not a palindrome.
Please leave this bug in the code, and take a look a the [[Testing_a_Function]] page.
Please leave this bug in the code, and take a look a the [[Testing_a_Function]] page.
<lang R>revstring <- function(stringtorev) {
<lang rsplus>revstring <- function(stringtorev) {
return(
return(
paste(
paste(
Line 4,512: Line 4,512:


Unicode is supported, but this ignores the "inexact palindromes" extra credit requirement because, without some sort of regex, supporting Unicode while stripping punctuation and white space is hard in R.
Unicode is supported, but this ignores the "inexact palindromes" extra credit requirement because, without some sort of regex, supporting Unicode while stripping punctuation and white space is hard in R.
<lang R>is.Palindrome<-function(string)
<lang rsplus>is.Palindrome<-function(string)
{
{
characters<-unlist(strsplit(string,""))
characters<-unlist(strsplit(string,""))