Home > Software engineering >  array shuffle and undo
array shuffle and undo

Time:05-03

as code below,

<?php
// sort array in a fixed order with given seed
function do_shuffle( $array, $seed ) {
    mt_srand( $seed );
    usort( $array, fn() => mt_rand( - 9, 9 ) );
    return $array;
}

// the reverse operation of do_shuffle
function undo_shuffle( $array, $seed ) {
    return $array; // to implement
}

$origin_array = str_split( 'olive tree' );

$seed = 20220502;

$shuffled_array = do_shuffle( $origin_array, $seed );
var_dump( $shuffled_array );

// undo shuffle
$origin_array = undo_shuffle( $shuffled_array, $seed );
var_dump( $origin_array );

how to implement array shuffle and its reverse operation?

how to return $shuffled_array to $origin_array?

CodePudding user response:

The key idea is to recreate a process but shuffle not the array values but array indices. This gives us a mapping between shuffled and original indices:

// the reverse operation of do_shuffle
function undo_shuffle( $array, $seed ) {
    mt_srand( $seed );

    $indices_map = range(0, count($array) - 1);

    // We do just the same shuffle operation but
    // shuffling array indices instead of values
    usort( $indices_map, function(){ 
      return mt_rand( - 9, 9 );
    } );

    // Restore original array using the map we've created
    $out = [];
    foreach( $indices_map as $shuffled_index => $original_index )
      $out[ $original_index ] = $array[ $shuffled_index ];

    // Sort our output by integer key
    ksort($out);

    return $out;
}

  • Related