Example 1: Algae
Lindenmayer's original L-system for modelling the growth of algae.
- variables : A B
- constants : none
- start : A
- rules : (A → AB), (B → A)
which produces:
- n = 0 : A
- n = 1 : AB
- n = 2 : ABA
- n = 3 : ABAAB
- n = 4 : ABAABABA
- n = 5 : ABAABABAABAAB
- n = 6 : ABAABABAABAABABAABABA
- n = 7 : ABAABABAABAABABAABABAABAABABAABAAB
def algae(n): if n == 0: return 'A' if n == 1: return 'B' if n == 2: return 'A' else: return algae(n-1)+algae(n-2) def all(n): result = '' for i in range(n+1): result+= algae(i) return result
def growth_of_algae(n): for i in range(n): print 'n = %d : %s' % (i, all(i))
n = int(raw_input("enter number of generation:\n")) growth_of_algae(n)
|
阅读(589) | 评论(0) | 转发(0) |