Домашно 1 - "Пет дребни функции"

„ Програмиране с Python“, ФМИ

22.03.2010

Функциите

def one(seq): ...
def inject(fun): ...
def unfold(initial, step, condition, skip = None): ...
def theta(fun, *lists): ...
def memoize(fun): ...

Общи забележики

def func(seq):
    """This function does something"""

one - Условие

def one(seq): ...

one - Реализация

def one(seq):
    matched = False
    for item in seq:
        if item:
            if not matched:
                matched = True
            else:
                return False
    return matched

one - Забележки (1)

one - Забележки (2)

if ((bool)(condition) == True): ...
if condition: ...

inject - Условие

def inject(fun): ...

inject - Реализация

def inject(fun):
    def injector(seq, initial = None):
        seq_iter = iter(seq)
        result = initial if initial != None else next(seq_iter)
        for n in seq_iter:
            result = fun(result, n)
        return result
    return injector

unfold - Условие

def unfold(initial, step, condition, skip = None): ...

unfold - Реализация

def unfold(initial, step, condition, skip = None):
    current = initial
    while condition(current):
        if skip == None or not skip(current):
            yield current
        current = step(current)

unfold - Ваши Реализации

def unfoldrec(initial,step,condition,skip=None):
    current = initial
    if not condition(current):
        return []
    elif skip==None or not skip(current):
        return [current] + unfold(step(current),step,condition,skip)
    else:
        return unfold(step(current),step,condition,skip)

def unfold(initial,step,condition,skip=None):
    '''But the recursion was so pretty'''
	...

theta - Условие

def theta(fun, *lists): ...

theta - Реализация

def theta(fun, *lists):
    return filter(lambda args: fun(*args), product(*lists))

theta - Реализация

def theta(fun, *lists):
    return filter(lambda args: fun(*args), product(*lists))

def product(first, *other):
    if other:
        for n in first:
            for nested in product(*other):
                yield tuple([n] + list(nested))
    else:
        for n in first: yield (n,)

theta - Ваши реализации (1)

def theta(predicate, *sequences):
	import itertools
	l=itertools.product(*sequences)
	return list(filter(lambda x:predicate(*x),l))

theta - Ваши реализации (2)

def theta(predicate, *sequences):
 temp, result = [[]], []
 for seq in sequences:
   # Cartesian product
   temp = [x+[y] for x in temp for y in seq]
 for product in temp:
   if predicate(*product): result.append( tuple(product) )
 return result

theta - Забележки

memoize - Условие

def memoize(fun): ...

memoize - Реализация

def memoize(fun):
    cache = {}
    def memoized(*args):
        if args in cache: return cache[args]
        result = cache[args] = fun(*args)
        return result
    
    return memoized, lambda:(args + (result,) for args, result in cache.items())

memoize - Ваши Реализации

def memoize(fun):
    cache = {}
    graph = set()
    def memoizedfun(*args, **kwargs):
        nonlocal graph
        args += tuple(kwargs.values())
        if args in graph:
            return cache[args]
        else:
            result = fun(*args)
            cache[args] = result
            graph |= {args + (result,)}
            return result
    return memoizedfun, lambda: graph

memoize - Забележки

Още въпроси?