Is there a way to do something only in non-recursive function? - python

If I have some function like that:
def function(some number, steps):
if steps == 1: return 1
result = some calculation + function(some number, steps - 1)
return round(result, 2)
which returns some float number.
My question is: Is there a way to return rounded result only when I return result from whole function but not from recursive call (to save precision to calculation).
I hope you understand my question.
Thanks in advance.

Generally speaking, if you want to detect that you are inside a recursive call in Python, you can add a default parameter to the function like so:
def my_func(arg1, arg2, _inside_recursion=False):
# Default calls will have _inside_recursion as False
# Make sure that your recursive calls set the parameter
recursive_result = my_func(arg1, arg2, _inside_recursion=True)

Yes! You can provide a flag that will get triggered for consecutive calls:
def function(some number, steps, do_rounding=True):
if steps == 1:
return 1
result = some calculation + function(some number, steps - 1, False)
if do_rounding:
return round(result, 2)
else:
return result

There's no way you can distinguish whether your current function is being invoked as part of the recursion or as it's first step. You can, however, just introduce a new function which will call the existing one and do any extra rounding.
def internal_function(some number, steps):
if steps == 1: return 1
return some calculation + function(some number, steps - 1)
def function(some_number, steps):
return round(internal_function(some_number, steps), 2)
Edit: while true that the approach with adding an optional argument to the function can serve as well it has two downsides:
pollutes the public interface of the function - your users are now wondering whether they should give the argument a value and what value should they give exactly
makes the computational part longer to read and can also potentially destroy tail-call optimizations.

Maybe you want something like this? This is not equivalent to OP code, but makes sense. It applies some_calculation recursively steps times and rounds the final value.
def function(x, steps):
if steps == 1: return round(x, 2)
return function(some_calculation(x), steps - 1)

Related

Why doesn't execution stop when 'return' statement is encountered?

