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