Pseudo-codice per shelf-stacking

voti
2

Supponiamo che io sono alcuni elementi in serie numerate che sono le unità 1-n di larghezza, che devono essere visualizzati in righe. Ogni riga è m unità larghe. Ho bisogno di pseudo-codice che si uscita le righe, per me, in modo che il limite m larghezza viene mantenuto. Questo non è un problema di zaino, come gli articoli devono rimanere per numero di serie - spazi vuoti alla fine di righe sono soddisfacenti.

Ho dato la caccia la coda su questo, in parte perché ne ho bisogno sia in PHP e jQuery / javascript, da qui la richiesta di pseudo-codice ....

È pubblicato 03/08/2009 alle 16:43
fonte dall'utente
In altre lingue...                            


4 risposte

voti
3

while (!items.isEmpty()) {
  rowRemain = m;
  rowContents = [];
  while (!items.isEmpty() && rowRemain > items[0].width) {
    i = items.shift();
    rowRemain -= i.width
    rowContents.push(i);
  }
  rows.push(rowContents);
}

tempo di esecuzione è Θ (numero di elementi)

Risposto il 03/08/2009 a 16:47
fonte dall'utente

voti
0

Modulus è tuo amico. Vorrei fare qualcosa di simile:

$items = array(/* Your list of stuff */);
$count = 0;
$maxUnitsPerRow = 4; // Your "m" above

while ($item = $items[$count]) {
 if ($count % $maxUnitsPerRow == 0) {
    $row = new row();
 }
$row->addItemToRow($item);
$count++;
}
Risposto il 03/08/2009 a 16:47
fonte dall'utente

voti
0

Per quel che vale, penso di avere quello che stavo cercando, per PHP - ma non sono sicuro se c'è un modo più semplice ...

<?php
// working with just a simple array of widths...
$items     = array(1,1,1,2,1,1,2,1);
$row_width = 0;
$max_width = 2;

echo "Begin\n"; // begin first row
foreach($items as $item=>$item_width) {
  // can we add item_width to row without going over?
  $row_width += $item_width;
  if($row_width < $max_width) {
    echo "$item_width ";
  } else if($row_width == $max_width) {
    echo "$item_width";
    echo "\nEnd\nBegin\n"; // end last row, begin new row
    $row_width = 0;
  } else if($row_width == 2* $max_width) {
    echo "\nEnd\nBegin\n"; // end last row, begin new row
    echo "$item_width";
    echo "\nEnd\n"; // end new row
    $row_width = 0;
    if($item < count($items)) echo "Begin\n"; // new row
  } else if($row_width > $max_width) {
    echo "\nEnd\nBegin\n"; // end last row, begin new row
    echo "$item_width";
    $row_width = $item_width;
  }
}
echo "\nEnd\n"; // end last row

?>
Risposto il 03/08/2009 a 17:06
fonte dall'utente

voti
0

Ecco un codice PHP alternativa ...

function arrayMaxWidthString($items, $maxWidth) {
    $out = array();
    if (empty($items)) {
        return $out;
    }

    $row = $maxWidth;
    $i = 0;

    $item = array_shift($items);
    $row -= strlen($item);
    $out[0] = $item;

    foreach ($items as $item) {
        $l = strlen($item);
        $tmp = ($l + 1);
        if ($row >= $tmp) {
            $row -= $tmp;
            $out[$i] = (($row !== $maxWidth) ? $out[$i] . ' ' : '') . $item;
        } elseif ($row === $maxWidth) {
            $out[$i] = $item;
            ++$i;
        } else {
            ++$i;
            $row = $maxWidth - $l;
            $out[$i] = $item;
        }
    }
    return $out;
}
Risposto il 03/08/2009 a 17:46
fonte dall'utente

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more