Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
palindrome code

Subject: palindrome code

From: Raymond

Date: 16 Sep, 2010 18:22:07

Message: 1 of 16

I'm trying to do a standard palindrome (Madam I'm Adam). I know that I have to just toss out the spaces and punctuation, and then test whether it's true or false.
Problem is how do I toss out spaces and punctuation?

a='hello world'
s=double(a)

now how do I toss out spaces and punctuation?

Thanks!

Subject: palindrome code

From: dpb

Date: 16 Sep, 2010 18:34:03

Message: 2 of 16

Raymond wrote:
> I'm trying to do a standard palindrome (Madam I'm Adam). I know that I
> have to just toss out the spaces and punctuation, and then test whether
> it's true or false. Problem is how do I toss out spaces and punctuation?
>
> a='hello world'
> s=double(a)
>
> now how do I toss out spaces and punctuation?

start by reading results of

help strfun

and seeing if there might be something useful in the "string tests"
section, maybe?

--

Subject: palindrome code

From: Sean

Date: 16 Sep, 2010 18:52:23

Message: 3 of 16

dpb <none@non.net> wrote in message <i6to5q$sqr$1@news.eternal-september.org>...
> Raymond wrote:
> > I'm trying to do a standard palindrome (Madam I'm Adam). I know that I
> > have to just toss out the spaces and punctuation, and then test whether
> > it's true or false. Problem is how do I toss out spaces and punctuation?
> >
> > a='hello world'
> > s=double(a)
> >

No reason to convert to double. As for removing spaces and punctuation
help isspace
a(isspace(a))=[];

Here's a random way for getting rid of punctuation that just occurred to me. Since lower case letters don't equal upper case letters but lower punctuation does = upper punctuation
a = a(lower(a)~=upper(a))

a='hello world'
isequal(a,fliplr(a))

a='level'
isequal(a,fliplr(a))

Subject: palindrome code

From: Walter Roberson

Date: 16 Sep, 2010 19:00:39

Message: 4 of 16

On 10-09-16 01:22 PM, Raymond wrote:
> I'm trying to do a standard palindrome (Madam I'm Adam). I know that I
> have to just toss out the spaces and punctuation, and then test whether
> it's true or false. Problem is how do I toss out spaces and punctuation?
>
> a='hello world'
> s=double(a)
>
> now how do I toss out spaces and punctuation?

Don't toss out things, keep what is required.

a = a(ismember(a,intersect(a,['a':'z' 'A':'Z'])));

Subject: palindrome code

From: Sean

Date: 16 Sep, 2010 19:09:06

Message: 5 of 16


> Here's a random way for getting rid of punctuation that just occurred to me. Since lower case letters don't equal upper case letters but lower punctuation does = upper punctuation
> a = a(lower(a)~=upper(a))

That won't work if you want to keep numbers :(

This would account for numbers:
a = a(lower(a)~=upper(a)|ismember(int8(a),48:57))

Subject: palindrome code

From: someone

Date: 16 Sep, 2010 19:13:22

Message: 6 of 16

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i6tp17$k9f$1@fred.mathworks.com>...
> dpb <none@non.net> wrote in message <i6to5q$sqr$1@news.eternal-september.org>...
> > Raymond wrote:
> > > I'm trying to do a standard palindrome (Madam I'm Adam). I know that I
> > > have to just toss out the spaces and punctuation, and then test whether
> > > it's true or false. Problem is how do I toss out spaces and punctuation?
> > >
> > > a='hello world'
> > > s=double(a)
> > >
>
> No reason to convert to double. As for removing spaces and punctuation
> help isspace

% I'm not sure isspace will remove ALL punctuation marks.

doc isletter

% might be more useful here.


> a(isspace(a))=[];
>
> Here's a random way for getting rid of punctuation that just occurred to me. Since lower case letters don't equal upper case letters but lower punctuation does = upper punctuation
> a = a(lower(a)~=upper(a))
>
> a='hello world'
> isequal(a,fliplr(a))
>
> a='level'
> isequal(a,fliplr(a))

% Or (another way):

palTestString = 'le; ve l.';
a = palTestString(isletter(palTestString));
strcmp(a,fliplr(a))

Subject: palindrome code

From: Walter Roberson

Date: 16 Sep, 2010 19:18:49

Message: 7 of 16

On 10-09-16 02:13 PM, someone wrote:

