Advertisement
horozov86

sequence_repeat

Jul 19th, 2023
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. class sequence_repeat:
  2.     def __init__(self, sequence, number):
  3.         self.sequence = sequence
  4.         self.number = number
  5.         self.idx = 0 # създаваме допълнителен атрибут self.idx = 0
  6.        
  7.     def __iter__(self):
  8.         return self
  9.        
  10.     def __next__(self):
  11.         if self.idx == self.number: # ако индекса стане равен на каунтъра, приключва
  12.             raise StopIteration
  13.         result = self.sequence[self.idx % len(self.sequence)] # ако стринга > каунтъра за да се върнем в началото на стринга
  14.         # делим модулно индекаса на дължината на стринга, при 3 % 3 връща индекс 0, при 4 % 3, връща индекс 1 и т.н.
  15.         self.idx += 1
  16.         return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement