インタビューストリートの質問用に循環バッファーのコードを書きました。しかし、たまたま、2 つのテストケースが成功し、他のテストケースは失敗しています。故障の原因:インデックスアウトf範囲。その後、失敗を再現するためにいくつかのテストケースを試しました。残念ながら、それらのどれもエラーを再現しません。これがコードです。
サイズ N の循環バッファーを実装します。呼び出し元がバッファーの内容を追加、削除、および一覧表示できるようにします。バッファーを実装して、各操作のパフォーマンスを最大化します。
"A" n - 次の n 行をバッファに追加します。バッファがいっぱいになると、古いエントリが置き換えられます。
"R" n - バッファの最初の n 個の要素を削除します。これらの n 個の要素は、現在の要素の中で最も早く追加されたものです。
"L" - buffer の要素を挿入時間順にリストします。
"Q" - 終了します。
class circbuffer():
#initialization
def __init__(self,size):
self.maximum=size
self.data=[]
self.current=0
#appending when the buffer is not full
def append(self,x):
if len(self.data)==self.maximum:
self.current=0
self.data[self.current]=x
self.current=(self.current+1)%self.maximum
self.__class__=bufferfull
else:
self.data.append(x)
def remove(self,x):
if self.data:
self.data.pop(0)
def cget(self):
return self.data
class bufferfull:
def append(self,x):
if len(self.data)<self.maximum:
self.data.insert(self.current, x)
else:
self.data[self.current]=x
self.current=(self.current+1)%self.maximum
def remove(self,x):
if self.data:
if self.current>len(self.data):
self.current=0
self.data.pop(self.current)
def cget(self):
return self.data[self.current:]+self.data[:self.current]
n=input()
buf=circbuffer(n)
outputbuf=[]
while True:
com=raw_input().split(' ')
if com[0]=='A':
n=int(com[1])
cominput=[]
for i in xrange(n):
cominput.append(raw_input())
for j in cominput:
buf.append(j)
elif com[0]=="R":
n=int(com[1])
for i in range(n):
buf.remove(i)
elif com[0]=="L":
for i in buf.cget():
outputbuf.append(i)
elif com[0]=="Q":
break
for i in outputbuf:
print i
エラーはself.data.pop(self.current)
、クラス bufferfull を指しています。街頭インタビューからテスト データを取得できません。エラーを再現するために自分でテストケースを考え出そうとしています。
洞察はありますか?