在 PHP 中猜测密码的递归函数?
Recursive function for guessing password in PHP?
我需要编写一个可以猜测 4 个字符长密码的递归函数。
我正在使用它来生成随机 "password":
/*start random password generating code*/
$random_password = "";
$charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for($i = 0; $i < 4; $i++){
$random_int = mt_rand();
$random_password .= $charset[$random_int % strlen($charset)];
}
echo "The password to guess is: " . $random_password . "\n";
/*end random password generating code*/
我可以很容易地做同样的事情,并使用 while 循环检查它是否与密码匹配,但这个练习是用递归函数来完成的。
我该怎么做?我之前的练习只是计算斐波那契数,但这有点过头了。
我应该这样做吗:
$counter = array(1, 0, 0, 0);
function enumurate(){
global $counter;
if($counter[0] != 0){
echo $counter[0];
if($counter[1] != 0){
echo $counter[1];
if($counter[2] != 0){
echo $counter[2];
if($counter[3] != 0){
echo $counter[3];
}else{
echo 'error! $counter[3] is: ' . $counter[3];
}
}else{
echo 'error! $counter[2] is: ' . $counter[2];
}
}else{
echo 'error! $counter[1] is: ' . $counter[1];
}
}else{
echo 'error! $counter[0] is: ' . $counter[0];
}
}
enumurate();
我相信我正在寻找的东西是位移中的东西(在我迭代 62 次后 [26 + 26 + 10= lowercase + UPPERCASE + numerical])然后再次递归调用该函数但我在亏了。
还是我想多了?
PS:我确实在 google 上寻找过它,但出于某种原因,我找不到任何关于匹配字符串枚举的递归函数的具体信息,也无法在此处进行检查。不过我的 1337 搜索技巧可能让我失望了。
猜测您刚刚生成的密码似乎有点无用...您为什么不 "guess" 那个密码?此外,只生成所有可能的 4 个字母的单词是没有乐趣的,直到它等于您从一开始就知道的密码。谈论低效代码...
为了让这变得不那么琐碎,我假设需要猜测密码的函数没有将密码作为参数(因为它可能只是 return 作为唯一正确的猜测),而是会得到一个回调函数,它可以调用它来验证以下哪一项为真:
- 猜测完全正确:回调将return值2
- 猜测与密码的第一个字符匹配,但需要更多字符才能完全匹配:回调将 return 在这种情况下为 1
- 所有其他情况:回调将 return 0
回调函数将如下所示:
function ($guess) use ($random_password) {
if ($guess === $random_password) return 2; // it is correct!
if (substr($random_password, 0, strlen($guess)) === $guess) return 1; // partial match;
return 0; // the password does not start with the guessed characters
}
有了这个设置,挑战就更有趣了,因为不允许解决方案偷看它需要猜测的密码。
这里是递归解决方案(根据您的函数生成随机密码):
function getRandomPassword() { // Your function
$random_password = "";
$charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for($i = 0; $i < 4; $i++){
$random_int = mt_rand();
$random_password .= $charset[$random_int % strlen($charset)];
}
return $random_password;
}
function findPassword($evaluateGuess) {
$charset = str_split("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
function guessTheRest(&$evaluateGuess, &$charset, $password) {
$match = $evaluateGuess($password); // call the callback function
if ($match === 0) return; // mismatch
if ($match === 2) return $password; // all characters correct
foreach ($charset as $char) { // taking each character as a "guess"
$guess = guessTheRest($evaluateGuess, $charset, $password . $char);
if ($guess) return $guess;
}
}
return guessTheRest($evaluateGuess, $charset, "");
}
$random_password = getRandomPassword();
echo "The password to guess is: $random_password\n";
$guess = findPassword(function ($guess) use ($random_password) {
if ($guess === $random_password) return 2; // it is correct!
if (substr($random_password, 0, strlen($guess)) === $guess) return 1; // partial match;
return 0; // the password does not start with the guessed characters
});
echo "guessed $guess\n";
在 repl.it 上查看 运行。
我需要编写一个可以猜测 4 个字符长密码的递归函数。 我正在使用它来生成随机 "password":
/*start random password generating code*/
$random_password = "";
$charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for($i = 0; $i < 4; $i++){
$random_int = mt_rand();
$random_password .= $charset[$random_int % strlen($charset)];
}
echo "The password to guess is: " . $random_password . "\n";
/*end random password generating code*/
我可以很容易地做同样的事情,并使用 while 循环检查它是否与密码匹配,但这个练习是用递归函数来完成的。
我该怎么做?我之前的练习只是计算斐波那契数,但这有点过头了。
我应该这样做吗:
$counter = array(1, 0, 0, 0);
function enumurate(){
global $counter;
if($counter[0] != 0){
echo $counter[0];
if($counter[1] != 0){
echo $counter[1];
if($counter[2] != 0){
echo $counter[2];
if($counter[3] != 0){
echo $counter[3];
}else{
echo 'error! $counter[3] is: ' . $counter[3];
}
}else{
echo 'error! $counter[2] is: ' . $counter[2];
}
}else{
echo 'error! $counter[1] is: ' . $counter[1];
}
}else{
echo 'error! $counter[0] is: ' . $counter[0];
}
}
enumurate();
我相信我正在寻找的东西是位移中的东西(在我迭代 62 次后 [26 + 26 + 10= lowercase + UPPERCASE + numerical])然后再次递归调用该函数但我在亏了。
还是我想多了?
PS:我确实在 google 上寻找过它,但出于某种原因,我找不到任何关于匹配字符串枚举的递归函数的具体信息,也无法在此处进行检查。不过我的 1337 搜索技巧可能让我失望了。
猜测您刚刚生成的密码似乎有点无用...您为什么不 "guess" 那个密码?此外,只生成所有可能的 4 个字母的单词是没有乐趣的,直到它等于您从一开始就知道的密码。谈论低效代码...
为了让这变得不那么琐碎,我假设需要猜测密码的函数没有将密码作为参数(因为它可能只是 return 作为唯一正确的猜测),而是会得到一个回调函数,它可以调用它来验证以下哪一项为真:
- 猜测完全正确:回调将return值2
- 猜测与密码的第一个字符匹配,但需要更多字符才能完全匹配:回调将 return 在这种情况下为 1
- 所有其他情况:回调将 return 0
回调函数将如下所示:
function ($guess) use ($random_password) {
if ($guess === $random_password) return 2; // it is correct!
if (substr($random_password, 0, strlen($guess)) === $guess) return 1; // partial match;
return 0; // the password does not start with the guessed characters
}
有了这个设置,挑战就更有趣了,因为不允许解决方案偷看它需要猜测的密码。
这里是递归解决方案(根据您的函数生成随机密码):
function getRandomPassword() { // Your function
$random_password = "";
$charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for($i = 0; $i < 4; $i++){
$random_int = mt_rand();
$random_password .= $charset[$random_int % strlen($charset)];
}
return $random_password;
}
function findPassword($evaluateGuess) {
$charset = str_split("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
function guessTheRest(&$evaluateGuess, &$charset, $password) {
$match = $evaluateGuess($password); // call the callback function
if ($match === 0) return; // mismatch
if ($match === 2) return $password; // all characters correct
foreach ($charset as $char) { // taking each character as a "guess"
$guess = guessTheRest($evaluateGuess, $charset, $password . $char);
if ($guess) return $guess;
}
}
return guessTheRest($evaluateGuess, $charset, "");
}
$random_password = getRandomPassword();
echo "The password to guess is: $random_password\n";
$guess = findPassword(function ($guess) use ($random_password) {
if ($guess === $random_password) return 2; // it is correct!
if (substr($random_password, 0, strlen($guess)) === $guess) return 1; // partial match;
return 0; // the password does not start with the guessed characters
});
echo "guessed $guess\n";
在 repl.it 上查看 运行。