2011-01-19 39 views
2

我是Python的新手,目前正在閱讀「潛入Python」中有關字符串操作的章節。Python:字符串操作的聰明方法

我想知道什麼是一些最好(或最聰明/創意)的方式來做到以下幾點:

1)從這個字符串中提取出來:「stackoverflow.com/questions/ask」字「的問題「。我做了string.split(/)[0] - 但那不是很聰明。

2)找到在一個給定的數字或字符串

3最長迴文)與給定的字開始(即「貓」) - 找到所有可能的方式從去另一個三字母詞( 「狗」),一次更換一個字母,使得每個字母的改變形成一個新的有效的單詞。

對於example--貓,嬰兒牀,圓點,狗

+3

這是三個獨立的問題,有沒有什麼共同點。一般來說,你似乎沒有問題。 – SilentGhost 2011-01-19 16:25:58

+8

問題是他對語言很陌生,已經在尋找'最瘋狂的方式':-D ..順便說一句,爲什麼string.split('/')[1]不聰明? – 2011-01-19 16:28:11

+0

@Parsetongue你有什麼? – OscarRyz 2011-01-19 16:40:56

回答

2

隨着個人的運動,這裏是你的,(希望)有很好的註釋代碼一些提示。

#!/usr/bin/env python2 

# Let's take this string: 
a = "palindnilddafa" 
# I surround with a try/catch block, explanation following 
try: 
    # In this loop I go from length of a minus 1 to 0. 
    # range can take 3 params: start, end, increment 
    # This way I start from the thow longest subsring, 
    # the one without the first char and without the last 
    # and go on this way 
    for i in range(len(a)-1, 0, -1): 
    # In this loop I want to know how many 
    # Palidnrome of i length I can do, that 
    # is len(a) - i, and I take all 
    # I start from the end to find the largest first 
    for j in range(len(a) - i): 
     # this is a little triky. 
     # string[start:end] is the slice operator 
     # as string are like arrays (but unmutable). 
     # So I take from j to j+i, all the offsets 
     # The result of "foo"[1:3] is "oo", to be clear. 
     # with string[::-1] you take all elements but in the 
     # reverse order 
     # The check string1 in string2 checks if string1 is a 
     # substring of string2 
     if a[j:j+i][::-1] in a: 
     # If it is I cannot break, 'couse I'll go on on the first 
     # cycle, so I rise an exception passing as argument the substring 
     # found 
     raise Exception(a[j:j+i][::-1]) 

# And then I catch the exception, carrying the message 
# Which is the palindrome, and I print some info 
except Exception as e: 
    # You can pass many things comma-separated to print (this is python2!) 
    print e, "is the longest palindrome of", a 
    # Or you can use printf formatting style 
    print "It's %d long and start from %d" % (len(str(e)), a.index(str(e))) 

經過討論,我很抱歉,如果它ot。我已經編寫了另一個迴文搜索器的實現,如果sberry2A可以,我想知道一些基準測試的結果!

請注意,關於指針和困難的「+1 -1」問題有很多錯誤(我猜),但這個想法很清楚。從中間開始,然後展開,直到可以。

下面的代碼:

#!/usr/bin/env python2 


def check(s, i): 
    mid = s[i] 
    j = 1 
    try: 
    while s[i-j] == s[i+j]: 
     j += 1 
    except: 
    pass 
    return s[i-j+1:i+j] 

def do_all(a): 
    pals = [] 
    mlen = 0 
    for i in range(len(a)/2): 
    #print "check for", i 
    left = check(a, len(a)/2 + i) 
    mlen = max(mlen, len(left)) 
    pals.append(left) 

    right = check(a, len(a)/2 - i) 
    mlen = max(mlen, len(right)) 
    pals.append(right) 

    if mlen > max(2, i*2-1): 
     return left if len(left) > len(right) else right 

string = "palindnilddafa" 

print do_all(string) 
0

3號:

如果字符串是s

max((j-i,s[i:j]) for i in range(len(s)-1) for j in range(i+2,len(s)+1) if s[i:j]==s[j-1:i-1:-1])[1]

將返回答案。