Created
January 26, 2012 02:29
-
-
Save ohpauleez/1680573 to your computer and use it in GitHub Desktop.
Picking out a subject
This file contains hidden or 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
;; This is O(n*m) for all cases, where n is the length of a, and m is the length of b | |
user=> a | |
"8th grade biology" | |
user=> b | |
["chemistry" "biology" "algebra"] | |
user=> (def s (cstr/split a #" ")) | |
#'user/s | |
user=> s | |
["8th" "grade" "biology"] | |
user=> (for [w b | |
ww s | |
:when (.contains w ww)] w) | |
("biology") | |
user=> ;; OR MORE LIKE THE PHP -- O(m) in the best case O(n*m) in the worst | |
user=> (def a "8th grade biology") | |
user=> (def b ["chemistry" "biology" "algebra"]) | |
user=> (reduce (fn [ret w] (if (neg? (.indexOf a w)) ret (conj ret w))) [] b) | |
["biology"] | |
user=> (for [w b :when (> (.indexOf a w) -1)] w) | |
("biology") | |
;;; --- Now let's sort by longest name descending :: sort is timsort, so O(n) best, O(nlogn) | |
;; where n is the length of ret | |
user=> (def a "8th grade microbiology") | |
user=> (def b ["chemistry" "biology" "algebra" "microbiology"]) | |
user=> (def ret (for [w b :when (> (.indexOf a w) -1)] w)) | |
user=> (sort-by count > ret) | |
("microbiology" "biology") |
This file contains hidden or 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
//O(m) in the best case O(n*m) in the worst - where m is the length of b and n is the length of a | |
$a = "8th grade microbiology"; | |
$b = array("chemistry", "biology", "algebra", "microbiology"); | |
$ret_array = array(); | |
foreach($b as $wi => $w) { | |
if (strpos($a, $w)) { | |
$ret_array[] = $w; | |
} | |
} | |
function wCmp($a, $b) | |
{ | |
if ($a == $b) { | |
return 0; | |
} | |
return (strlen($a) > strlen($b)) ? -1 : 1; | |
} | |
// O(nlogn) -- n is the length of ret_array | |
usort($ret_array, 'wCmp'); // Believe it or not, this is destructive | |
/* -- From my PHPSH session | |
php> $a = "8th grade microbiology"; | |
php> $b = array("chemistry", "biology", "algebra", "microbiology"); | |
php> $ret_array = array(); | |
php> foreach($b as $wi => $w) { | |
... if (strpos($a, $w)) { | |
... $ret_array[] = $w; | |
... } | |
... } | |
php> var_dump($ret_array); | |
array(2) { | |
[0]=> | |
string(7) "biology" | |
[1]=> | |
string(12) "microbiology" | |
} | |
php> function wCmp($a, $b) | |
... { | |
... if ($a == $b) { | |
... return 0; | |
... } | |
... return (strlen($a) > strlen($b)) ? -1 : 1; | |
... } | |
php> usort($ret_array, 'wCmp'); // Believe it or not, this is destructive | |
php> var_dump($ret_array); | |
array(2) { | |
[0]=> | |
string(12) "microbiology" | |
[1]=> | |
string(7) "biology" | |
} | |
php> | |
*/ |
This file contains hidden or 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
>>> a = "8th grade microbiology" | |
>>> b = ["chemistry", "biology", "algebra", "microbiology"] | |
>>> ret = [w for w in b if w in a] | |
>>> sorted(ret, key=len, reverse=True) | |
['microbiology', 'biology'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment