Custom Auto Generate Sequences in C# [duplicate] - c#

This question already has answers here:
Add zero-padding to a string
(6 answers)
C# convert int to string with padding zeros?
(15 answers)
Simple and elegant way to zero-pad a value in C# [duplicate]
(4 answers)
Closed 4 years ago.
So I Have a List, but the list is in integer.
Like 1, 2, 3, 4, 5,...,10,...100,..
And I want to convert that integer to string with format 00000X.
X is represent the integer. So the list will be 000001, 000002, 000010, 000100, etc.

you can achive this by using LINQ. Please check below answer.
List<int> numbers = new List<int> { 1, 2, 3, 4, 5, 6, 7, 10, 100, 1000,10000,99999,100000 };
var mask = "00000";
List<string> stringNumbers = numbers.Select(x =>
{
if (mask.Length > x.ToString().Length)
{
return mask.Substring(0, mask.Length - x.ToString().Length) + x.ToString();
}
else return x.ToString();
}).ToList();

List<string> result = new List<string>();
foreach (int yourNumber in intList) {
result.Add("0000000".Substring(0, 7 - yourNumber.ToString().Length);
}
Sould work.
You also can do in on one line, intList.ForEach(...

Related

Fast Sequence checking in a List<int> [duplicate]

This question already has answers here:
Is there a built-in method to compare collections?
(15 answers)
Functional way to check if array of numbers is sequential
(7 answers)
Closed 4 years ago.
I am looking for a faster and more accurate way to check a Sequence:
List<int> sequence = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7 … 41}
private bool IsSequential(List<int> sequence)
{
int S = sequence[0];
int T = sequence[sequence.Count- 1];
List<int> Possible = Enumerable.Range(S, T).ToList();
List<int> Except = sequence.Except(Possible).ToList();
if (Except.Count == 0)
return true;
else
return false;
}
My code returns 1 if the list is the same, I have some sort of count issue?
I wonder if there is a better way to check an integer sequence: 200, 201, 202... and so on.
Some Sequences may be out of sequence: 200, 210, 203, 204... I need to identify this issue.
Thanks
You can try like following using SequenceEqual.
List<int> sequence = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7 };
bool isInSequence = sequence.SequenceEqual(Enumerable.Range(sequence[0], sequence.Count()));

Find the index of the highest numbers in a list [duplicate]

This question already has answers here:
What is faster in finding element with property of maximum value [duplicate]
(5 answers)
How to get the index of element in the List<T> in c#
(5 answers)
Closed 5 years ago.
I have a list of integers and I want to find out the position of the biggest integer in my list.
List<int> members = new List<int>({312, 33, 122, 3979, 8712, 88})
I tried getting the biggest number by doing int max = members.Max(); and then I tried to get its index like this int highestMember = members.FindIndex(max); but it doesn't work this way
you can try :
List<int> members = new List<int> { 312, 33, 122, 3979, 8712, 8888 };
int a = members.IndexOf(members.Max());

Two double type arrays into one, C# [duplicate]

This question already has answers here:
Merging two arrays in .NET
(26 answers)
Closed 6 years ago.
I'm trying to make a console app with two double type arrays, but i have a problem.
How can i add two arrays into one, but without repeating same number (i have same number in two arrays)?
Thank you very much
Use Union from Linq:
double[] a = new double[] { 1, 2, 3, 4 };
double[] b = new double[] { 3, 4, 5, 6 };
double[] arr = a.Union(b).ToArray();
Variable arr will now contain 1, 2, 3, 4, 5, 6.

How to check same value in two integer Arrays in C# [duplicate]

This question already has answers here:
Comparing two arrays in C#
(11 answers)
Closed 7 years ago.
i can't find same values in two integer arrays. Here is my code
int[] array1 = { 4, 5, 6, 7, 8, 9, 10 };
int[] array2 = { 1, 2, 3, 4, 5, 6 };
How to check same value?
I tried;
IEnumerable<int> k = (from d1 in array1
select d1).Intersect(from d2 in array2
select d2).ToList();
foreach (var item in k)
{
Console.WriteLine(item);
}
It is true, but i don't want to do this way.
Thank you.
Here is a 1 liner:
array1.Intersect(array2).ToList().ForEach(i => Console.WriteLine(i));

Is there an "AllIndexesOf" method? [duplicate]

This question already has answers here:
Getting a collection of index values using a LINQ query
(6 answers)
Closed 9 years ago.
Background:
I'm working on an evaluator (I know there's solutions available, but I need some features that I need to implement myself). I need to find all occurrences of open brackets in the evaluation. However, for that I need all the indexes of the brackets.
Question:
Is there something like an AllIndexesOf method that returns a int[], or IEnumerable<int>?
There is not but you can get all the indexes using the following LINQ query.
int number = 10;
int[] intArray = new[] { 1, 32, 10, 5, 65, 6, 10, 10 };
var allIndexes = intArray.Select((r,i)=> new {value = r, index = i})
.Where(r=> r.value == number)
.Select(r=> r.index);
allIndexes will contain 2,6 and 7
You also can use Enumerable.Range
var indexes = Enumerable.Range(0, list.Count)
.Where(index => list[index] == yourValue);

Categories