Oct-28-2020, 01:56 PM
(Oct-28-2020, 05:31 AM)omm Wrote: Let's have a look at some examples:
seven(times(five())) # must return 35
If this is all what is required then I would approach it in simple way.
I observe that
five()
is argument of function times()
and function seven()
argument(s) is/are result of function times
.So - five() should return 5. Simple enough:
def five(): return 5Now we have to think what should return function
times()
. It takes integer as input and this should be returned, otherwise we have nothing to calculate. We also should pass operator, otherwise we have no operation to perform. As this is probably homework I will go for operator module:import operator def times(num): return operator.mul, numNow we have to write function
seven()
which takes times()
as argument. Our function returns operator and integer, we just unpack and calculate:def seven(func): multiple, num = func return multiple(7, num)Now I can call as in example and get required result:
print(seven(times(five()))) -> 35As
times()
expects number one can provide it directly, without calling function:print(seven(times(5))) -> 35 print(seven(times(7))) -> 49
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.