jeudi 22 avril 2021

Getting consecutive numbers without repetition

From a random sequence of numbers {"1", "2", "3", "4", "5"}, it is asked to determine the number of times that groups of 3 consecutive numbers appear, it means, the number of times that have been generated with the base = c ("1", "2", "3", "4", "5") any of the following groups {"123", "234", "345" }.

# I undertand that I have generate a sample with 5 numbers
a<-c(sample(1:5,5))
a
#I generated the list, as you can see I didn't fix a seed because I know that in every single sequence I will have differents grupos of 3 consecutive numbers, so I should obtain something like this

b<-c(2,4,5,1,3) #this example gives me just one that it would be {2,4,5}
b
#answer expected
1
#Then, I don't know how to obtain the sequence I have tried with permutations and combinations but I don't get it.




Aucun commentaire:

Enregistrer un commentaire