I am trying to divide samples (numbers) into tests (in the form of intervals). But my results are not correct as per the below required result. The code below is working fine till samples of 20 or less than 20. But if more than 20 samples, the results are wired.
The result should come with this case :
If 19 samples - 2 tests: 1-10, 11-19
If 20 samples - 2 tests: 1-10, 11-20
If 21 samples - 3 tests: 1-10, 11-16, 17-21
If 23 samples - 3 tests: 1-10, 11-17, 18-23
If 24 samples - 3 tests: 1-10, 11-17, 18-24
If 31 samples - 4 tests: 1-10, 11-20, 21–26, 27-31.
<?php
$samples = 23;
$tests = 1;
if($samples > 10) {
$tests = ceil($samples/10);
}
echo $tests." tests\n";
$s = 1
for ($x = 0; $x < $tests; $x++) {
if($samples > 20) {
$c = $s.'-'.ceil($samples/$tests);
}
if($x > 0) {
$c = (ceil($samples/$tests)+1).'-'.$samples;
}
echo $c."\n";
}
?>
Result coming which is wrong:
3 tests : 1-8, 9-23, 9-23
My Answer:
Here is a PHP code snippet that divides samples into tests based on the required intervals:
php
function divideSamplesIntoTests($numSamples) {
$numTests = ceil($numSamples / 10);
$tests = [];
$start = 1;
for ($i = 1; $i <= $numTests; $i++) {
$end = min($start + 9, $numSamples);
$tests[] = "$start-$end";
$start = $end + 1;
}
return $tests;
}
$numSamples = 31;
$tests = divideSamplesIntoTests($numSamples);
echo "If $numSamples samples - " . count($tests) . " tests: " . implode(", ", $tests);
You can use this code snippet by providing the number of samples as input to the `divideSamplesIntoTests` function. It will return an array of tests in the required format.
Rate this post
3 of 5 based on 5748 votesComments