2009 104: Difference between revisions

From XPUB & Lens-Based wiki
Line 10: Line 10:
  jar, sore hats, a peon, a canal - Panama!
  jar, sore hats, a peon, a canal - Panama!


One solution:
<source lang="python">
def first(word):
return word[0]


def last(word):
return word[-1]
def middle(word):
return word[1:-1]
def is_palindrome (word):
print "is_palindrome(" + word + ")"
if word == "":
return True
if first(word) == last(word):
return is_palindrome(middle(word))
else:
return False
word = raw_input("give me a word? ")
# word = "tomato"
if is_palindrome(word):
print "hey that's a palindrome!"
else:
print "that's no palindrome!"
</source>
= Afternoon =


* http://wikipedia.org/wiki/Koch_snowflake
* http://wikipedia.org/wiki/Koch_snowflake

Revision as of 14:50, 4 November 2008

Morning

Review of the palindrome problem

steve suggests:

A man, a plan, a canoe, pasta, heros' rajahs, a coloratura, maps,
snipe, percale, macaroni, a gag, a banana bag, a tan, a tag, a banana
bag again (or a camel), a crepe, pins, Spam, a rut, a Rolo, cash, a
jar, sore hats, a peon, a canal - Panama!

One solution:

def first(word):
	return word[0]

def last(word):
	return word[-1]

def middle(word):
	return word[1:-1]

def is_palindrome (word):
	print "is_palindrome(" + word + ")"
	if word == "":
		return True
	if first(word) == last(word):
		return is_palindrome(middle(word))
	else:
		return False

word = raw_input("give me a word? ")
# word = "tomato"
if is_palindrome(word):
	print "hey that's a palindrome!"
else:
	print "that's no palindrome!"

Afternoon