This is my first program in Python: a stack implementation.
class Stack:
items =[];
index =-1;
def isEmpty(self):
return self.index >=0;
def push(self,item):
self.index +=1;
print(self.index);
self.items.insert(self.index,item);
def pop(self):
if self.index<0:
raise Exception(" no items in list");
else:
del self.items[self.index];
self.index -=1;
def peek(self):
print(self.index);
if self.index >-1:
return self.items[self.index];
stackobj = Stack();
stackobj.push("solomon");
stackobj.push("helloworld")
stackobj.pop();
print(stackobj.peek())
Even though this program works I'm still confused as to whether I've written the program in the right way. I am also not sure when to use the self
operator.