Skip to content

Instantly share code, notes, and snippets.

@briedis
Created November 22, 2014 09:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briedis/7c5d7da87b6612355e8b to your computer and use it in GitHub Desktop.
Save briedis/7c5d7da87b6612355e8b 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) || (isset($forbidden[$v]) && $forbidden[$v] == $k)){
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