aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordrbaggy <js5@sanger.ac.uk>2021-08-05 09:14:10 +0100
committerdrbaggy <js5@sanger.ac.uk>2021-08-05 09:14:10 +0100
commit01d189f6488f5f14c8f54986bec0f0ce0158bba5 (patch)
tree3ad762246198f38d0602a1ab07e92320536fe07a
parent8b1aec66b256f143030fd16ed4759187973c54cd (diff)
downloadperlweeklychallenge-club-01d189f6488f5f14c8f54986bec0f0ce0158bba5.tar.gz
perlweeklychallenge-club-01d189f6488f5f14c8f54986bec0f0ce0158bba5.tar.bz2
perlweeklychallenge-club-01d189f6488f5f14c8f54986bec0f0ce0158bba5.zip
added PHP version of challenge 2
-rw-r--r--challenge-124/james-smith/php/ch-2.php23
1 files changed, 23 insertions, 0 deletions
diff --git a/challenge-124/james-smith/php/ch-2.php b/challenge-124/james-smith/php/ch-2.php
new file mode 100644
index 0000000000..0696562330
--- /dev/null
+++ b/challenge-124/james-smith/php/ch-2.php
@@ -0,0 +1,23 @@
+<?php
+
+echo match_teams([1,2,3,4,5,6,7,8,9,10]);
+echo match_teams([1,2,3,4,5,6,7,8,9]);
+echo match_teams([1,1,1,1,1,1,3,1,10]);
+
+function match_teams( $teams ) {
+ $d = array_shift( $teams );
+ $bt =[1e99];
+ separate( 1 + floor( sizeof($teams)/2 ), [$d], [], $d, $bt, $teams );
+ return sprintf( "T1: [%s]; T2: [%s]; Diff %d\n",
+ implode( ' ', $bt[1] ), implode( ' ', $bt[2] ), $bt[0] );
+}
+
+function separate( $m, $t1, $t2, $d, &$b, $w ) {
+ if(!sizeof($w)) {
+ if( $b[0]>abs($d) ) list($b[0],$b[1],$b[2]) = [abs($d),$t1,$t2];
+ return;
+ }
+ $n = array_shift( $w );
+ if( sizeof($t1)<$m ) separate( $m, array_merge($t1,[$n]), $t2, $d+$n, $b, $w );
+ if( sizeof($t2)<$m ) separate( $m, $t1, array_merge($t2,[$n]), $d-$n, $b, $w );
+}