iterator를 사용하는 패턴
class Iterator:
def nextVal(self):
pass
def hasNext(self):
pass
class ArryIterator(Iterator):
def __init__(self,arryContainer):
self.container = arryContainer.container
self.pos = -1
def hasNext(self):
if self.pos<len(self.container)-1:
return True
return False
def nextVal(self):
if self.hasNext():
self.pos += 1
return self.container[self.pos]
class ArryRevIterator(Iterator):
def __init__(self,arryContainer):
self.container = arryContainer.container
self.pos = len(self.container)
def hasNext(self):
if -1 < self.pos-1:
return True
return False
def nextVal(self):
if self.hasNext():
self.pos -= 1
return self.container[self.pos]
class ArryContainer:
def __init__(self):
self.container = []
def add(self,num:int):
self.container.append(num)
def getIterator(self):
return ArryIterator(self)
def getRevIterator(self):
return ArryRevIterator(self)
def printByIter(iter):
while iter.hasNext():
print(iter.nextVal(),end=' ')
print(' ')