0

I have a project to create a circular array class, and the language I will be using is python. I am new to classes in python, but after reading through some webpages and chapters in books I think I have an understanding of how they work. However I need help, so I figured I would come to the wonderful teachers here at SO :)

Our class must be able to implement several operations; insert at front, insert at back, insert at index, remove from front, remove from back, remove from index.

I have started coding but am running into some problems, and I am not 100% sure if my syntax is even right.

Here is what I have so far:

class circular:

    def __init__(self):
        self.store = []
        self.capacity = len(self.store)
        self.size = 0
        self.startIndex = 0
        self.endIndex = 0

    def decrementIndex(self):
        index = index - 1
        if index < 0:
            self.store = self.store + self.capacity

    def incrementIndex(self):
        index = index + 1
        if index == self.capacity:
            index = index - self.capacity

    def addToBack(self, value):
        self.store[self.endIndex] = value
        self.endIndex = incrementIndex(self.endIndex)
        self.size += 1

    def addToFront(self, value):
        if self.size == 0:
            addToBack(self, value)
        else:
            self.startIndex = decrementIndex(self.startIndex)
            self.store[self.startIndex] = value
            self.size += 1

I stopped there to start testing some of the functions, primarily t he addTofront and addToback. Upon testing them in IDLE using c = circular() and c.addToBack(2) I get an index error...and I'm not sure why. That isn't the only problem, it's just where I have gotten stuck and need help moving forward.

I am posting here because I need help and want to learn, not because I am lazy and haven't tried researching my problem. Thanks already SO!

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
roboman
  • 13
  • 6
  • `a=[]; a[1]=3` will create the same error. you cannot write to a non-existing index. what are you trying to do, is this a circular (fixed size) *buffer*? – Karoly Horvath Sep 16 '11 at 22:06
  • @yi_H yes this is a project to write a fixed circular buffer that performs the above operations. I have fixed the index problem and am now getting not defined errors for each function that I call, so something is wrong with my syntax i'm guessing. – roboman Sep 16 '11 at 22:16
  • Just a little tip: when you add or subtract one to index, you haven't to verify if index is (respectively) equal to capacity or < 0: do the sum or subtraction in %self.capacity and here you go ;-) – DonCallisto Sep 17 '11 at 07:44

1 Answers1

1

In __init__ you set

self.store = []

In addToBack you do

self.store[self.endIndex] = value

If this is the first operation on the circular array, and you pass 2 as the value, then that turns into

[][0] = 2

The problem should be obvious -- an empty list has no index of 0, it has no indexes at all.

You need to add the item to the list in a different way.

I'm not going to tell you exactly how as it's part of your homework to figure that out.

agf
  • 171,228
  • 44
  • 289
  • 238
  • Yeah I knew what the problem was. I wrote all of this code based on a reading that we had that explains circular arrays, and our teacher pseudo-coded how each function worked. Should I be appending? or would it be better to set self.store = [None] or would that be the same thing? Also upon testing c.addToFront(2) I get an error saying addToBack is not defined...why would that be? Thanks for the response. – roboman Sep 16 '11 at 22:12
  • @roboman - For your first question: If you set `self.store = [None]` then you'd just have the problem when the index was 1 instead of 0. You need to grow the list -- `append` sounds like a good way for `addToBack` to do that. For `addToFront`, it's looking for `addToBack` as a local, enclosing, global, or built-in variable -- it's none of those, it's an instance method, so you need to reference it on the instance, just like `size`, `startindex`, etc. – agf Sep 16 '11 at 22:15
  • Okay. The only problem with growing the list is that this has to be a fixed-size buffer, one that has a set capacity. I feel like growing the list would be making it dynamic, which is my next project after this one. Am i right? – roboman Sep 16 '11 at 22:19
  • Your code doesn't show it as a fixed size buffer -- You never set the size to anything other than zero. Did you mean to take a size in the constructor? If you do know the real size in `__init__`, you could certainly initialize the `list` in some way similar to `self.store = [None] * size`. You're not asking the same question any more as what is in your question, so if you need more help please accept an answer and ask a new, clear, specific question, don't keep expanding your question in the comments. – agf Sep 16 '11 at 22:24