{ Generators, Iterators and Decorators Exercises. }

Implement the following functions:

get_next_multiple

This function should accept a number and return the next number that is divisible by it.

gen_multiple_of_two = get_next_multiple(2)
next(gen_multiple_of_two) # 2
next(gen_multiple_of_two) # 4
next(gen_multiple_of_two) # 6
next(gen_multiple_of_two) # 8

gen_multiple_of_thirteen = get_next_multiple(13)

next(gen_multiple_of_thirteen) # 13
next(gen_multiple_of_thirteen) # 26
next(gen_multiple_of_thirteen) # 39
next(gen_multiple_of_thirteen) # 52

is_prime

This function should accept a number and return True or False if the number is a prime number.

is_prime(11) # True
is_prime(122) # False

get_next_prime

This function should return a generator that yields in the next prime number. The highest it should go should be 1000.

gen = get_next_prime()

next(gen)

double_result

This decorator function should return the result of another function multiplied by two

def add(a,b):
    return a+b

add(5,5) # 10

@double_result
def add(a,b):
    return a+b

add(5,5) # 20

only_even_parameters

This decorator function should only allow a function to have even parameters or else return the string "Please only use even numbers!"

@only_even_parameters
def add(a,b):
    return a+b

add(5,5) # "Please add even numbers!"
add(4,4) # 8

@only_even_parameters
def multiply(a,b,c,d,e):
    return a*b*c*d*e

sum_index

This function should accept a list or tuple and return the sum of each index. As a bonus, make this function able to accept a variable number of arguments.

sum_index([1,2,3,4]) # 6

zip

Research the built in zip method, what does it do? Start here

For solutions to these exercises, click here.

When you're ready, move on to Web Scraping

Continue