Skip to content

Instantly share code, notes, and snippets.

@briedis
Last active August 29, 2015 14:10
Show Gist options
  • Save briedis/44638f1f90f00fcaa8db to your computer and use it in GitHub Desktop.
Save briedis/44638f1f90f00fcaa8db to your computer and use it in GitHub Desktop.
<?php
$userNames = str_split('QWERTYUIJPABCDE');
$forbidden = [
'Q' => 'W',
'P' => 'A',
'U' => 'I',
'D' => 'E',
];
$generateSet = function() use ($userNames){
$randUsers = $userNames;
shuffle($randUsers);
return array_combine($randUsers, $userNames);
};
$isValidSet = function($set) use ($forbidden){
foreach($set as $k=>$v){
if(($k == $v) || (isset($forbidden[$k]) && $forbidden[$k] == $v)){
return false;
}
}
return true;
};
do{
$set = $generateSet();
}while(!$isValidSet($set));
foreach($set as $k => $v){
echo $k . " dāvina " . $v . "<br>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment