Skip to content

Instantly share code, notes, and snippets.

@jesuscast
Last active August 29, 2015 14:02
Show Gist options
  • Save jesuscast/90ef9b07af4943e336d3 to your computer and use it in GitHub Desktop.
Save jesuscast/90ef9b07af4943e336d3 to your computer and use it in GitHub Desktop.
Finds the depth of a mathematical expression. It follows the syntax expr = ('operator','term1','term2',...,'termN')
def depth(expr):
depthLevel = 0
if(isinstance(expr,(list,tuple))==False):
return 0
else:
depthLevel += 1
if( isinstance(expr[1],(list,tuple)) ):
biggestDepth = 0
for i in range(1,len(expr)):
depthOfCurrentTerm = depth(expr[i])
if depthOfCurrentTerm > biggestDepth:
biggestDepth = depthOfCurrentTerm
depthLevel += biggestDepth
return depthLevel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment