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 3 defnintion for the function, 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): if zero(b): return 0 else: return add(a ,mult(a, decr(b))) def gt(a, b): '''Returns True if a is an integer greater than b; returns False otherwise''' # using only incr, decr, zero, and recursion if zero(b) and zero(a): return False elif zero(b): return True elif zero(a): return False return gt(decr(a), decr(b))
Help with these Please.
def sub(a, b): '''Returns the difference between a and b. Only works if b >= 0''' # using only gt, incr, decr, zero, and recursion return def quot(a, b): '''Returns the result of integer division of a by b''' # using only sub, gt, incr, decr, zero, and recursion return def rem(a, b): '''Returns the remainder of integer division of a by b''' # using only sub, gt, 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
