Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
cs190c:lab14_09 [2009/04/24 11:17]
tang
cs190c:lab14_09 [2009/04/29 23:16] (current)
tang
Line 63: Line 63:
 print L3 print L3
 print count_a(L3) print count_a(L3)
 +</​code>​
 +
 +===== Solution =====
 +<code python>​def count_a(L):
 +    if not len(L):
 +        return 0
 +    count_zero = 0
 +    if isinstance(L[0],​ list):
 +        count_zero = count_a(L[0])
 +    elif L[0] == '​a':​
 +        count_zero = 1
 +    return count_zero + count_a(L[1:​])
 +
 +if __name__ == '​__main__':​
 +    L1 = [1, 2, 3, 4]
 +    L2 = [['​a',​ 4, [4]], '​a',​ [2, [4], [3, ['​a'​]]],​ '​ab'​]
 +    L3 = [[[['​a'​]]],​ ['​a',​ '​a',​ '​a'​],​ []]
 +
 +    for L in [L1, L2, L3]:
 +        print L
 +        print count_a(L)
 </​code>​ </​code>​
 
cs190c/lab14_09.txt ยท Last modified: 2009/04/29 23:16 by tang
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki