[Tutor] Mutable data type in python
C Smith
illusiontechniques at gmail.com
Sat Oct 3 20:10:27 CEST 2015
> Here is my modified version which I think works as you want:
>
> def findMinDepthPath(n):
> if n <= 0: raise ValueError
> elif n==1:
> return 0
> elif n==2 or n==3:
> return 1
> else:
> d1 = findMinDepthPath(n-1)+1
> d2 = d3 = (d1+1) # initialize to higher than d1
>
> if n%3 == 0:
> d3 = findMinDepthPath(n/3)+1
> if n%2 == 0:
> d2 = findMinDepthPath(n/2)+1
>
> return min(d1,d2,d3)
>
>
> n = int(raw_input('N? '))
> print "Minimum depth = ", findMinDepthPath(n),'\n'
Doesn't this only look one level deep? Is the poster asking for
something that would traverse all possible paths and then check for
the shortest?
More information about the Tutor
mailing list