1

インタビューストリートの質問用に循環バッファーのコードを書きました。しかし、たまたま、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 を指しています。街頭インタビューからテスト データを取得できません。エラーを再現するために自分でテストケースを考え出そうとしています。

洞察はありますか?

4

2 に答える 2

1

ここに 1 つのバグがあります。

def remove(self,x):
        if self.data:
                if self.current>len(self.data):
                        self.current=0
                self.data.pop(self.current)

の場合self.current == len(self.data)、存在しない要素をポップしようとします。

一般的な意見として、あなたの実装は複雑すぎるため、私の本ではあまり高く評価されません (他の人はこれを別の見方をするかもしれません)。あなたの質問に対する@ 9000のコメントは、それをうまくまとめています:

複雑にしないでおく。同じ数の行で簡単にできるのに、賢くならないでください。必要なのは、ヘッド ポインター、テール ポインター、および固定サイズのリストだけです。手の込んだメタプログラミングは一切必要ありません。– @9000

于 2013-07-02T19:24:43.657 に答える
1

以下のコードでエラーを止めようとしているようですがindex out of range、チェックしている条件が間違っています。

if self.current > len(self.data):
    self.current = 0
self.data.pop(self.current)

リストのインデックスが 0 であるため、呼び出すself.data.pop(len(self.data))と、間違いなくそのエラーが発生します。あなたはおそらく次のことを意味していました:

if self.current >= len(self.data):
    self.current = 0
self.data.pop(self.current)
于 2013-07-02T19:24:58.117 に答える