While I was reading this code:
def factorial(number):
if not isinstance(number, int):
raise TypeError("Sorry. 'number' must be an integer.")
if number < 0:
raise ValueError("Sorry. 'number' must be zero or positive.")
def inner_factorial(number):
if number <= 1:
return 1
return number * inner_factorial(number - 1)
return inner_factorial(number)
I couldn't understand this part of the code:
def inner_factorial(number):
if number <= 1:
return 1
return number * inner_factorial(number - 1)
return inner_factorial(number)
I mean, doesn't Python stop the code, when it sees the statement return? I want to know how the return number * inner_factorial(number - 1) code line works.
The line you have highlighted belongs to inner function. If it helps, you can think them separately, as follows.
def inner_factorial(number):
if number <= 1:
return 1
return number * inner_factorial(number - 1) # 1
def factorial(number):
if not isinstance(number, int):
raise TypeError("Sorry. 'number' must be an integer.")
if number < 0:
raise ValueError("Sorry. 'number' must be zero or positive.")
return inner_factorial(number) # 2
The factorial function just returns inner_factorial with the same value passed in (Line #2). Then, inner_factorial is called and it returns itself, which means it is a recursive call. inner_factorial will be calling itself until the number becomes less than or equal to 1.
How does the recursion work?
When we run factorial(4), the values are being pushed onto the Call Stack (The Call Stack is the stack data structure that stores information about the active subroutines of a computer program. The Call Stack is important because each task can have it’s own separate stack. This means that each function called can be active simultaneously for different tasks doing different things. Another advantage is that a Call Stack supports recursive calls automatically.) in the program and there will be a new inner_factorial function pushed onto the Call Stack until inner_factorial returns 1. To see that, one can use debugger (I use PyCharm's) with break point in the return statement of inner_factorial function and use step into button.
So, with factorial(4), we populate 4 new inner_factorials.
If I continue clicking in step into button, these inner_factorials are being removed one by one. (Being popped off the Call Stack which is why it is happening in reverse order from when it was pushed onto the Call Stack)
Another thing is whether or not this multiplication will be done at once, or sequentially. To test this, instead of using multiplication with *, I create a function multiply to reveal what is going on under the hood. It appears that multiplication is done as follows:
2x1 → 3x2 → 4x6. This behavior can be illustrated by running the script below:
import time
def inner_factorial(number):
if number <= 1:
return 1
time.sleep(1)
def multiply(a, b):
print(f"multiplying {a} with {b}.")
time.sleep(1)
return a * b
return multiply(number, inner_factorial(number - 1)) # <- change is in here.
# where multiply (number being popped of the call stack, return a*b)
# multiplying 2 with 1.
# multiplying 3 with 2.
# multiplying 4 with 6.
# 24
inner_factorial(4)
If I misinterpret any part and explained wrong, please let me know.
def inner_factorial(number):
if number <= 1:
return 1
return number * inner_factorial(number - 1)
return inner_factorial(number)
This part does two things:
It defines a function inner_factorial, but does NOT run it.
It returns inner_factorial(number), here is where inner_factorial is actually run.
If you define a function B inside function A, Python doesn't just return when it sees the return statement of the inner function B. I think this answers your question.

Intermediate results from recursion

I have a problem where I need to produce something which is naturally computed recursively, but where I also need to be able to interrogate the intermediate steps in the recursion if needed.
I know I can do this by passing and mutating a list or similar structure. However, this looks ugly to me and I'm sure there must be a neater way, e.g. using generators. What I would ideally love to be able to do is something like:
intermediate_results = [f(x) for x in range(T)]
final_result = intermediate_results[T-1]
in an efficient way. While my solution is not performance critical, I can't justify the massive amount of redundant effort in that first line. It looks to me like a generator would be perfect for this except for the fact that f is fundamentally much more suited to recursion in my case (which at least in my mind is the complete opposite of a generator, but maybe I'm just not thinking far enough outside of the box).
Is there a neat Pythonic way of doing something like this that I just don't know about, or do I just need to just capitulate and pollute my function f by passing it an intermediate_results list which I then mutate as a side-effect?
I have a generic solution for you using a decorator. We create a Memoize class which stores the results of previous times the function is executed (including in recursive calls). If the arguments given have already been seen, the cached versions are used to quickly lookup the result.
The custom class has the benefit over an lru_cache in that you can see the results.
from functools import wraps
class Memoize:
def __init__(self):
self.store = {}
def save(self, fun):
#wraps(fun)
def wrapper(*args):
if args not in self.store:
self.store[args] = fun(*args)
return self.store[args]
return wrapper
m = Memoize()
#m.save
def fibo(n):
if n <= 0: return 0
elif n == 1: return 1
else: return fibo(n-1) + fibo(n-2)
Then after running different things you can see what the cache contains. When you run future function calls, m.store will be used as a lookup so calculation doesn't need to be redone.
>>> f(8)
21
>>> m.store
{(1,): 1,
(0,): 0,
(2,): 1,
(3,): 2,
(4,): 3,
(5,): 5,
(6,): 8,
(7,): 13,
(8,): 21}
You could modify the save function to use the name of the function and the args as the key, so that multiple function results can be stored in the same Memoize class.
You can use your existing solution that makes many "redundant" calls to f, but employ the use of function caching to save the results to previous calls to f.
In other words, when f(x1) is called, it's input arguments and corresponding return values are saved, and the next time it is called, the result is simply pulled from the cache
see functools.lru_cache for the standard library solution to this
ie:
from functools import lru_cache
#lru_cache
intermediate_results = [f(x) for x in range(T)]
final_result = intermediate_results[T-1]
Note, however, f must be a pure function (no side-effects, 1-to-1 mapping) for this to work properly
Having considered your comments, I'll now try to give another perspective on the problem.
So, let's consider a concrete example:
def f(x):
a = 2
return g(x) + a if x != 0 else 0
def g(x):
b = 1
return h(x) - b
def h(x):
c = 1/2
return f(x-1)*(1+c)
I
First of all, it should be mentioned that (in our particular case) the algorithm has form of: f(x) = p(f(x - 1)) for some p. It follows that f(x) = p^x(f(0)) = p^x(0). That means we should just apply p to 0 x times to get the desired result, which can be done in an iterative process, so this can be written without recursion. Though I believe that your real case is much harder. Moreover, it would be too boring and uninformative to stop here)
II
Generally speaking, we can divide all possible solutions into two groups: the ones that require refactoring (i.e. rewriting functions f, g, h) and the ones that do not. I have little to offer from the latter one (and I don't think anyone can). Consider the following, however:
def fk(x, k):
a = 2
return k(gk(x, k) + a if x != 0 else 0)
def gk(x, k):
b = 1
return k(hk(x, k) - b)
def hk(x, k):
c = 1/2
return k(fk(x-1, k)*(1+c))
def printret(x):
print(x)
return x
f(4, printret) # see what happens
Inspired by continuation-passing style, but that's totally not it.
What's the point? It's something between your idea of passing a list to write down all the computations and memoizing. This k carries additional behavior with it, such as printing or writing to list (you can make a function that writes to some list, why not?). But if you look carefully you'll see that it lefts inner code of these functions practically untouched (only input and output to function are affected), so one can produce a decorator associated with a function like printret that does essentially the same thing for f, g, h.
Pros: no need to modify code, much more flexible than passing a list, no additional work (like in memoizing).
Cons: Impure (printing or modifying sth), not so flexible as we would like.
III
Now let's see how modifying function bodies can help. Don't be afraid of what's written below, take your time and play with that thing a little.
class Logger:
def __init__(self, lst, cur_val):
self.lst = lst
self.cur_val = cur_val
def bind(self, f):
res = f(self.cur_val)
return Logger([self.cur_val] + res.lst + self.lst, res.cur_val)
def __repr__(self):
return "Logger( " + repr({'value' : self.cur_val,'lst' : self.lst}) + " )"
def unit(x):
return Logger([], x)
# you can also play with lala
def lala(x):
if x <= 0:
return unit(1)
else:
return lala(x - 1).bind(lambda y: unit(2*y))
def f(x):
a = 2
if x == 0:
return unit(0)
else:
return g(x).bind(lambda y: unit(y + a))
def g(x):
b = 1
return h(x).bind(lambda y: unit(y - b))
def h(x):
c = 1/2
return f(x-1).bind(lambda y: unit(y*(1+c)))
f(4) # see for yourself
Logger is called a monad. I'm not very familiar with this concept myself, but I guess I'm doing everything right) f, g, h are functions that take a number and return a Logger instance. Logger's bind takes in a function (like f) and returns Logger with new value (computed by f) and updated 'logs'. The key point - as I see it - is the ability to do whatever we want with collected functions in the order the resulting value was calculated.
Afterword
I'm not at all some kind of 'guru' of functional programming, I believe I'm missing a lot of things here. But what I've understood is that functional programming is about inversing the flow of the program. That's why, for instance, I totally agree with your opinion about generators being opposed to functional programming. When we use generator gen in, say, function func, we yield values one by one to func and func does sth with them in e.g. a loop. The functional approach would be to make gen a function taking func as a parameter and make func perform computations on 'yielded' values. It's like gen and func exchanged their places. So the flow is inversed! And there are plenty of other ways of inversing the flow. Monads are one of them.
itertools islice gets a generator, start value and stop value. it will give you the elements between the start value and stop value as a generator. if islice is not clear you can check the docs here https://docs.python.org/3/library/itertools.html
intermediate_result = map(f, range(T))
final_result = next(itertools.islice(intermediate_result, start=T-1, stop=T))

