Created
May 31, 2014 16:07
-
-
Save hillscottc/ab70980911a81bf318f9 to your computer and use it in GitHub Desktop.
The basic recursive function. Sum of a list of numbers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Recursive sum of a list of numbers. The basic recursive case. | |
Define the base case. | |
If base case, return it. | |
Else: listSum(numList) = first(numList) + listSum(rest(numList)) | |
""" | |
def listsum(numList): | |
if len(numList) == 1: | |
return numList[0] | |
else: | |
return numList[0] + listsum(numList[1:]) | |
print(listsum([1,3,5,7,9])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment