Complete the Pattern #6-odd Ladder

Source: Internet
Author: User

Complete the Pattern #6-odd laddertask:

You has to write a function pattern which creates the following pattern (see examples) up to the desired number of rows.

    • If the Argument is 0 or a negative integers then it should return "" i.e. empty string.

    • If any even number was passed as argument then the pattern should last upto the largest odd number which is smaller than th E passed even number.

Examples:

Pattern (9):

1333555557777777999999999

Pattern (6):

133355555

Note: There are no spaces in the pattern

Hint: Use \n in string to jump to next line

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq; Public Static classkata{ Public Static stringOddladder (intN) {stringresult =string.                           Empty; List<int> list =Newlist<int>();  for(inti =1; I <= N; i = i +2) {list.                ADD (i); } result=string. Join ("\ n", list. Select (item =Selector (item))); returnresult; }         Public Static stringSelector (intNumber ) {             stringstr =string.            Empty;  for(inti =0; I < number; i++) {str= str +Number .            ToString (); }            returnstr; }}

Select in LINQ

public static ienumerable<tresult> Select<tsource, tresult> (this ienumerable<tsource> source, Func<tsource, tresult> selector);

Func<tsource, tresult> generic delegate
Public delegate TResult Func<in T, out tresult> (t Arg)

Other people's Solution:

usingSystem;usingSystem.Linq; Public Static classkata{ Public Static stringOddladder (intN) {if(N <=0)returnString.Empty; varOddnumbers = Enumerable.range (1, N). Where (i = i%2==1); varlines = Oddnumbers.select (i = String.Join ("", Enumerable.repeat (i.ToString (), i)); returnstring.join (Environment.NewLine, lines); }}

Complete the Pattern #6-odd Ladder

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.