Skip to content

Instantly share code, notes, and snippets.

@IlianIliev
Last active December 15, 2015 00:49
Show Gist options
  • Save IlianIliev/5175694 to your computer and use it in GitHub Desktop.
Save IlianIliev/5175694 to your computer and use it in GitHub Desktop.
The "homework" from the presentation "Function in Python" - http://ilian.i-n-i.org/functions-in-python-presentation/
# Problem 1
def f(*args, **kwargs):
return sum(list(args) + kwargs.values())
# Sample run
# f()
# f(1, 2, 3)
# f(a=1, c=3, b=2)
# f(1, b=2, c=3)
# Problem 2
import random
def dice():
i = 0
while True:
yield random.randint(1, 6)
i += 1
if i==10:
break
print 'Dice brokes, you can\'t use it any more'
# Sample run 1
#[x for x in dice()]
# Sample run 2
#d = dice()
#d.next(); d.next(); d.next(); d.next(); d.next(); d.next(); d.next(); d.next(); #d.next(); d.next(); d.next();
# Problem 3
import string
class decorator(object):
def __init__(self, func):
self.func = func
def __call__(self, *args, **kwargs):
def convertor(a):
return ord(string.lower(a)) - 97 if str(a) in string.letters else a
args = [convertor(a) for a in args]
kwargs = {key:convertor(kwargs[key]) for key in kwargs}
return self.func(*args, **kwargs)
@decorator
def f(*args, **kwargs):
return sum(list(args) + kwargs.values())
# Sample run
# f(1, 'b', 'c', **{'a': 1, 'b': 'b', 'C': 'c'})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment