新农上路
- 积分
- 99
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2011-11-7
- 最后登录
- 1970-1-1
|
- """
- Given a nested list of integers, returns the sum of all integers
- in the list weighted by their depth
- For example, given the list {{1,1},2,{1,1}}
- the function should return 10 (four 1's at depth 2, one 2 at depth 1)
- Given the list {1,{4,{6}}} the function should return 27
- (one 1 at depth 1, one 4 at depth 2, one 6 at depth2)
- """
- def foo(A):
- return _aux(A, 1)
- def _aux(A, weight):
- res = 0
- for item in A:
- if type(item) is list:
- res += _aux(item, weight + 1)
- else:
- res += item * weight
- return res
复制代码 |
|