Not a member of Pastebin yet?
                        Sign Up,
                        it unlocks many cool features!                    
                - # Iterators and Generators - Exercise
 - ========================================================================================
 - # 01. Take Skip
 - class take_skip:
 - def __init__(self, step, count):
 - self.step = step
 - self.count = count
 - self.counter = 0
 - def __iter__(self):
 - return self
 - def __next__(self):
 - if self.counter >= self.count:
 - raise StopIteration
 - result = self.counter * self.step
 - self.counter += 1
 - return result
 - ========================================================================================
 - # 02. Dictionary Iterator
 - class dictionary_iter:
 - def __init__(self, kwargs):
 - self.kwargs = kwargs
 - self.counter = 0
 - def __iter__(self):
 - return self
 - def __next__(self):
 - if len(self.kwargs) == 0:
 - raise StopIteration
 - for count, number in self.kwargs.items():
 - result = count,number
 - del self.kwargs[count]
 - break
 - return result
 - ========================================================================================
 - # 03. Countdown Iterator
 - class countdown_iterator:
 - def __init__(self, num):
 - self.num = num
 - def __iter__(self):
 - return self
 - def __next__(self):
 - if self.num < 0:
 - raise StopIteration
 - result = self.num
 - self.num -= 1
 - return result
 - ========================================================================================
 - # 04. Sequence Repeat
 - class sequence_repeat:
 - def __init__(self, text, count):
 - self.text = text
 - self.count = count
 - self.index = 0
 - def __iter__(self):
 - return self
 - def __next__(self):
 - if self.index == self.count:
 - raise StopIteration
 - result = self.text[self.index % len(self.text)]
 - self.index += 1
 - return result
 - ========================================================================================
 - # 05. Take Halves
 - def solution():
 - def integers():
 - num = 1
 - while True:
 - yield num
 - num += 1
 - def halves():
 - for num in integers():
 - yield num / 2
 - def take(n, seq):
 - return [next(seq) for _ in range(n)]
 - return take, halves, integers
 - ========================================================================================
 - # 06. Fibonacci Generator
 - def fibonacci():
 - n1, n2 = 0, 1
 - while True:
 - yield n1
 - n1, n2 = n2, n1 + n2
 - ========================================================================================
 - # 07. Reader
 - def read_next(*args):
 - for el in args:
 - for sub_el in el:
 - yield sub_el
 - ========================================================================================
 - # 08. Prime Numbers
 - def get_primes(numbers):
 - for number in numbers:
 - if number <= 1:
 - continue
 - for num in range(2,number):
 - if number % num == 0:
 - break
 - else:
 - yield number
 - ========================================================================================
 - # 09. Possible permutations
 - from itertools import permutations
 - def possible_permutations(seq):
 - for el in permutations(seq):
 - yield list(el)
 
Advertisement
 
                    Add Comment                
                
                        Please, Sign In to add comment