Python Function Challenge

Board Discussion Number 3

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

45 Replies - 10223 Views - Last Post: 11 January 2011 - 06:40 PM Rate Topic: ****- 2 Votes

#1 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Python Function Challenge

Post icon  Posted 23 December 2010 - 08:36 AM

I must admit, Dogstopper's Java Function Challenge inspired me. It's time for the Python Function challenge, Project Palindrome!

Is everyone here familiar with palindromes? If not, check em out on wikipedia. One of my favorite palindromes is, "Madam, I'm Adam." Madam I'm Adam, when looking at only the characters, is a palindrome. It is the same set of chars forward and backwards when we ignore punctuation and formatting (madamimadam).

So here's my challenge. Write a program than can correctly recognize wordwise and characterwise palindromes and be able to correctly report what we're looking at. I will not reveal which specific palindromes I'll be using for the test until all entries have been submitted. Your program should return a tuple with 2 values. The first value will be whether it's a characterwise palindrome, and the second will be if it's a wordwise palindrome. For example:

isPalindrome("i HAS A kitty!") should return (False, False)

isPalindrome("Madam, I'm Adam") should return (True,False)

isPalindrome("Fall leaves after leaves fall") should return (False, True)

isPalindrome("Level, madam, level!") should return (True, True)


The deadline is January 10th. To keep the test fair, all submissions must be Python3 compliant... which shouldn't be a problem. They can be in any version of Python, I'll translate them to 3 as needed. Please be sure to name your function isPalindrome for ease of testing.

I'll update soon to show you the specific function I'll use for testing! Good luck!

Please post your code in your post.


Update:
I'm sure you'll want an easy way to test your functions. Here's a sample function you can use to test your palindrome function. To keep things fair, this will NOT be the one I use for the final test. It's just something you can use to test yours.


import time
import palindrome #your palindrome.py file.

def main(palFunction, sampleSize=10000):
	def testPalindrome():
		sampleData = [["Madam, I'm Adam.", (True,False)],
			["level madam, level!", (True, True)],                  
			["i HAS A kitty :)/>", (False,False)],
			["Fall leaves after leaves fall",(False,True)]]
		for eachEntry in sampleData:
			results = palFunction(eachEntry[0])
			if not results==eachEntry[1]:
				return (eachEntry[0],eachEntry[1],results)
		return None
		
	results = testPalindrome()
	if results:
		print("This entry failed to correctly identify:\n\"%s\" as: %s" % (results[0],results[1]))
		print("Function returns: %s" % (results[2],))
	else:
		start = time.time()
		for i in range(sampleSize):
			testPalindrome()
		elapsed = time.time() - start
		print("Elapsed time: %f\nAverage time: %f\n" % (elapsed, elapsed/sampleSize))

        
if __name__=="__main__":
    main(palindrome.isPalindrome)




EDIT 2:
Don't worry about linewise palindromes, like:

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.

This post has been edited by atraub: 23 December 2010 - 10:26 PM
Reason for edit:: code cleanup


Is This A Good Question/Topic? 4
  • +

Replies To: Python Function Challenge

#2 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Python Function Challenge

Posted 23 December 2010 - 09:19 AM

You made it py3 just to spite us didn't you? :lol:
Was This Post Helpful? 0
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Python Function Challenge

Posted 23 December 2010 - 09:26 AM

Well, there are speed differences between 2 and 3. I do want to promote 3, so it seems like the fair way to test it while simultaneously pushing my own agenda :walkman: I only worry that time.time() might not be a small enough unit of measure for this test.

This post has been edited by atraub: 23 December 2010 - 09:40 AM

Was This Post Helpful? 0
  • +
  • -

#4 Steven Smith  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 45
  • Joined: 17-March 08

Re: Python Function Challenge

Posted 23 December 2010 - 10:06 AM

Can you specify what sort of entry validation and filtering might be required? Your example shows commas, apostrophes, exclamations, smilies that must be filtered. Could there be misformatting like double spacing?

I would typically add all this by default, but if the goal is 100% speed related...
Was This Post Helpful? 0
  • +
  • -

#5 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: Python Function Challenge

Posted 23 December 2010 - 10:31 AM

For palindromic purposes (HAH!), only concern yourself with alphanumeric values.

"I am       (^___^) +_+       MAI!!!!!!!"


