Conditions are as follows-
- The range of numbers is 1-90, no number should be repeated
- Every row should have exactly 5 numbers
- Every column should have 10 numbers and they should be sorted, the column 1 should have numbers from 1-10, second column should have numbers from 11-20 and so on
- In the grid where there are no number there should be zero.
<?php
function generateTable($rows, $cols, $minNumbersPerRow, $maxNumbersPerRow) {
$table = array_fill(0, $rows, array_fill(0, $cols, 0));
$columnValues = [];
for ($col = 0; $col < $cols; $col++) {
$start = $col * 10 + 1;
$end = $start + 9;
$columnValues[$col] = range($start, $end);
shuffle($columnValues[$col]);
}
for ($col = 0; $col < $cols; $col++) {
$shuffledRows = range(0, $rows - 1);
shuffle($shuffledRows);
foreach ($shuffledRows as $row) {
if (count($columnValues[$col]) > 0) {
$table[$row][$col] = array_shift($columnValues[$col]);
}
}
}
$table = sortTable($table);
return $table;
}
function sortTable($table) {
$sortedTable = $table;
for ($col = 0; $col < count($sortedTable[0]); $col++) {
$columnValues = array_filter(array_column($sortedTable, $col));
sort($columnValues);
$nonZeroIndex = 0;
for ($row = 0; $row < count($sortedTable); $row++) {
if ($sortedTable[$row][$col] != 0) {
$sortedTable[$row][$col] = $columnValues[$nonZeroIndex++];
}
}
}
return $sortedTable;
}
function findMissingNumbers($table) {
$allNumbers = range(1, 90);
$presentNumbers = array_unique(array_merge(...$table));
$missingNumbers = array_diff($allNumbers, $presentNumbers);
return $missingNumbers;
}
function insertMissingNumbers($table, $missingNumbers) {
$updatedTable = $table;
foreach ($missingNumbers as $num) {
$col = intval(($num - 1) / 10);
$emptyRows = array_keys(array_column($updatedTable, $col), 0);
if (!empty($emptyRows)) {
$emptyRow = array_shift($emptyRows);
$updatedTable[$emptyRow][$col] = $num;
}
}
$updatedTable = sortTable($updatedTable);
return $updatedTable;
}
function ensureMinNumbersPerRow($table, $minNumbersPerRow) {
$updatedTable = $table;
foreach ($updatedTable as &$row) {
$nonZeroCount = array_sum(array_map(fn($cell) => $cell != 0, $row));
if ($nonZeroCount < $minNumbersPerRow) {
$emptyCols = array_keys(array_filter($row, fn($cell) => $cell == 0));
while ($nonZeroCount < $minNumbersPerRow && !empty($emptyCols)) {
$colIndex = array_shift($emptyCols);
$rangeStart = $colIndex * 10 + 1;
$rangeEnd = $rangeStart + 9;
$availableNumbers = array_filter(range($rangeStart, $rangeEnd), fn($num) => !in_array($num, array_merge(...$updatedTable)));
if (!empty($availableNumbers)) {
$row[$colIndex] = array_shift($availableNumbers);
$nonZeroCount++;
}
}
} elseif ($nonZeroCount > $minNumbersPerRow) {
$nonZeroIndices = array_keys(array_filter($row, fn($cell) => $cell != 0));
foreach (array_slice($nonZeroIndices, $minNumbersPerRow) as $index) {
$row[$index] = 0;
}
}
}
return $updatedTable;
}
function ensureMinNumbersPerColumn($table, $minNonZeroPerColumn) {
$updatedTable = $table;
$numRows = count($updatedTable);
$numCols = count($updatedTable[0]);
for ($col = 0; $col < $numCols; $col++) {
$columnValues = array_filter(array_column($updatedTable, $col));
$nonZeroCount = count($columnValues);
if ($nonZeroCount < $minNonZeroPerColumn) {
$rangeStart = $col * 10 + 1;
$rangeEnd = $rangeStart + 9;
$availableNumbers = array_filter(range($rangeStart, $rangeEnd), fn($num) => !in_array($num, array_merge(...$updatedTable)));
while ($nonZeroCount < $minNonZeroPerColumn && !empty($availableNumbers)) {
$emptyRow = array_search(0, array_column($updatedTable, $col));
if ($emptyRow === false) {
break; // No empty row available, stop the process
}
$updatedTable[$emptyRow][$col] = array_shift($availableNumbers);
$nonZeroCount++;
}
}
}
return $updatedTable;
}
function printTable($table) {
echo "<table border='1' style='border-collapse: collapse; width: 100%;'>n";
foreach ($table as $rowIndex => $row) {
echo "<tr>n";
foreach ($row as $cell) {
echo "<td style='padding: 8px; text-align: center;'>" . ($cell ?: '0') . "</td>n";
}
echo "</tr>n";
if (($rowIndex + 1) % 3 === 0 && $rowIndex < count($table) - 1) {
echo "<tr><td colspan='" . count($row) . "'> </td></tr>n";
}
}
echo "</table>n";
}
$rows = 18;
$cols = 9;
$minNumbersPerRow = 5;
$maxNumbersPerRow = 5;
$table = generateTable($rows, $cols, $minNumbersPerRow, $maxNumbersPerRow);
$missingNumbers = findMissingNumbers($table);
$table = insertMissingNumbers($table, $missingNumbers);
$table = ensureMinNumbersPerRow($table, $minNumbersPerRow);
$table = ensureMinNumbersPerColumn($table, 10);
$table = sortTable($table);
printTable($table);
?>
New contributor
Aryan Chachra is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.