Last active
August 29, 2015 14:02
-
-
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')
This file contains hidden or 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
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