Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Generator to output Yang Hui Triangle in python

Shulou Source: shulou.com Published: 2022-06-01 02:08:58 09月10日 Update

Editor to share with you how to use python generator to output Yang Hui triangle, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Generator

Def triangle_2 (n): "": param n: number of Yang Hui triangle rows to be generated: return: "" triangle = [1] # initialize Yang Hui triangle for i in range (n): yield triangle triangle.append (0) # add a 0 to the last bit Used to calculate the next line triangle = [triangle [I] + triangle [I-1] for i in range (len (triangle))] # take the value for i in triangle_5 (10) from the generator: print ('. Join (str (I)). Center (100)) # formatted output

The above is all the contents of the article "how to use the generator to output Yang Hui triangles by python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Generation generator Yang Hui output article angle content Yang Hui triangle triangle line not much most more format knowledge industry information channel channel reference Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Redmi Xiaomi Shulou Tech Info MariaDB