Python Lambda usage

Im trying to learn how to utilize the lambda function and i am having some difficulties.
example from HomeWork.
i have the following functions
def square(x):
return x**2
def incr(x):
return x+1
i need to create a function called 'repeated' that will take one of the above functions + an integer and will return some value that depends on the times the functions have been called IE:
repeated (incr,4)(2)
6
repeated (square,2)(5)
625
using a 'compose' function.
def compose(f,g):
return lambda x:f(g(x))
thank in advance for an explanation.
If you should use a lambda, you can use recursion like this:
def repeated(func, times):
return lambda x: repeated(func, times - 1)(func(x)) if times > 0 else x
what repeated return is a function waiting for an argument, x,and then it activates itself times times (descriptive...) on x
When it should no longer activate itself (in other words, times <= 0, it stops and returns the x the last call received.
You can use compose in the same manner as follows:
If you want to use compose you can do this:
def repeated(func, times):
return compose(func, repeated(func, times-1)) if times > 0 else lambda x
What this does is just call compose recursively times - 1 more times, using repeated, until no more composing is needed.
Edit:
Python's standard implementation and recursion don't get along too well, both of these solution are correct, but using a loop is more pythonic. Another answer is available that does just that.
repeated needs to return a callable too; compose() can produce that callable for you, but you need to apply it multiple times:
def repeated(f, count):
callable = f
for _ in range(count - 1):
callable = compose(f, callable)
return callable
So if count is 1, the original f() is returned. For count = 2, f(f()) is returned, etc.

calculating current value based on previous value

i would like to perform a calculation using python, where the current value (i) of the equation is based on the previous value of the equation (i-1), which is really easy to do in a spreadsheet but i would rather learn to code it
i have noticed that there is loads of information on finding the previous value from a list, but i don't have a list i need to create it! my equation is shown below.
h=(2*b)-h[i-1]
can anyone give me tell me a method to do this ?
i tried this sort of thing, but that will not work as when i try to do the equation i'm calling a value i haven't created yet, if i set h=0 then i get an error that i am out of index range
i = 1
for i in range(1, len(b)):
h=[]
h=(2*b)-h[i-1]
x+=1
h = [b[0]]
for val in b[1:]:
h.append(2 * val - h[-1]) # As you add to h, you keep up with its tail
for large b list (brr, one-letter identifier), to avoid creating large slice
from itertools import islice # For big list it will keep code less wasteful
for val in islice(b, 1, None):
....
As pointed out by #pad, you simply need to handle the base case of receiving the first sample.
However, your equation makes no use of i other than to retrieve the previous result. It's looking more like a running filter than something which needs to maintain a list of past values (with an array which might never stop growing).
If that is the case, and you only ever want the most recent value,then you might want to go with a generator instead.
def gen():
def eqn(b):
eqn.h = 2*b - eqn.h
return eqn.h
eqn.h = 0
return eqn
And then use thus
>>> f = gen()
>>> f(2)
4
>>> f(3)
2
>>> f(2)
0
>>>
The same effect could be acheived with a true generator using yield and send.
First of, do you need all the intermediate values? That is, do you want a list h from 0 to i? Or do you just want h[i]?
If you just need the i-th value you could us recursion:
def get_h(i):
if i>0:
return (2*b) - get_h(i-1)
else:
return h_0
But be aware that this will not work for large i, as it will exceed the maximum recursion depth. (Thanks for pointing this out kdopen) In that case a simple for-loop or a generator is better.
Even better is to use a (mathematically) closed form of the equation (for your example that is possible, it might not be in other cases):
def get_h(i):
if i%2 == 0:
return h_0
else:
return (2*b)-h_0
In both cases h_0 is the initial value that you start out with.
h = []
for i in range(len(b)):
if i>0:
h.append(2*b - h[i-1])
else:
# handle i=0 case here
You are successively applying a function (equation) to the result of a previous application of that function - the process needs a seed to start it. Your result looks like this [seed, f(seed), f(f(seed)), f(f(f(seed)), ...]. This concept is function composition. You can create a generalized function that will do this for any sequence of functions, in Python functions are first class objects and can be passed around just like any other object. If you need to preserve the intermediate results use a generator.
def composition(functions, x):
""" yields f(x), f(f(x)), f(f(f(x)) ....
for each f in functions
functions is an iterable of callables taking one argument
"""
for f in functions:
x = f(x)
yield x
Your specs require a seed and a constant,
seed = 0
b = 10
The equation/function,
def f(x, b = b):
return 2*b - x
f is applied b times.
functions = [f]*b
Usage
print list(composition(functions, seed))
If the intermediate results are not needed composition can be redefined as
def composition(functions, x):
""" Returns f(x), g(f(x)), h(g(f(x)) ....
for each function in functions
functions is an iterable of callables taking one argument
"""
for f in functions:
x = f(x)
return x
print composition(functions, seed)
Or more generally, with no limitations on call signature:
def compose(funcs):
'''Return a callable composed of successive application of functions
funcs is an iterable producing callables
for [f, g, h] returns f(g(h(*args, **kwargs)))
'''
def outer(f, g):
def inner(*args, **kwargs):
return f(g(*args, **kwargs))
return inner
return reduce(outer, funcs)
def plus2(x):
return x + 2
def times2(x):
return x * 2
def mod16(x):
return x % 16
funcs = (mod16, plus2, times2)
eq = compose(funcs) # mod16(plus2(times2(x)))
print eq(15)
While the process definition appears to be recursive, I resisted the temptation so I could stay out of maximum recursion depth hades.
I got curious, searched SO for function composition and, of course, there are numerous relavent Q&A's.

Improving Newton's Method Recursion

I have solved a previous problem and now I am stuck with this one.
It is asking for an improvement of the previous solution which I posted below, but can't quite understand what the problem is asking. (Also, can't really figure out how to solve it.)
Please help
thanks.
Problem:
Elena complains that the recursive newton function in Project 2 includes
an extra argument for the estimate. The function’s users should not have to
provide this value, which is always the same, when they call this function.
Modify the definition of the function so that it uses a keyword parameter
with the appropriate default value for this argument, and call the function
without a second argument to demonstrate that it solves this problem.
Here is my code:
def newtonSquare(x, estimate):
if abs(x-estimate ** 2) <= 0.000001:
return estimate
else:
return newtonSquare(x, (estimate + x / estimate) / 2)
def main():
num = int(raw_input('Enter a positive number >> '))
print newtonSquare(num, 1.0)
main()
The problem is a complaint that when somebody calls newtonSquare, they always do so with an initial estimate of 1.0. To set it as the default value, unless you explicitly provide it, you only have to modify the definition of the function to:
def newtonSquare(x, estimate = 1.0):
An alternative to the default value solution of Michael's is defining the recursive function local to the newtonSquare function:
def newtonSquare(x):
def f(x, estimate):
if abs(x-estimate ** 2) <= 0.000001:
return estimate
else:
return f(x, (estimate + x / estimate) / 2)
return f(x, 1.0)
def main():
num = int(raw_input('Enter a positive number >> '))
print newtonSquare(num)
main()
In this case having the default value is nicer and more flexible though, but this can still be a useful pattern.

Categories