|
| 1 | +### |
| 2 | +# video challenge answer |
| 3 | +# |
| 4 | +def get_money(customer, amount): |
| 5 | + # calculate money needed to cover the "amount" to the nearest 5 dollar bill |
| 6 | + nonfive_bills = amount % 5 |
| 7 | + remfive_bills = amount - nonfive_bills |
| 8 | + needed = remfive_bills + (0 if nonfive_bills == 0 else 5) |
| 9 | + # take that money out of the customers wallet |
| 10 | + customer['wallet'] -= needed |
| 11 | + # return the money you calculated to pay for the meal |
| 12 | + return needed |
| 13 | + |
| 14 | +### |
| 15 | +# factory functions |
| 16 | +# |
| 17 | +def make_is_divisble(den): |
| 18 | + |
| 19 | + def is_divisible(num): |
| 20 | + return num % den == 0 |
| 21 | + |
| 22 | + return is_divisible |
| 23 | + |
| 24 | +# create functions |
| 25 | +is_div_2 = make_is_divisble(2) |
| 26 | +is_div_13 = make_is_divisble(13) |
| 27 | + |
| 28 | +# use them |
| 29 | +a = is_div_2(12) |
| 30 | +b = is_div_2(345+16122-48//45) |
| 31 | +c = is_div_2(23*41981) |
| 32 | + |
| 33 | +d = is_div_13(341) |
| 34 | +e = is_div_13(5+12-48) |
| 35 | +f = is_div_13(54*81) |
| 36 | + |
| 37 | +# use it without saving the function |
| 38 | +g = make_is_divisble(5)(1615) |
| 39 | + |
| 40 | + |
| 41 | +### |
| 42 | +# wrap existing functions |
| 43 | +# |
| 44 | +def whats_running(function): |
| 45 | + """Says when a function starts and stops. |
| 46 | +
|
| 47 | + @usage |
| 48 | + your_function = whats_running(your_function) |
| 49 | + """ |
| 50 | + |
| 51 | + def new_function(): |
| 52 | + print(function.__name__, 'has started.') |
| 53 | + function() |
| 54 | + print(function.__name__, 'has stopped.') |
| 55 | + |
| 56 | + return new_function |
| 57 | + |
| 58 | + |
| 59 | +def time_this(function): |
| 60 | + """Times a function in seconds. |
| 61 | +
|
| 62 | + @usage |
| 63 | + your_function = time_this(your_function) |
| 64 | + """ |
| 65 | + import time |
| 66 | + |
| 67 | + def wrapper(*args, **kargs): |
| 68 | + start = time.time() |
| 69 | + result = function(*args, **kargs) |
| 70 | + stop = time.time() |
| 71 | + print(f'{function.__name__} took {stop - start} seconds to execute.') |
| 72 | + return result |
| 73 | + |
| 74 | + wrapper.__name__ = function.__name__ + '_timed' |
| 75 | + return wrapper |
| 76 | + |
| 77 | +# used for example functions |
| 78 | +club = ['jake', 'alice', 'mark', 'john', 'jerry', 'kipper'] |
| 79 | + |
| 80 | +# example functions |
| 81 | +def counter(): |
| 82 | + for x in range(1_000_000): |
| 83 | + pass |
| 84 | + |
| 85 | +def in_club(name, my_club=club): |
| 86 | + return name in my_club |
| 87 | + |
| 88 | +# apply the wrapper functions |
| 89 | +counter = time_this(counter) |
| 90 | +in_club = time_this(in_club) |
| 91 | + |
| 92 | +# you can apply a wrapper to an existing wrapped function |
| 93 | +counter = whats_running(counter) |
| 94 | + |
| 95 | +# call our newly wrapped functions |
| 96 | +counter() |
| 97 | + |
| 98 | +if in_club('jake'): |
| 99 | + print('They are in the club.') |
| 100 | +else: |
| 101 | + print('They are not in the club.') |
| 102 | + |
| 103 | +counter() |
0 commit comments