Untitled

 avatar
unknown
plain_text
10 months ago
4.2 kB
13
Indexable
<?php
declare(strict_types=1);
?>
<!doctype html>
<html lang="ru">
<head>
    <meta charset="utf-8">
    <title>Пример: таблица умножения, генератор простых, chunkStable</title>
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <style>
        body { font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial; padding: 20px; line-height:1.4; }
        h1,h2 { margin: 0 0 10px 0; }
        .grid { gap:40px; flex-wrap:wrap; }
        table { border-collapse: collapse; min-width: 280px; }
        table, th, td { border: 1px solid #ccc; }
        th, td { padding: 6px 10px; text-align: center; }
        thead th { background:#f3f3f3; position:sticky; top:0; }
        tbody tr:hover td { background:#fafafa; }
        .example { background:#f9f9ff; padding:10px; border-radius:6px; margin-top:8px; }
        pre { background:#111; color:#fff; padding:12px; border-radius:6px; overflow:auto; }
    </style>
</head>
<body>
<h1>1) Таблица умножения (1–10)</h1>

<?php
$min = 1;
$max = 10;
?>
<table aria-label="Таблица умножения 1 по 10">
    <thead>
    <tr>
        <th>x</th>
        <?php for ($j = $min; $j <= $max; $j++): ?>
            <th scope="col"><?= $j ?></th>
        <?php endfor; ?>
    </tr>
    </thead>
    <tbody>
    <?php for ($i = $min; $i <= $max; $i++): ?>
        <tr>
            <th scope="row"><?= $i ?></th>
            <?php for ($j = $min; $j <= $max; $j++): ?>
                <td><?= $i * $j ?></td>
            <?php endfor; ?>
        </tr>
    <?php endfor; ?>
    </tbody>
</table>

<div class="grid" style="margin-top:24px; margin-right: 50%;">
    <div style="min-width:320px; flex:1;">
        <h2>2) Генератор простых чисел — <code>yieldPrimes($max)</code></h2>

        <?php
        function yieldPrimes(int $max): Generator {
            if ($max < 2) {
                throw new DomainException('Аргумент $max должен быть >= 2');
            }
            for ($n = 2; $n <= $max; $n++) {
                $isPrime = true;
                for ($d = 2; $d * $d <= $n; $d++) {
                    if ($n % $d === 0) {
                        $isPrime = false;
                        break;
                    }
                }
                if ($isPrime) {
                    yield $n;
                }
            }
        }

        $maxForDemo = 50;
        $primes = [];
        try {
            foreach (yieldPrimes($maxForDemo) as $p) {
                $primes[] = $p;
            }
        } catch (DomainException $e) {
            $primes = ['error' => $e->getMessage()];
        }
        ?>

        <div class="example">
            <strong>Простые числа до <?= $maxForDemo ?>:</strong>
            <pre><?= implode(' ', $primes) ?></pre>
        </div>

    </div>

    <div style="min-width:320px; flex:1;">
        <h2>3) <code>chunkStable(array $arr, int $size): array</code></h2>

        <?php
        function chunkStable(array $arr, int $size): array {
            if ($size <= 0) {
                throw new InvalidArgumentException('Размер чанка должен быть > 0');
            }
            $result = [];
            $chunk = [];
            $count = 0;
            foreach ($arr as $key => $value) {
                $chunk[$key] = $value;
                $count++;
                if ($count === $size) {
                    $result[] = $chunk;
                    $chunk = [];
                    $count = 0;
                }
            }
            if ($chunk !== []) {
                $result[] = $chunk;
            }
            return $result;
        }

        $arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5, 'f' => 6];
        $chunks = chunkStable($arr, 2);
        ?>

        <div class="example">
            <strong>Исходный массив:</strong>
            <pre><?php print_r($arr); ?></pre>

            <strong>Результат chunkStable(..., 2):</strong>
            <pre><?php print_r($chunks); ?></pre>
        </div>
    </div>
</div>
</body>
</html>
Editor is loading...
Leave a Comment