aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--challenge-088/henry-wong/README1
-rw-r--r--challenge-088/henry-wong/php/ch-1.php17
-rw-r--r--challenge-088/henry-wong/php/ch-2.php57
3 files changed, 75 insertions, 0 deletions
diff --git a/challenge-088/henry-wong/README b/challenge-088/henry-wong/README
new file mode 100644
index 0000000000..3d63a6f0be
--- /dev/null
+++ b/challenge-088/henry-wong/README
@@ -0,0 +1 @@
+Solution by Henry Wong
diff --git a/challenge-088/henry-wong/php/ch-1.php b/challenge-088/henry-wong/php/ch-1.php
new file mode 100644
index 0000000000..202052ca76
--- /dev/null
+++ b/challenge-088/henry-wong/php/ch-1.php
@@ -0,0 +1,17 @@
+<?php
+/**
+TASK #1 › Array of Product
+**/
+
+function solution($a) {
+ $b = array();
+ for ($i = 0; $i < count($a); $i++) {
+ $c = $a;
+ unset($c[$i]);
+ $b[] = array_product($c);
+ }
+ return $b;
+}
+
+$a = array(5, 2, 1, 4, 3);
+print_r(solution($a));
diff --git a/challenge-088/henry-wong/php/ch-2.php b/challenge-088/henry-wong/php/ch-2.php
new file mode 100644
index 0000000000..2e639bec68
--- /dev/null
+++ b/challenge-088/henry-wong/php/ch-2.php
@@ -0,0 +1,57 @@
+<?php
+
+# see http://techieme.in/matrix-rotation/
+
+# 0. take row
+# 1. delete row
+# 2. rotate matrix 90 degrees
+# 3. repeat again till no matrix
+
+# Rotate matrix
+# 1. transpose matrix
+# 2. reflect horizontal axis
+
+$a = array(
+ array(1,2,3),
+ array(4,5,6),
+ array(7,8,9)
+);
+
+$c = array(
+ array( 1, 2, 3, 4 ),
+ array(5, 6, 7, 8 ),
+ array(9, 10, 11, 12 ),
+ array(13, 14, 15, 16)
+);
+
+function rotate($a) {
+ # transpose array
+ $b = array();
+ if (isset($a[0]) && is_array($a[0])) {
+ $column_size = count($a[0]);
+ for($i = 0; $i < $column_size; $i++) {
+ $b[] = array_column($a, $i);
+ };
+ # then reflect horizontal axis
+ $b = array_reverse($b);
+
+ }
+ return $b;
+}
+
+function solution($a) {
+ $b = array();
+ $shift = true;
+ while ($shift) {
+ $b = array_merge($b, array_values($a[0]));
+ array_shift($a);
+ if (empty($a)) {
+ $shift = false;
+ }
+ $a = rotate($a);
+ }
+ return $b;
+}
+
+print_r(solution($a));
+print_r(solution($c));