Array? Please help.

Scott David Daniels scott.daniels at acm.org
Sun May 28 19:52:25 CEST 2006


Dr. Pastor wrote:
> Scott David Daniels wrote:
>> Dr. Pastor wrote:
>>> I need a row of 127 bytes that I will use as a
>>> circular buffer. Into the bytes (at unspecified times)
>>> a mark (0<mark<128) will be written, one after the other.
>>> After some time the "buffer" will contain the last 127 marks.
>> Sounds a lot like homework.
> No it is not home work.

OK, taking you at your word, here's one way:

     class Circular(object):
         def __init__(self):
             self.data = array.array('b', [0] * 127)
             self.point = len(self.data) - 1

         def mark(self, value):
             self.point += 1
             if self.point >= len(self.data):
                 self.point = 0
             self.data[self.point] = value

         def recent(self):
             result = self.data[self.point :] + self.data[: self.point]
             for n, v in enumerate(result):
                 if v:
                     return result[n :]
             return result[: 0] # an empty array
-- 
--Scott David Daniels
scott.daniels at acm.org



More information about the Python-list mailing list