I am a newbie to Python and recently watching python-based programming course of renowned Google scientist, Peter Norvig on udacity. I love this man enormously for the way he looks, for the pace when he talks, for the succinctness and elegance of the code he writes and for the colorful shirts he wears as like beloved Keith Cooper. Most importantly, what he taught us blew up my mind every single time.
He later explained that this is supposed to happen because we will probably not have full knowledge of what we have in the built-in functions within a programming language or whatever package out there that might help in the real world situation. We should simply live with it and try everything ourselves.
The following example may not be any impressive or relevant to what I said about Peter just now. It is merely a reminder for myself( You may stop here).
This is an interesting little problem called Subpalindrome in his course:
# --------------
# User Instructions
#
# Write a function, longest_subpalindrome_slice(text) that takes
# a string as input and returns the i and j indices that
# correspond to the beginning and end indices of the longest
# palindrome in the string.
#
# Grading Notes:
#
# You will only be marked correct if your function runs
# efficiently enough. We will be measuring efficency by counting
# the number of times you access each string. That count must be
# below a certain threshold to be marked correct.
#
# Please do not use regular expressions to solve this quiz!
def longest_subpalindrome_slice(text):
"Return (i, j) such that text[i:j] is the longest palindrome in text."
# Your code here
text = text.lower()
longest=(0,0)
for i in xrange(1,len(text)):
longest = grow(i,text,longest)
return longest
def grow(i,text,l):
r = growth(i,text)
if r is not None and r[1]-r[0]>l[1]-l[0]:
l = r
return l
def growth(i,text):
l = i-1
r = i+1 if i<len(text) else len(text)-1
while l>=0 and r<len(text) and text[r]==text[l]:
l= l-1
r= r+1
a = (l+1,r)
l = i-1
spec =i
while l>=0 and spec<len(text) and text[l]==text[spec]:
l=l-1
spec=spec+1
b = (l+1,spec)
return a if a[1]-a[0]>b[1]-b[0] else b
def test():
L = longest_subpalindrome_slice
assert L('racecar') == (0, 7)
assert L('Racecar') == (0, 7)
assert L('RacecarX') == (0, 7)
assert L('Race carr') == (7, 9)
assert L('') == (0, 0)
assert L('something rac e car going') == (8,21)
assert L('xxxxx') == (0, 5)
assert L('Mad am I ma dam.') == (0, 15)
return 'tests pass'
print test()
I wrote three little functions to solve this problem. But the code is retarded as you can see above—— redundancy in growth function,etc.
def lonest_subpalindrome_slice(text):
if test=='':return(0,0)
def length(slice):a,b = slice;return b-a
candidates = [grow(text,start,end)
for start in range(len(text))
for end in (start, start+1)]
return max(candidates,key=length)
def grow(text,start,end):
while(start>0 and end< len(text)
and text[start-1].upper() == text[end].upper()):
start-=1;end+=1
return(start,end)
Ta-da! Look at his code——Removed my redundancy by thinking differently——he argued that he focused on the start and end point of a potential subpalindrome, which have two possibilities, for example, "aba" and "bb". So the start and end points can point both to a middle letter like 'b' in 'aba' . They can also point to to consecutive letters like the first and second 'b' in 'bb' respectively. To conclude, his method covers both of the possibilities with start and end parameters. In contrast ,mine does the same thing with redundancy because I pass only the position of a middle letter for things like "aba" and left letter for ones like "bb" (the i parameter serves as two roles).