Question

Write a recursive function sum-odds that takes a non-empty list of integers

in python 

Part I: Sum of Odd Integers 

Write a recursive function sum-odds that takes a non-empty list of integers as an argument and returns the sum of only the odd integers in the list. In class we explored a recursive function called rsum that recursively computes the sum of a list of integers use it as a model to get started. Your function must be recursive and must not use any loops

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. sumOfOddInteger.py

def sumOfOddInteger(myList,size):
if size > 0:
if myList[size-1]%2!=0:
return myList[size-1]+sumOfOddInteger(myList,size-1)
else:
return sumOfOddInteger(myList,size-1)
else:
return 0

myList=[1,2,3,4];
print myList
print sumOfOddInteger(myList,4);
myList=[1,2,3,4,5,6,7,8,9]
print myList
print sumOfOddInteger(myList,9)

Steps to run & sample output is shown in below screenshot.

Add a comment
Know the answer?
Add Answer to:
Write a recursive function sum-odds that takes a non-empty list of integers
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT