What is the best possible algorithm for this problem below? I am curious to see the solutions/ideas stackoverflow community can come up with!
Let's assume that a timing register "X" counts up from any random number between 0 and 5000 and then increments unevenly at a random rate. If the timer overshoots 5000, it resets back to 0 and starts the counting process all over again. Each loop can be defined as a cluster and the "legal" clustering boundary is set between 0 to 1000. "Illegal" clusters have data points between 1001 and 5000.
Function
count_time()
can read back the value of the register "X" at any given time.You will need to write an algorithmic function that counts the total legal clusters and will collect all the data points within every cluster. The algorithm will reject data points that is between 1001 and 5000, and will track the "illegal" clusters as well. This algorithmic function can be called as frequently as you want
The code below could be a sample solution skeleton, however, it might not be the best solution out there:
int repeat_every_1ms(void) //this function is called every 1ms
{
static int unsigned count = 0;
static int unsigned cluster = 1;
static int unsigned legal_cluster = 1;
static int unsigned illegal_cluster = 1;
if (count >= count_time()) {
//inside a cluster.it could be either legal or illegal.
cluster++;
count = count_time(); // Line 1
if(count <= 1000) {
legal_cluster++;
/* Start collecting data points (code not given) */
// more statements...
}
count = count_time(); // polling count_time() value beacuse it can be different than Line 1
if(count > 1000) {
illegal_cluster++;
}
}
count = count_time(); //get the final timestamp
return 0;
}
转载于:https://stackoverflow.com/questions/52959152/counting-clusters-in-a-looping-linear-progression-data-in-c