10 Replies - 9871 Views - Last Post: 02 January 2011 - 10:13 AM

#1 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

C# Programming Challenge: Palindromes

Post icon  Posted 30 December 2010 - 09:42 AM

Well, as atraub was inspired by Dogstopper, I am inspired by atraub. Maybe "ripping off" would be a better word, but I thought this would be a good C# topic as well.

So: Palindromes. Examples: "kayak", "racecar", and "Madam, I'm Adam." All are (ignoring whitespace and punctuation) what we'll call "characterwise palindromes." Meaning that if you reversed the letters, they'd still spell out the same word/phrase.

Another type of palindrome would be a "wordwise palindrome." In this type of palindrome, reading the words backwards should make the same phrase or sentence. An example: "Fall leaves after leaves fall."

So onto the challenge. I'm not looking to start a competition here, since there's more value to code than can be measured objectively (we discussed this quite a bit in the python thread; maintainability and elegance should count, but there's no objective way to measure such things). So what I'd like to do is just have everyone who wants to participate, solve the question that will follow in your own way. Use whatever tricks you'd like, whatever libraries you want. Efficient or slow, I don't care. I'm more interested in seeing distinct solutions to the question rather than measuring the speed of one algorithm vs. another (although if you feel like measuring it against others' code for a benchmark, feel free to do so and post the results). As an example of the kind of challenge this is, take a look at this thread.

The Challenge
Write a C# program that will determine if a string (or list of strings) is a wordwise palindrome and/or a characterwise palindrome. You can ignore case, and remove any non-alphanumeric character (except spaces, for wordwise palindromes. Remove them for the characterwise test).

Again, I'm looking for diverse ways to solve this problem. Obvious or obscure, simple or complicated, I don't care, I just like seeing how other people do things (or just can do things). I'll post mine after some other people get involved in the thread.

Remember to use [spoiler] tags.

Some test strings:
string                        : (charwise, wordwise)
Madam, I'm Adam.              : (True,     False)
level madam, level!           : (True,     True)
i HAS A kitty :)/>              : (False,    False)
Fall leaves after leaves fall : (False,    True)


Is This A Good Question/Topic? 4
  • +

Replies To: C# Programming Challenge: Palindromes

#2 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,232
  • Joined: 14-September 07

Re: C# Programming Challenge: Palindromes

Posted 30 December 2010 - 10:20 AM

I shall kick off this auspicious event with a somewhat naive implementation of the wordwise check:

Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# Programming Challenge: Palindromes

Posted 30 December 2010 - 11:25 AM

Well, I'll go ahead and post mine now. I used extension methods and a little bit of LINQ to do the job.

Spoiler

Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon

Reputation: 2298
  • View blog
  • Posts: 9,535
  • Joined: 29-May 08

Re: C# Programming Challenge: Palindromes

Posted 30 December 2010 - 12:24 PM

Here's mine
Spoiler

This post has been edited by AdamSpeight2008: 30 December 2010 - 12:51 PM

Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 828
  • View blog
  • Posts: 2,236
  • Joined: 23-December 08

Re: C# Programming Challenge: Palindromes

Posted 30 December 2010 - 01:49 PM

I like your topic :) To make it more interesting, you should include line-wise palindromes!

Such as:

Quote

As I was passing near the jail
I met a man, but hurried by.
His face was ghastly, grimly pale.
He had a gun. I wondered why.
He had, A gun? I wondered... why,
His face was ghastly! grimly pale,
I met a man but hurried by,
As I was passing near the jail.


You C# guys are up to the challenge, yes?
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# Programming Challenge: Palindromes

Posted 30 December 2010 - 02:27 PM

Well, with the way I'm solving them, it's basically the same as a wordwise palindrome except split on the newlines rather than spaces. To use my existing code, I have to do a loop to clean each string individually, as my CleanString method would remove newlines. I could add that there, but...meh, I'll stick with what I got:

Spoiler

Was This Post Helpful? 0
  • +
  • -

#7 [email protected]  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 1003
  • View blog
  • Posts: 975
  • Joined: 30-September 10

Re: C# Programming Challenge: Palindromes

Posted 30 December 2010 - 02:33 PM

This is what I came up with:

Spoiler

This post has been edited by [email protected]: 30 December 2010 - 02:35 PM

Was This Post Helpful? 0
  • +
  • -

#8 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# Programming Challenge: Palindromes

Posted 30 December 2010 - 02:40 PM

Nice. Regex matching. I've never studied regex, so basically when I've needed them I've just had to find one that did what I needed on the internet and trust it. I've really been meaning to learn them, I just have so much else on my plate.
Was This Post Helpful? 0
  • +
  • -

#9 Shane Hudson  Icon User is offline

  • D.I.C Technophile
  • member icon

Reputation: 345
  • View blog
  • Posts: 1,286
  • Joined: 06-December 09

Re: C# Programming Challenge: Palindromes

Posted 31 December 2010 - 05:33 AM

Here is mine, didn't write the recursion myself though... need to learn more about it.

Spoiler

This post has been edited by Shane Hudson: 31 December 2010 - 05:34 AM

Was This Post Helpful? 0
  • +
  • -

#10 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 448
  • View blog
  • Posts: 1,504
  • Joined: 28-April 09

Re: C# Programming Challenge: Palindromes

Posted 31 December 2010 - 09:22 AM

I created my own Reverse method as well, also implemented my own Equals method just to get some practice on generics. Anywho, here's what I came up with.

Spoiler

Was This Post Helpful? 0
  • +
  • -

#11 Xeonen  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 64
  • Joined: 01-January 11

Re: C# Programming Challenge: Palindromes

Posted 02 January 2011 - 10:13 AM

I am reminded of how noobish I am yet I've learned a lot today...
Spoiler

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1