would be considered a characterwise palindrome despite all the extra junk. 0-9 a-z are all that matter.

This post has been edited by atraub: 23 December 2010 - 10:33 AM

Was This Post Helpful? 0
  • +
  • -

#6 Seta00  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 84
  • Joined: 22-September 10

Re: Python Function Challenge

Posted 23 December 2010 - 10:36 AM

I would do it, but I'm too lazy to download Python 3 and learn the differences :P
Was This Post Helpful? 0
  • +
  • -

#7 Sergio Tapia  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1253
  • View blog
  • Posts: 4,168
  • Joined: 27-January 10

Re: Python Function Challenge

Posted 23 December 2010 - 11:07 AM

Ditto.
Was This Post Helpful? 0
  • +
  • -

#8 soldner  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 07-December 09

Re: Python Function Challenge

Posted 23 December 2010 - 11:12 AM

Well I'm learning with 2.7 and I think 3.1's code also can be run in 2.7, so I'm giving it a whirl.
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Python Function Challenge

Posted 23 December 2010 - 12:07 PM

View Postatraub, on 23 December 2010 - 10:26 AM, said:

Well, there are speed differences between 2 and 3. I do want to promote 3, so it seems like the fair way to test it while simultaneously pushing my own agenda :walkman: I only worry that time.time() might not be a small enough unit of measure for this test.


I can't use Python 3 yet! It'll break my Wireless Internet in my OS. I can hack it to make it work, but it's not worth the effort. I'll be doing it in 2.7 and converting later.
Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Python Function Challenge

Posted 23 December 2010 - 12:39 PM

How is that even possible?
Was This Post Helpful? 0
  • +
  • -

#11 Curtis Rutland  Icon User is online

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


Reputation: 4490
  • View blog
  • Posts: 7,822
  • Joined: 08-June 10

Re: Python Function Challenge

Posted 23 December 2010 - 12:42 PM

Alright, here's my try:

Spoiler


Be gentle, it's my first non-"hello world" python program. I had to google for how to clean the string, but other than that, the rest I figured out from my python book.

EDIT: Figured I'd post my average time from your program: 7.5e-05. I got that +/- 1e-05 about 10 tries.

Also, I think I'm going to use this challenge in the C# forum, if you don't mind.

This post has been edited by insertAlias: 23 December 2010 - 12:50 PM

Was This Post Helpful? 2
  • +
  • -

#12 Steven Smith  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 45
  • Joined: 17-March 08

Re: Python Function Challenge

Posted 23 December 2010 - 12:42 PM

My first go at it before I start refactoring and finding better functions to keep from typecasting like mad... think this works though.

Spoiler


Edit: @Dogstopper, sorry, the OP did not request it so I didn't see it necessary.
Time on my laptop: Your function works! Average time: 0.000102999997139
Edited by Dogstopper: Can you please post in [spoiler] tags?

This post has been edited by Steven Smith: 23 December 2010 - 01:00 PM

Was This Post Helpful? 0
  • +
  • -

#13 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Python Function Challenge

Posted 23 December 2010 - 12:56 PM

View PostKYA, on 23 December 2010 - 01:39 PM, said:

How is that even possible?


The python package got conveniently renamed to python from its original pyhton3. I don't wanna mess with it.

BUT, I found an Eclipse plugin that does the execution internally. Here's my code (now with classes):
Spoiler

Was This Post Helpful? 1
  • +
  • -

#14 Seta00  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 84
  • Joined: 22-September 10

Re: Python Function Challenge

Posted 23 December 2010 - 01:07 PM

Haha, by looking at the other solutions insertAlias' solution I can see how much influenced I am by C/C++:
Spoiler


Guess I need to get more pythonic :D

This post has been edited by Seta00: 23 December 2010 - 01:10 PM

Was This Post Helpful? 1
  • +
  • -

#15 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2872
  • View blog
  • Posts: 11,031
  • Joined: 15-July 08

Re: Python Function Challenge

Posted 23 December 2010 - 01:15 PM

Here is a far more pythonic version of my last program. It uses list comprehensions to remove the non-letters and it uses string/list slicing (to reverse them):

Spoiler

This post has been edited by Dogstopper: 23 December 2010 - 01:15 PM

Was This Post Helpful? 1
  • +
  • -

  • (4 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »