[Python] Determinazione altezza albero
    Federico Caboni 
    federico.caboni a python-academy.de
       
    Gio  5 Dic 2013 15:11:05 CET
    
    
  
Il giorno 05/dic/2013, alle ore 11:55, Alberto Granzotto <agranzot a gmail.com> ha scritto:
> 
> On Thu, Dec 5, 2013 at 12:54 AM, Dario Bertini <berdario a gmail.com> wrote:
> >>> def height(tree):
> ...   if len(tree) == 0:
> ...     return 1
> ...   else:
> ...     return 1+max(map(height, tree))
>  
> 
> bello, molto elegante l'uso di map con la funzione ricorsiva.
Stesso algoritmo, con sintassi un po’ più compatta:
def depth(tree):
	return 1 if not tree else 1+max(map(depth, tree))
con le list comprehension:
def depth(tree):
	return 1 if not tree else 1+max([depth(t) for t in tree])
con le generator expressions (quasi uguale): 
def depth(tree):
	return 1 if not tree else 1+max((depth(t) for t in tree))
— Federico 
    
    
       
       Maggiori informazioni sulla lista 
	       Python