> % I'm not sure isspace will remove ALL punctuation marks.
>
> doc isletter
>
> % might be more useful here.

I wonder... how does isletter() work with non-English letters?

Aren't there some alphabets in which the lower-case equivalent of some of the
upper-case characters require 2 characters to write? I thought I once
encountered something like that in German.

Subject: palindrome code

From: someone

Date: 16 Sep, 2010 19:21:20

Message: 8 of 16

"someone" <someone@somewhere.net> wrote in message <i6tq8i$bg1$1@fred.mathworks.com>...
> "Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i6tp17$k9f$1@fred.mathworks.com>...
> > dpb <none@non.net> wrote in message <i6to5q$sqr$1@news.eternal-september.org>...
> > > Raymond wrote:
> > > > I'm trying to do a standard palindrome (Madam I'm Adam). I know that I
> > > > have to just toss out the spaces and punctuation, and then test whether
> > > > it's true or false. Problem is how do I toss out spaces and punctuation?
> > > >
> > > > a='hello world'
> > > > s=double(a)
> > > >
> >
> > No reason to convert to double. As for removing spaces and punctuation
> > help isspace
>
> % I'm not sure isspace will remove ALL punctuation marks.
>
> doc isletter
>
> % might be more useful here.
>
>
> > a(isspace(a))=[];
> >
> > Here's a random way for getting rid of punctuation that just occurred to me. Since lower case letters don't equal upper case letters but lower punctuation does = upper punctuation
> > a = a(lower(a)~=upper(a))
> >
> > a='hello world'
> > isequal(a,fliplr(a))
> >
> > a='level'
> > isequal(a,fliplr(a))
>
> % Or (another way):
>
> palTestString = 'le; ve l.';
> a = palTestString(isletter(palTestString));
> strcmp(a,fliplr(a))

% Perhaps one more tweak is needed:

palTestString = 'Le; ve l.';
a = lower(palTestString(isletter(palTestString)));
strcmp(a,fliplr(a))

Subject: palindrome code

From: dpb

Date: 16 Sep, 2010 19:27:16

Message: 9 of 16

Walter Roberson wrote:
> On 10-09-16 01:22 PM, Raymond wrote:
>> I'm trying to do a standard palindrome (Madam I'm Adam). I know that I
>> have to just toss out the spaces and punctuation, and then test whether
>> it's true or false. Problem is how do I toss out spaces and punctuation?
>>
>> a='hello world'
>> s=double(a)
>>
>> now how do I toss out spaces and punctuation?
>
> Don't toss out things, keep what is required.
>
> a = a(ismember(a,intersect(a,['a':'z' 'A':'Z'])));

 >> s='Hello World';
 >> s(isletter(s))
ans =
HelloWorld
 >>


--

Subject: palindrome code

From: Sean

Date: 16 Sep, 2010 19:39:20

Message: 10 of 16

Walter Roberson <roberson@hushmail.com> wrote in message <i6tpkp$jv8$1@canopus.cc.umanitoba.ca>...
> On 10-09-16 01:22 PM, Raymond wrote:
> > I'm trying to do a standard palindrome (Madam I'm Adam). I know that I
> > have to just toss out the spaces and punctuation, and then test whether
> > it's true or false. Problem is how do I toss out spaces and punctuation?
> >
> > a='hello world'
> > s=double(a)
> >
> > now how do I toss out spaces and punctuation?
>
> Don't toss out things, keep what is required.
>
> a = a(ismember(a,intersect(a,['a':'z' 'A':'Z'])));

I think Walter's is the best solution since it can be easily adapted to include numbers or specific symbols etc.
So we want all letters, numbers and the pound sign:

a = a(ismember(lower(a),intersect(a,['a':'z' '0':'9' '#'])));

Subject: palindrome code

From: dpb

Date: 16 Sep, 2010 20:13:30

Message: 11 of 16

Raymond wrote:
> I'm trying to do a standard palindrome (Madam I'm Adam). ...

Depending on the rules; for the type of the above--

 >> type isPalindrome.m

function r = isPalindrome(s)
   l=lower(s(isletter(s)));
   r = all(l==fliplr(l));

 >> s='Madam I''m Adam';
 >> isPalindrome(s)
ans =
      1
 >>

--

Subject: palindrome code

From: Walter Roberson

Date: 16 Sep, 2010 20:38:10

Message: 12 of 16

