再帰を実装してみる

コーディングを支える技術を読んでて再帰の話が出てきたので
読む前にまず自分で実装してみた。

#/usr/bin/env python                                                            
#-*-coding:utf-8-*-                                                             

array = [1,2,[3,4],5]

def summation(array):
    total = 0
    for a in array:
        if(type(a) == type(array)):
            total += summation(a)
        else:
            total += a
    return total

print summation(array)

Pythonには整数なのかリストかどうか判定する関数(type())が
あったから比較的簡単に実装できた。