TRENDING NEWS

POPULAR NEWS

Python Recursive Function

How to write a recursive function in Python which outputs the following?

Here's my solution, the isprime function is recursive:
http://gist.github.com/fzxt/32a3aafc0829...

isPalinprime(2,12)

output:
2 is a palin prime

3 is a palin prime

4 is not a palin prime

5 is a palin prime

6 is not a palin prime

7 is a palin prime

8 is not a palin prime

9 is not a palin prime

10 is not a palin prime

11 is a palin prime

How many recursive calls of a function in Python does it take to generate a maximum recursion depth exceeded error?

In my system(64 bit, python 3.6) it is 993 callls.You can change the limit in your system but it is dangerous. Because you are going to change the memory allocation space for the stack which is responsible for the storing the values of the programming counter during the recursion proccess.you can check it by using [math]sys.getrecursionlimit()[/math]and you can set it by using [math]sys.setrecursionlimit()[/math]

How can you explain recursive functions in Python?

Recursion is nothing special in python and can be found in other languages, too.One (mostly) speaks of recursion if a function call in some function f calls f again. There are however other cases where the function f call g and g calls f again, so generally speaking:A function is recursive if the function call hierarchy chain of this function contains a cycle to itself.Why the use of recursive functions?They are another form of doing computation and are computationally equivalent to while loops.Why not use while loops instead of recursion?Many things can be easier described with recursive functions as opposed to while loops which need for it many state variables, the use of state variables is error prone on large scale and add noise of the intended computation.Indeed, it is more easy to show correctness of recursive functions than for while loops.Why aren’t recursion used that much?For simple finite looping for-loops or map-filter-reduce functions are much more lightweight to use than recursion.Looping over an Boolean condition is what while loops are created for.And for the rest of complex computations?Recursive functions have one drawback, they allocate local variables for each recursive call which can overflow the stack memory in large scale.To make this more efficient (stackless recursion), a special compiler or runtime support is needed which is not supplied by most PL including Python.

Recursive Calling Functions in Python Question?

I am getting a weird "None" word printed out below my output. How do I get rid of it?

Write a statement that calls the recursive function backwards_alphabet() with parameter starting_letter.

def backwards_alphabet(curr_letter):
if curr_letter == 'a':
print(curr_letter)
else:
print(curr_letter)
prev_letter = chr(ord(curr_letter) - 1)
backwards_alphabet(prev_letter)

starting_letter = 'f'

Code I've used: print(backwards_alphabet(starting_letter...

HOW to write a recursive function by using Digital Root in Python?

Hey guys!!

I'm a beginner in Python and got this question and just stucked with it. (YOUR HELP IS APPRECIATED)
The digital root of a non-negative integer n is computed as follows. Begin by summing the digits of n. The digits of the resulting number are then summed, and this process is continued until a single-digit number is obtained. For example, the digital root of 2019 is 3 because 2+0+1+9=12 and 1+2=3. Write a recursive function digitalRoot(n) which returns the digital root of n.
Assume that a working definition of digitalSum will be provided for your program.
This's the provided DigitalSum:


data = 0
def digitalSum(n):
global data
if ( n < 10 ):
data += n
value = data
data = 0
return value
else:
data += n % 10
return digitlSum(n // 10)


MY ANSWER:

import math
data = 0
def digitalRoot(n):
global data
if ( n < 10 ):
data += n
value = data
data = 0
return value
else:
data += (n math.sqrt 10)
return digitlRoot(n math.sqrt 10)

Recursive Function: Writing the base case in Python?

Write code to complete double_pennies()'s base case. Sample output for below program:
Number of pennies after 10 days: 1024

def double_pennies(num_pennies, num_days):
total_pennies = 0

'''Your solution goes here'''

else:
total_pennies = double_pennies((num_pennies * 2), (num_days - 1));

return total_pennies

starting_pennies = 1
user_days = 10

print('Number of pennies after', user_days, 'days: ', end="")
print(double_pennies(starting_pennies, user_days))

What is the difference between recursion and loop in python?

Recursion involves making functions that call themselves explicitely. Loops can be implemented using recursion.
Note: -- is used in place of indents. If you wish to run the functions included, find/replace instances of "--" with tabs or spaces.

#Recursion
def factorial(X):
-- if X==1: return 1
-- return X*factorial(X-1)

#Loop
def factorial2(X):
-- retval=1
-- for num in range(2,X+1):
---- retval*=num
-- return retval


Recursion can often be much more intuitive than looping, particularly for recursive algorithms that make use of more than one call to the recursive function in the case of divide and conquer algorithms. However, excepting in languages that support it (NOT python), recursion is not free. Each recursive call adds extra stuff to the stack. Thus, trying to compute factorial 10 would eventually end up sticking multiple instances of the factorial function in memory. This is not the case if you both use tail recursion AND use a language with support for tail recursion (the recursive call being at the end of the function) optimization. However, Python is not one of them, unless you use special declarators written for that purpose (and I consider this a bad idea).

How do you design recursive functions around Python's stack limit without giving up on conciseness and simplicity?

That problem will always exist, its not possible to get around it completely, but you can reduce the size of your recursive call graph byTail Call Recursion. If you have a function which calls itself more than once in the function scope, you can pass the necesary mutated parameters as arguments to this function which then is called only once within the scope.Dynamic Programming. The function can implement a cache dictionary which stores results of previously called values so the stored result is returned instead of computing the function for the same parameters again.Looping. Sometimes, this just makes a lot more sense.

TRENDING NEWS