On 10-09-16 02:27 PM, dpb wrote:
> Walter Roberson wrote:

>> a = a(ismember(a,intersect(a,['a':'z' 'A':'Z'])));
>
> >> s='Hello World';
> >> s(isletter(s))

ssssh!

---
"Oh Walter! You are the master of obfuscation!!"

Subject: palindrome code

From: Jos (10584)

Date: 16 Sep, 2010 21:11:20

Message: 13 of 16

Walter Roberson <roberson@hushmail.com> wrote in message <i6tqmr$lm3$1@canopus.cc.umanitoba.ca>...
> On 10-09-16 02:13 PM, someone wrote:
>
> > % I'm not sure isspace will remove ALL punctuation marks.
> >
> > doc isletter
> >
> > % might be more useful here.
>
> I wonder... how does isletter() work with non-English letters?
>
> Aren't there some alphabets in which the lower-case equivalent of some of the
> upper-case characters require 2 characters to write? I thought I once
> encountered something like that in German.

German has the lower-case ß, which turns into a two S's in upper case:
http://en.wikipedia.org/wiki/ß

Subject: palindrome code

From: Walter Roberson

Date: 16 Sep, 2010 21:42:13

Message: 14 of 16

On 10-09-16 04:11 PM, Jos (10584) wrote:
> Walter Roberson <roberson@hushmail.com> wrote in message
> <i6tqmr$lm3$1@canopus.cc.umanitoba.ca>...
>> On 10-09-16 02:13 PM, someone wrote:
>>
>> > % I'm not sure isspace will remove ALL punctuation marks.
>> >
>> > doc isletter
>> >
>> > % might be more useful here.
>>
>> I wonder... how does isletter() work with non-English letters?
>>
>> Aren't there some alphabets in which the lower-case equivalent of some
>> of the upper-case characters require 2 characters to write? I thought
>> I once encountered something like that in German.
>
> German has the lower-case ß, which turns into a two S's in upper case:
> http://en.wikipedia.org/wiki/ß

Ah, I was thinking of that letter but I wasn't sure about it.

The implication would seem to be that in at least some cases, two S's in a row
should become a single lower-case character if everything is converted to
lowercase for comparison.

In German, is it possible for two upper-case S's to appear adjacent to each
other (possibly with punctuation or spaces between) that do not represent this
letter? I seem to recall that German capitalizes proper nouns, but are there
cases where a capital letter can occur in the middle of a word or at the end
of a word? In English, capital letters can occur in the middle of a word only
for names derived from other languages (e.g., VanHooke), or for acronyms
(e.g., ATM); or for trademarks (e.g. MicroSoft); or for abbreviations (e.g.,
kHz). Alternately, is it possible in German for two lower-case s's to appear
adjacent to each other in a word and _not_ to be ß ??

The point of the above questions is of course to look for palindromes that
would break if SS is lowercased as ß, and to look for palindromes which are
only successful if SS is lowercased as ss, and to look to see whether SS to ß
is a mechanical translation or has to be guided by some more intelligence.

Several of the algorithms proposed used tolower() _after_ removing the spaces
and punctuations, but if it is possible in german to have a group of letters
(word or acronym) that can end in S followed by space/punctuation followed by
a word that starts with a single S, then in order for the lower-casing to be
accurate it would have to be done _before_ the punctuation was removed, as
otherwise after removal you might end up with an SS pair that was *not* ß in
lower-casing. Mind you, it would also be interesting to find one in German
that worked exactly the other way, dependant on the two SS's being jammed
together and matched with ß ...

Subject: palindrome code

From: Jan Simon

Date: 16 Sep, 2010 21:58:04

Message: 15 of 16

Dear Walter,

> a = a(ismember(a,intersect(a,['a':'z' 'A':'Z'])));

I cannot see the benefit of the INTERSECT here. Simpler:
  a = a(ismember(a, ['a':'z', 'A':'Z']));
(or with '0':'9' etc).

Jan

Subject: palindrome code

From: Walter Roberson

Date: 16 Sep, 2010 22:28:10

Message: 16 of 16

On 10-09-16 04:58 PM, Jan Simon wrote:
> Dear Walter,
>
>> a = a(ismember(a,intersect(a,['a':'z' 'A':'Z'])));
>
> I cannot see the benefit of the INTERSECT here.

http://www.squidoo.com/the_daily_joke_may_2008#module9441338

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us