How to instruct irregular course in Enumerable in C#? -


panagiotis kanavos introduced following clever solution produce letternumbernumber pattern in sof question: for loop when values of loop variable string of pattern letternumbernumber?

var maxletters=3; // take 26 letters     var maxnumbers=3; // take 99 required numbers     var values=from char c in enumerable.range('a',maxletters).select(c=>(char)c)            int in enumerable.range(1,maxnumbers)            select string.format("{0}{1:d2}",(char)c,i);      foreach(var value in values)     {         console.writeline(value);     }     

a01 a02 a03 b01 b02 b03 c01 c02 c03 d01 d02 d03

is there way instruct irregular course in enumerable stuff? "enumerable.range(1, maxnumbers)" leads 01, 02, ...,99 (for maxnumbers 99).

restriction examples:
1. restrict (01,02,...,99) (01,03,05,07,09,11,13)
2. restrict (01,02,...,99) (02,04,06,08,10)
3. restrict (01,02,...,99) (01,04,09,10)

what did:
worked "enumarable", tried methods like: enumerable.contains(1,3,5,7,9,13) gave big error, , not achieve reach:
a01, a03, a05, ....,z09, z11, z13.

if enumarable not suitable type of job, offer handle problem?

this isn't direct feature in c#, while in f#.

f# example:

[1..2..10] 

will produce list of [1,3,5,7,9].

you first example, "restrict (01,02,...,99) (01,03,05,07,09,11,13)" can achieved with

enumerable.range(1,99).where(x => x % 2 == 1).take(7); 

the second example, "restrict (01,02,...,99) (02,04,06,08,10)" can achieved with

enumerable.range(1,99).where(x => x % 2 == 0).take(5); 

and third example, "restrict (01,02,...,99) (01,04,09,10)" seems odd. i'm not sure pattern here is. if last element isn't typo, starting @ 1 , incrementing 3, 5, 1 seems unclear, here's method can accomplish it.

public static ienumerable<int> getoddmutation(int start, int max, int count, list<int> increments) {     int counter = 0;     int reset = increments.count - 1;     int index = 0;     int incremented = start;     while(counter < count) {         var previous = incremented;         incremented += increments[index];         index = index == reset ? 0 : index + 1;         counter++;         if(previous != incremented) //avoid duplicates if 0 part of incrementation strategy. alternatively, call .distinct() on method.         yield return incremented;     } } 

called with

getoddmutation(1,99,4, new list<int> {0,3,5,1}) 

will result in [1,4,9,10]


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -