list index out of range

题目是(p121,Ex10.12):
Two words are a “reverse pair” if each is the reverse of the other. Write a program
that finds all the reverse pairs in the word list. 
代码:

#!/usr/bin/python
#encoding=utf-8
import datetime


def readfile (fpath):
    starttime=datetime.datetime.now()
    li=[]
    f=open(fpath)
    while True:
         line=f.readline()
         if not line:
              break
         else:
              line=line.strip()
              if not line:
                   break
              else:
                   #print line[0]
                   li.append([line])
    f.close()
    endtime=datetime.datetime.now()
    print '%d seconds'%(endtime-starttime).seconds
    return li
def binary_search(word,wordlist):
    findindex=-1
    lenlist=len(wordlist)
    mid=lenlist/2
    if wordlist[mid]==word:
         findindex=mid
    elif wordlist[mid]<word:
         findindex=binary_search(word,wordlist[mid+1:])
    else:
         findindex=binary_search(word,wordlist[0:mid])
    return findindex
def reverse_pair (fpath):
    wordlist=readfile(fpath)
    solve(wordlist)
def solve (wordlist,startindex=0):
    lenlist=len(wordlist)
    if startindex==lenlist-1:
         return None
    else:
         firstword=wordlist[startindex]
         restlist=wordlist[startindex+1:]                  
         findindex=binary_search(firstword,restlist)
         if findindex>-1:
              print '(%s,%s)'%(wordlist[startindex],wordlist[startindex+1+findindex])
              del wordlist[startindex+1+findindex]
              del wordlist[startindex]
         else:
              startindex+=1
         solve(wordlist,startindex)    
###############主程序###################


fpath=r'C:\Users\chenqinghe\Desktop\word.txt'
readfile(fpath)
reverse_pair(fpath)


代码错误提示

  File "Excise.py", line 63, in binary_search
    if wordlist[mid]==word:
IndexError: list index out of range
输出完成 (耗时 0 秒) - 正常终止
原因为:
当lenlist=1且wordlist[0]!=word时,会进行findindex=binary_search(word,wordlist[0:0])的迭代
在这轮迭代时,wordlist=[],所以wordlist[0],而mid=0,所以worlist[mid]会越界。
OK,这个问题解决,但是调试发现,又出现了一个新的问题啊!
错误提示如下 :
RuntimeError: maximum recursion depth exceeded in cmp
迭代深度超过了?不合理 啊!
不过最后还真让我发现了
我的代码是有问题,但是这个方法,根本不能迭代的,因为迭代有1000上限,所以很容易报错。
而且二分法有个细节,当a betwwen arr[0] and arr[1]的时候,会陷入死循环。
最终代码为:
#!/usr/bin/python
#encoding=utf-8
import datetime

def readfile (fpath):
    starttime=datetime.datetime.now()
    li=[]
    f=open(fpath)
    while True:
         line=f.readline()
         if not line:
              break
         else:
              line=line.strip()
              if not line:
                   break
              else:
                   #print line[0]
                   li.append(line)
    f.close()
    endtime=datetime.datetime.now()
    print '%d seconds'%(endtime-starttime).seconds
    return li
def binary_search(word,wordlist):
    findindex=-1
    lenlist=len(wordlist)
    if lenlist==0:
         return -1  
    if lenlist==2:
         if word not in wordlist:
              #print word,"word list;",wordlist
              return -1       
    mid=(lenlist-1)/2
    if wordlist[mid]==word:
         findindex=mid
    elif wordlist[mid]<word:
         if mid+1<lenlist:         
              findindex=binary_search(word,wordlist[mid+1:])
              if findindex!=-1:
                   findindex+=mid+1        
    else:      
         #print mid,word,wordlist[0],len(wordlist)
         findindex=binary_search(word,wordlist[0:mid])
    return findindex
def reverse_pair (fpath):
    wordlist=readfile(fpath)
    solve(wordlist)
def solve (wordlist):
    
    startindex=0
    while True:
         if startindex==len(wordlist)-1:
              return None
         else:
              firstword=wordlist[startindex][::-1]
             
              
              
              
              #print firstword,temp
              restlist=wordlist[startindex+1:]                  
              findindex=binary_search(firstword,restlist)
              if findindex>-1:
                   print '(%s,%s)'%(wordlist[startindex],wordlist[startindex+1+findindex])
                   del wordlist[startindex+1+findindex]
                   del wordlist[startindex]
              else:
                   startindex+=1
        
###############主程序###################

fpath=r'C:\Users\chenqinghe\Desktop\word.txt'
readfile(fpath)
reverse_pair(fpath)
还是小结一下吧。

1.像这种二分法必须要考虑到找不到的情况也就是n betwwen a[0] and a[1]这种情况。
2.像python这种语言的二分法,在已经进行了n次迭代,要进行n+1次迭代的时候,必须先考虑mid是否越界。否则就是出现 当len=0,mid=(len-1)/2=-1/2=0,从而出现越界的情况。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值