0
I have a List<int>
:
[2,3,5,4,6]
Although not in order, it is a sequence of numbers (2,3,4,5,6) How do I validate this?
Note: There will always be 5 positions and the numbers will be from 1 to 6
0
I have a List<int>
:
[2,3,5,4,6]
Although not in order, it is a sequence of numbers (2,3,4,5,6) How do I validate this?
Note: There will always be 5 positions and the numbers will be from 1 to 6
1
You can use the SequenceEqual
next to the OrderBy
. Supposing your list calls lista
, see how it would look:
lista.SequenceEqual(lista.OrderBy(x => x))
This line will return a Boolean saying whether it is ordered or not.
See working on Dotnetfiddle.
0
You can use the following function:
public bool hasNumberSequence(List<int> numbers)
{
numbers.Sort();
int numerosSequenciais = 0;
for (int i = 1; i < numbers.Count(); i++)
{
if (numbers[i] != (numbers[i - 1] + 1))
numerosSequenciais = 0;
else
numerosSequenciais++;
}
return numerosSequenciais >= 4;
}
This function returns true
in case there is a sequence of at least 4 numbers in the list of int
and false
if there is no.
Examples of Use
List<int> numeros = new List<int>() { 0, 3, 5, 4, 6 };
Console.WriteLine(hasNumberSequence().ToString(numeros)); // True
numeros = new List<int>() { 2, 3, 5, 4, 6 };
Console.WriteLine(hasNumberSequence(numeros).ToString()); // True
numeros = new List<int>() { 2, 1, 0, 4, 6 };
Console.WriteLine(hasNumberSequence(numeros).ToString()); // False
OK Roberto, with this function he brings me true if the 5 positions are in a sequence, this part beauty...but also need another logic that if you have 4 positions in the sequence he returns me true too...example [2,3,1,4,6] that in this case are the numbers 1,2,3,4...understood? Hug
@Arthur changed the answer according to his explanation in the comments, see if it solves.
Thanks for the strength, but I’m doing some tests here and this list for example is not accepting as a sequence with 4 numbers [1,2,3,3,4], what I understood is that when it arrives at the second number 3 it Zera the count. Can you help me with that please?
also did another simple test passing [1,2,3,4,5] and it is returning me only 4 sequential numbers, in which case would have to go back 5 right?
Browser other questions tagged c# asp.net
You are not signed in. Login or sign up in order to post.
Hello. Welcome to Sopt. your question is not very clear. What exactly do you want to validate? if the numbers in there are between 1 and 6? Establish a sequence (even outside the order)? ...
– Diego Rafael Souza
@Diegorafaelsouza, I think in that sentence:
Por mais que não estejam na ordem, ele é uma sequencia de numeros (2,3,4,5,6) Como faço para validar isso?
he makes that very clear.– Roberto de Campos
@Robertodecampos is actually exactly where he got confused. He made two statements:
é uma sequência
andé de 1 a 6
. If it is to establish a rule, the correct one would bedevem ser uma sequência
anddevem ser de 1 a 6
(which limits it to two valid possibilities: 1,2,3,4,5 OR 2,3,4,5,6). With your edition you changed the question to reflect your interpretation. It may be that it is correct. Let’s see if that’s what he meant– Diego Rafael Souza
I did not edit to change the meaning of the question, I only made more visible what was already separated, it was just an observation.
– Roberto de Campos
@Robertodecampos In the post that he did in the OR I think he tried to detail more, but for me (and apparently for others as well) what he wants remains a mystery.
– Diego Rafael Souza
Good evening guys, yeah, that’s a mystery, but I’ll try to explain it better around here... I have a list with 5 random numbers from 1 to 6...and I have 2 sequence rules that I need to know.... for example: Rule 1 - have at least 4 numbers in numerical order...that would be that [1,2,3,4,6] or that too [2,1,3,4,6]...and Rule 2 - have the 5 numbers in numerical order...that could be that [1,2,3,4,5] or that [2,3,4,5,6] or that [6,2,3,4,5].... Did the explanation get better?? I already appreciate all your help...hug
– Dev xablix