Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
P.S. I'm not an experienced programmer !

I've a generator code for sequential numbers each on a line:
0001
0002
0003

I'm trying to create a function using an integer N, which would repeat each line "N" times:
0001
0001
0001
0002
0002
0002
0003
0003
0003


Here is the generator part:

C#
int temp = start_number;
int progress_value = 0;
File.AppendAllText(strPath, prefix + start_number.ToString().PadLeft(L,T) + suffix + "\n");
txt_sample.Invoke((MethodInvoker)delegate { txt_sample.AppendText(prefix + start_number.ToString().PadLeft(L,T) + suffix + Environment.NewLine); });
while (true)

{

    temp +=   increment_number;
    progress_value += increment_number;
    if (temp > end_number )
        break;
    string sample = prefix + temp.ToString().PadLeft(L, T) + suffix.ToString() + "\n";

    File.AppendAllText(strPath, sample);
    txt_sample.Invoke((MethodInvoker)delegate { txt_sample.AppendText(prefix + temp.ToString().PadLeft(L,T) + suffix + Environment.NewLine); });
    if(end_number - start_number < progress_value)
    {
        break;
    }

    progress.Invoke((MethodInvoker)delegate { progress.Value = progress_value; });


What I have tried:

I tried implementing
string.Concat(Enumerable.Repeat(sample, N));

however not sure how it should fit the code ?
Thank You
Posted
Updated 24-Jan-21 19:52pm
v3

The simplest way is just a nested loop:
C#
public static string GetRepeatedSequential(int start, int end, int repeat)
    {
    StringBuilder sb = new StringBuilder();
    for(int i = start; i <= end; i++)
        {
        for (int j = 0; j < repeat; j++ )
            {
            sb.AppendLine($"{i:D4}");
            }
        }
    return sb.ToString();
    }

To do it using Linq methods like Repeat isn't that complicated either, but does look clumsy:
C#
public static string GetRepeatedSequential(int start, int end, int repeat)
    {
    return string.Join(Environment.NewLine,
                       Enumerable.Range(start, end - start + 1)
                                 .SelectMany(i => Enumerable.Repeat($"{i:D4}", repeat)));
    }
 
Share this answer
 
Comments
Maciej Los 25-Jan-21 4:14am    
5ed!
Here is another (linq) solution:

C#
void Main()
{
	
	List<string> lines = new List<string>() {"0001", "0002", "0003"};
	List<string> repeated = RepeteItemNTimes(lines, 3);
	//list is ready to use!
	
}

// Define other methods and classes here
public List<string> RepeteItemNTimes(List<string> inputList, int timesN) 
{
	//create list of N items
	List<int> lst = Enumerable.Range(1, timesN).Select(x=> x).ToList();
	//use cross join 
	List<string> outputList = inputList.SelectMany(il=> lst, (il, l) => il).ToList();
	
	return outputList;
}
 
Share this answer
 
Do a new loop, that repeats the number in question X times. Your concat has no NewLine, so can't give you want you want?
 
Share this answer
 
Comments
Xlance 25-Jan-21 1:19am    
for loops takes 4x time to process than other alternatives.

the concat/enumerate was the fastest, but it's a bit tricky to embed in the right place in the code.
Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900