Advertisement
GeorgiLukanov87

Iterators and Generators - Exercise

Dec 9th, 2022 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.38 KB | None | 0 0
  1. # Iterators and Generators - Exercise
  2.  
  3.  
  4. ========================================================================================
  5. # 01. Take Skip
  6.  
  7. class take_skip:
  8.     def __init__(self, step, count):
  9.         self.step = step
  10.         self.count = count
  11.         self.counter = 0
  12.  
  13.     def __iter__(self):
  14.         return self
  15.  
  16.     def __next__(self):
  17.         if self.counter >= self.count:
  18.             raise StopIteration
  19.  
  20.         result = self.counter * self.step
  21.         self.counter += 1
  22.         return result
  23. ========================================================================================
  24.  
  25. # 02. Dictionary Iterator
  26.  
  27. class dictionary_iter:
  28.     def __init__(self, kwargs):
  29.         self.kwargs = kwargs
  30.         self.counter = 0
  31.  
  32.     def __iter__(self):
  33.         return self
  34.  
  35.     def __next__(self):
  36.         if len(self.kwargs) == 0:
  37.             raise StopIteration
  38.  
  39.         for count, number in self.kwargs.items():
  40.             result = count,number
  41.             del self.kwargs[count]
  42.             break
  43.  
  44.         return result
  45. ========================================================================================
  46. # 03. Countdown Iterator
  47.  
  48. class countdown_iterator:
  49.     def __init__(self, num):
  50.         self.num = num
  51.  
  52.     def __iter__(self):
  53.         return self
  54.  
  55.     def __next__(self):
  56.         if self.num < 0:
  57.             raise StopIteration
  58.  
  59.         result = self.num
  60.         self.num -= 1
  61.         return result
  62. ========================================================================================
  63.  
  64. # 04. Sequence Repeat
  65.  
  66. class sequence_repeat:
  67.     def __init__(self, text, count):
  68.         self.text = text
  69.         self.count = count
  70.         self.index = 0
  71.  
  72.     def __iter__(self):
  73.         return self
  74.  
  75.     def __next__(self):
  76.         if self.index == self.count:
  77.             raise StopIteration
  78.  
  79.         result = self.text[self.index % len(self.text)]
  80.         self.index += 1
  81.         return result
  82. ========================================================================================
  83.  
  84. # 05. Take Halves
  85.  
  86. def solution():
  87.     def integers():
  88.         num = 1
  89.         while True:
  90.             yield num
  91.             num += 1
  92.  
  93.     def halves():
  94.         for num in integers():
  95.             yield num / 2
  96.  
  97.     def take(n, seq):
  98.         return [next(seq) for _ in range(n)]
  99.  
  100.     return take, halves, integers
  101. ========================================================================================
  102.  
  103. # 06. Fibonacci Generator
  104.  
  105. def fibonacci():
  106.     n1, n2 = 0, 1
  107.  
  108.     while True:
  109.         yield n1
  110.  
  111.         n1, n2 = n2, n1 + n2
  112. ========================================================================================
  113.  
  114. # 07. Reader
  115.  
  116. def read_next(*args):
  117.     for el in args:
  118.         for sub_el in el:
  119.             yield sub_el
  120. ========================================================================================
  121.  
  122. # 08. Prime Numbers
  123.  
  124. def get_primes(numbers):
  125.     for number in numbers:
  126.         if number <= 1:
  127.             continue
  128.  
  129.         for num in range(2,number):
  130.             if number % num == 0:
  131.                 break
  132.  
  133.         else:
  134.             yield number
  135. ========================================================================================
  136.  
  137. # 09. Possible permutations
  138.  
  139. from itertools import permutations
  140.  
  141.  
  142. def possible_permutations(seq):
  143.     for el in permutations(seq):
  144.         yield list(el)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement