Skip to content

Instantly share code, notes, and snippets.

@hillscottc
Created May 31, 2014 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hillscottc/ab70980911a81bf318f9 to your computer and use it in GitHub Desktop.
Save hillscottc/ab70980911a81bf318f9 to your computer and use it in GitHub Desktop.
The basic recursive function. Sum of a list of numbers.
"""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