Last active
August 29, 2015 14:09
-
-
Save devjack/6fe8a1e2e78bf1a2a46c 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 | |
function factorial($n) { | |
if($n==0) return 1; | |
$t=$n--; | |
while ($n > 0) { | |
$t*=($n--); | |
} | |
return $t; | |
} | |
function combinations($n) { | |
$t=0; | |
//echo $t; | |
$nFactorial = factorial($n); | |
for($r=1; $r<=$n; $r++) { | |
//echo " + (".$nFactorial/factorial($n-$r).") "; | |
$t += $nFactorial/factorial($n-$r); | |
} | |
return $t; | |
} | |
for($i=1; $i<10; $i++) { | |
echo "$i => "; | |
echo number_format(combinations($i)); | |
echo PHP_EOL; | |
} | |
function applyBlacklist($blacklistPhrase, $searchWords) { | |
// first sort the terms for skippin gmatches - think selection sort. | |
sort($blacklistPhrase); | |
sort($searchWords); | |
foreach($blacklistPhrase as $blacklistword) { | |
if(!in_array($blacklistword, $searchWords)) { | |
return false; | |
} | |
} | |
// Think in terms of PHP arrays - one liner! | |
return count($blacklistPhrase) == count (array_intersect($blacklistPhrase, $searchWords)); | |
return true; | |
} | |
$tests = array ( | |
array( | |
"name" => "direct match", | |
"blacklist" => "a b c", | |
"search" => "a b c", | |
"result" => true, | |
), | |
array( | |
"name" => "search out of order. Partial search match", | |
"blacklist" => "a b", | |
"search" => "c b a", | |
"result" => true, | |
), | |
array( | |
"name" => "complete mismatch", | |
"blacklist" => "a b c", | |
"search" => " d e f", | |
"result" => false, | |
), | |
array( | |
"name" => "partial match - incomplete", | |
"blacklist" => "a b c", | |
"search" => "a d e f", | |
"result" => false, | |
), | |
); | |
foreach($tests as $case) { | |
$blacklist = explode(' ', $case['blacklist']); | |
$search = explode(' ', $case['search']); | |
$result = applyBlacklist($blacklist, $search); | |
echo $case['name'] . "\t ==> "; | |
var_dump($result == $case['result']); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment