Output Yang Hui triangle with yield keyword

Source: Internet
Author: User

Recently learned the next python, found that there is also the use of yield, originally on C # yield is not very understanding, but through learning Python, C # yield understanding deeper!!

Not much to say, the level of pupils can not hurt the ability to express ....

Directly on the code:

Using system;using system.collections;using system.collections.generic;using system.linq;using System.Text; Namespace consoleapplication2{class Program {static void Main (string[] args) {foreach (V            Ar obj in YHSJ (8)) {Printrow (obj);        } console.readkey (); }///<summary>//Get an iterator to the Yang Hui triangle///</summary>//<param name= "n" ></param         >//<returns></returns> public static ienumerable<ienumerable<int>> yhsj (int n)            {//First line list<int> row = new List<int> {1};            yield return row;            Second row = new List<int> {1, 1};            yield return row;                The third row and its subsequent while (n > 0) {list<int> NewRow = new List<int> {1, 1}; int index = row.                Count; for (int i = 1; I &lT Index i++) {NewRow.                Insert (1, row[i-1] + row[i]);                } yield return newrow;                row = NewRow;            n--; }}///<summary>///print a row of data///</summary>//<param name= "Row" >&lt            ;/param> public static void Printrow (Ienumerable<int> row) {foreach (int i in row)            {Console.Write (i + "\ t");        } Console.WriteLine (); }    }}

  

Output Yang Hui triangle with yield keyword

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.