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!

3
  • 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? Commented Sep 16, 2011 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. Commented Sep 16, 2011 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 ;-) Commented Sep 17, 2011 at 7:44

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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 - 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.
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?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.