Created
November 22, 2014 09:10
-
-
Save briedis/7c5d7da87b6612355e8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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