0

I want to implement an iterator class(without using python built-in iterator) to only iterate over multiples of 3(could be other number,odd or even number but lets stick with 3 for now). The code should run on the given arraylist inside main() function and return multiples of 3.

This is my pseudocode below. Pretty new to python and don't know how iterator is implemented, would appreciate any help to make this code work:

class threeIterator:
    def __init__(self):
        self.it=it
        self.currval=currval

    def hasNext(self):
        while currval%3!=0 and it.hasNext():
               temp=it.next()
               if temp==None:
                   continue
               currval=temp
          if currval%3==0:
                 return True

     def next(self):
         if hasNext():
            res=currval
            currval=1
            return res
          else:
               raise exception("No such value")

        def main():
           arraylist=[random elements]
           arraylist.threeIter()

also, what is the best way to write a test case for it so i don't have to manually input numbers? thanks

gg777
  • 1
  • 4
  • You should use the python *iterator protocol*, i.e. implement `__iter__` (which for an iterator should simply `return self`) and `__next__` – juanpa.arrivillaga Jan 15 '18 at 07:26
  • the point is to not use _iter_ and build everything from scratch. – gg777 Jan 15 '18 at 07:30
  • ... why would you do that? So your iterator doesn't work with Python `for` statements, or anything else that expects an iterator? – juanpa.arrivillaga Jan 15 '18 at 07:30
  • i don't understand your point. can you provide code? – gg777 Jan 15 '18 at 07:31
  • You should read about [the iterator protocol](https://stackoverflow.com/questions/19151/build-a-basic-python-iterator) – juanpa.arrivillaga Jan 15 '18 at 07:33
  • can you clarify the requirement - you can't use any iterator class within your own class, and you can't use the iterator protocol, but what you want is to have a function called next() that returns the next multiple of 3? in which case you want the link from above by @juanpa.arrivillaga – Marcin Jan 16 '18 at 09:02

2 Answers2

0

Assume there is not init vals in the class.

class THREEITER(object):
    def __init__(self):
        self.currval = 3

    def hasNext(self):
        return True

    def next(self):
        if self.hasNext():
            res = self.currval
            self.currval += 3
            return res
        else:
            raise "No such value"


ti = THREEITER()
for i in range(100):
    if(ti.hasNext()):
        print ti.next(),
Lennon liu
  • 39
  • 4
0

As simple as I can:

class ThreeIterator(list):

    def __init__(self, *args, **kwargs):
        super(ThreeIterator, self).__init__(*args, **kwargs)

    def threeIter(self):
        return [n for n in self if n % 3 == 0]


ti = ThreeIterator([1,2,3,4,5,6,7,8,9])

for i in ti.threeIter():
    print i

Using a generator:

class ThreeIterator(list):

    def __init__(self, *args, **kwargs):
        super(ThreeIterator, self).__init__(*args, **kwargs)

    def threeIter(self):
        for i in self:
            if i % 3 == 0:
                yield i
            else:
                continue


ti = ThreeIterator([1,2,3,4,5,6,7,8,9])
git = ti.threeIter()


for i in git:
    print i
Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60