[Small test] Python implements Yanghui's triangle

Topic: Yang Hui Triangle, as we all know

Output the Yanghui triangle for a specific layer:

1. The results of the great gods

#yanghui triangle
def triangles():
        a = [1]
        while True:
                yield a
                a = [sum(i) for i in zip([0] + a,a + [0])]

if __name__ == '__main__':
        h = int(input('Please enter the maximum number of lines you want to display:'))
        tri = triangles()
        for n in range(h):
                print(next(tri))

2. The result of one's own inertial thinking

#yanghui triangle
def sma_yanghui(h):
	a1 = [1]
	print(a1)
	n=2
	while(n<=h and n>1):
		a2=[[]]*n
		a2[0]=a1[0]
		a2[n-1]=a1[n-2]
		for j in range(1,n-1):
			a2[j]=a1[j-1]+a1[j]
		
		print(a2)

		from a1
		a1=a2[:]
		n=n+1

if __name__ == '__main__':
        h = int(input('Please enter the maximum number of lines you want to display:'))
        sma_yanghui(h)

It can only be said that the result is correct, but it is obviously a complete failure in terms of program simplicity and memory requirements.

Python is broad and profound, and I am accustomed to doing it myself in the C language, and I will really feel uncomfortable for a while.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325522762&siteId=291194637