Ver Mensaje Individual
  #5 (permalink)  
Antiguo 14/04/2014, 15:09
Axo
 
Fecha de Ingreso: abril-2003
Ubicación: Virtual
Mensajes: 953
Antigüedad: 23 años, 5 meses
Puntos: 7
Respuesta: Cómo mezclar X palabras sin que se repita la combinación

Código PHP:

function pc_next_permutation($p, $size) {
    
// slide down the array looking for where we're smaller than the next guy
    
for ($i = $size - 1; $p[$i] >= $p[$i+1]; --$i) { }

    
// if this doesn't occur, we've finished our permutations
    // the array is reversed: (1, 2, 3, 4) => (4, 3, 2, 1)
    
if ($i == -1) { return false; }

    
// slide down the array looking for a bigger number than what we found before
    
for ($j = $size; $p[$j] <= $p[$i]; --$j) { }

    
// swap them
    
$tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;

    
// now reverse the elements in between by swapping the ends
    
for (++$i, $j = $size; $i < $j; ++$i, --$j) {
         
$tmp = $p[$i]; $p[$i] = $p[$j]; $p[$j] = $tmp;
    }

    return 
$p;
}

$set = split(' ', 'diseno web madrid'); // like array('she', 'sells', 'seashells')
$size = count($set) - 1;
$perm = range(0, $size);
$j = 0;

do { 
     foreach (
$perm as $i) { $perms[$j][] = $set[$i]; }
} while (
$perm = pc_next_permutation($perm, $size) and ++$j);

foreach (
$perms as $p) {
    print 
join(' - ', $p) . "\n";
} 
Me da esto:

diseno web madrid
diseno madrid web
web diseno madrid
web madrid diseno
madrid diseno web
madrid web diseno

Y necesitaría que me diera esto:

diseno web
diseno madrid
madrid web
diseno web madrid (aunque esta me da igual, ya que con un explode lo tengo rápido)

Estoy viendo a ver si puedo modificarla pero se me escapa.