Question: Using python 3.0 or greater. I need help writing this recursive function. Question: Write a recursive function (not using loops, global varibles, and conditional operators
Using python 3.0 or greater. I need help writing this recursive function.
Question: Write a recursive function (not using loops, global varibles, and conditional operators (+,-,=,<=,==,!=, etc) to for the following definitions.
In other words, make the functions rely on the definitions given and use recursion (can be a bit confusing).
(Three given functions to use in question)
def incr(a):
'''Returns the next integer after a''' return a + 1 def zero(a): '''Returns True if a is zero''' return a == 0 def decr(a): '''Returns the integer before a''' return a - 1
(Write the following recursive functions below, by using the functions above.)
I already got the first defnintion for the function add(a,b), but need help writing the recursive for the others.
(Notice how I did not use any +,-,==,=,//,*,etc...becuase they are not allowd in question, only recursion and the functions given.)
def add(a, b):
'''Returns the sum of a and b''' # using only incr, decr, zero, and recursion
if zero(a) and zero(b): return 0 elif zero(b): return a else: return add(incr(a), decr(b))
def mult(a, b): '''Returns the product of a and b''' # using only add, incr, decr, zero, and recursion return def gt(a, b): '''Returns True if a is an integer greater than b; returns False otherwise''' # using only incr, decr, zero, and recursion return
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
