Convert the following recursive functions into iterative ones: 1. def sum_odd (n, total): if n = 2. 3. == 1 return total elif n % 2 == 0: return sum_odd (n - 1, total) else return sum_odd (n - 2, total + n ) def max (I, n): if I []: return n elif [0] > n return max (1[1:], [0] ) else return max (1[1:], n) : == def mylen (I, n): if | == : return n else return mylen (1[1:], n +1)

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 6PE
icon
Related questions
Question

Code Must be in Python. I will upvote you. 

Convert the following recursive functions into iterative ones:
1.
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)
2.
3.
def max (I, n):
if I ==
[]: return n
elif [0] >n : return max (1[1:], 1[0])
else return max (1[1:], n)
def mylen (1, n):
if I == =: return n
else : return mylen (1[1:], n +1)
Transcribed Image Text:Convert the following recursive functions into iterative ones: 1. 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) 2. 3. def max (I, n): if I == []: return n elif [0] >n : return max (1[1:], 1[0]) else return max (1[1:], n) def mylen (1, n): if I == =: return n else : return mylen (1[1:], n +1)
Expert Solution
steps

Step by step

Solved in 2 steps with 3 images

Blurred answer
Knowledge Booster
Computational Systems
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