Skip to content

Instantly share code, notes, and snippets.

@devjack
Last active August 29, 2015 14:09
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 devjack/6fe8a1e2e78bf1a2a46c to your computer and use it in GitHub Desktop.
Save devjack/6fe8a1e2e78bf1a2a46c to your computer and use it in GitHub Desktop.
<?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