5. Convert the following recursive functions into iterative functions. def max(k, n): if k == []: return n elif k[0] > n: return max(k[1:], k[0]) else: return max(k[1:], n) def sum_odd(n, total): if n == 1: return total elif n % 2 == 0: return sum_odd(n 1, total) else: return sum_odd(n - 2, total + n) Hint: You may find it necessary to reduce the number of parameters for the iterative version.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 8SA
icon
Related questions
Question

solve in python

5. Convert the following recursive functions into iterative functions.
def max(k, n):
if k == []: return n
elif k[0] > n: return max(k[1:], k[0])
else: return max(k[1:], n)
def sum_odd(n, total):
if n
elif n % 2 == 0: return sum_odd(n - 1, total)
else: return sum_odd(n - 2, total + n)
1: return total
==
Hint: You may find it necessary to reduce the number of parameters for the iterative version.
Transcribed Image Text:5. Convert the following recursive functions into iterative functions. def max(k, n): if k == []: return n elif k[0] > n: return max(k[1:], k[0]) else: return max(k[1:], n) def sum_odd(n, total): if n elif n % 2 == 0: return sum_odd(n - 1, total) else: return sum_odd(n - 2, total + n) 1: return total == Hint: You may find it necessary to reduce the number of parameters for the iterative version.
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Declaring and Defining the Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning