
Originally Posted by
Pullo
Hi there,
Here's my solution.
I'm still learning PHP (normally I work in Ruby), so I would be really grateful for any constructive criticism / pointers the rest of you could give me.
Hi! I'm still learning PHP too. Your solution is good. Thank a lot!

Originally Posted by
Cups
Do you know for sure that $start (or $end) will always contain a fixed set of keys/values? Or a set of fixed keys/values you can divine/compute?
$start (or $end) is not contain a fixed set of keys and values, it's based on database. Each subarray is a transaction (or an order).
And here is my solution:
main.php
PHP Code:
for($i=0; $<count($database); $i++){
$transaction[] = getTransaction($candidates, $database);
}
PHP Code:
<?php
function minus_sets($a, $b){
// number candidates in database
$number_cans = count($a);
// number transactions database
$number_trans = count($b);
for($i=0; $i<count($a); $i++){
$same= 0;
for($j=0; $j<count($b); $j++){
if($a[$i]==$b[$j]) $same= 1;
}
if(!$same){
$tmp = $tmp + 1;
$c[$tmp-1] = $a[$i];
}
}
return $c;
}
//demo
//$a = array(1, 2, 3, 4);
//$b = array(2, 3, 4);
//$minus = minus_set($a, $b);
//print_r($minus);
//result is Array ( [0] => 1 )
?>
PHP Code:
<?php
include('minus_sets.php');
function getTransaction($candidates, $database){
// $candidates minus $database to take elements not in $database
$tmp = minus_sets($candidates, $database);
for($i=0; $i<count($candidates); $i++){
if(!in_array($candidates[$i], $tmp)){
$transaction[$candidates[$i]] = 1;
} else {
$transaction[$candidates[$i]] = 0;
}
}
return $transaction;
}
//$candidates = array('a', 'b', 'c', 'd', 'e');
//$database = array('a', 'b', 'c');
//$transaction = getTransaction($candidates, $database);
//print_r($transaction);
// result is Array ( [a] => 1 [b] => 1 [c] => 1 [d] => 0 [e] => 0 )
?>
What do you think about my solution?
Bookmarks