Skip to content

Instantly share code, notes, and snippets.

@abenezerangelos
Created March 28, 2022 12:44
Show Gist options
  • Save abenezerangelos/a01c614f34c908e02f857cb8b448ed60 to your computer and use it in GitHub Desktop.
Save abenezerangelos/a01c614f34c908e02f857cb8b448ed60 to your computer and use it in GitHub Desktop.
The word counter problem but instead using in-built iterator's __next__() function. So happy
class counter():
def get_next(self):
while self.controller==True and self.i!=None:
try:
self.i=self.numbers.__next__()
except:
return None
self.controller=True
self.c=""
while not self.i.isspace() and self.i!="\n" and self.i!=None :
self.c += self.i
self.controller=False
try:
self.i = self.numbers.__next__()
except:
return None
self.controller=True
print(self.c)
return self.c
def __init__(self,numbers):
self.j=0
self.i=""
self.c=""
self.controller=True
self.store=[]
self.numbers=iter(numbers)
self.word= self.get_next()
def __next__(self):
n=0
if self.word is None:
raise StopIteration
final=self.word
# if word is None
# print(word)
self.store.append(self.word)
for item in self.store:
print(item)
if self.word == item and self.word != None:
n+=1
elif self.word == None:
return (final)
self.word = self.get_next()
print((final, n))
print(self.store)
return ((final,n))
def __iter__(self):
return self
numbers = """
one
one two
one two three
one two three four
one two three four five
one two three four five six
"""
mywords= counter(numbers)
r=[]
for item in mywords:
r.append(item)
print(r)
print (mywords.__next__())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment