Skip to content

Instantly share code, notes, and snippets.

@egulhan
Created December 2, 2014 13:25
Show Gist options
  • Select an option

  • Save egulhan/c4e1da2357d5c1e98311 to your computer and use it in GitHub Desktop.

Select an option

Save egulhan/c4e1da2357d5c1e98311 to your computer and use it in GitHub Desktop.
Solution for an algorithm question using PHP
<?php
/*
QUESTION
--------
Verify if the given password is valid/invalid;
1. must be 5-12 characters long
2. must contain atleast one number and one lowercase character
3. a sequence must not be followed by the same sequence (like 123123qs is invalid, 123qs123 is valid)
*/
$password='dasdsa123das';
$r=validatePassword($password);
var_dump($r);
/**
* @date 2014-12-02
*/
function validatePassword($password)
{
if(strlen($password)<5 || strlen($password)>12)
return false;
$letExist=$numExist=false;
for($i=0;$i<strlen($password);$i++)
{
$chr=$password[$i];
if(is_numeric($chr))
$numExist=true;
else if(ord($chr)>=97 && ord($chr)<=122)
$letExist=true;
if($letExist && $numExist)
break;
}
if(!$letExist || !$numExist)
return false;
for($i=0;$i<strlen($password);$i++)
{
for($k=0;$k<strlen($password);$k++)
{
$cur=substr($password,$k,$i+1);
$len=strlen($cur);
$nxt=substr($password,$k+$len,$i+1);
if($cur==$nxt)
false;
}
}
return true;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment