diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-11-25 22:30:10 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-11-25 22:30:10 +0000 |
| commit | ab691a2a1d4cb09e6079456ccef39144952bd48d (patch) | |
| tree | 297428b593e92e55382531dc682c118c68b28f0d | |
| parent | da9eb407cf5fb6d9b7b2c30c3a6985c0ee46e8bc (diff) | |
| parent | ce8d37e7a80ba4b15907cd5db241d30707e795cf (diff) | |
| download | perlweeklychallenge-club-ab691a2a1d4cb09e6079456ccef39144952bd48d.tar.gz perlweeklychallenge-club-ab691a2a1d4cb09e6079456ccef39144952bd48d.tar.bz2 perlweeklychallenge-club-ab691a2a1d4cb09e6079456ccef39144952bd48d.zip | |
Merge pull request #2845 from somethingweird/henry-wong-088
C88
| -rw-r--r-- | challenge-088/henry-wong/README | 1 | ||||
| -rw-r--r-- | challenge-088/henry-wong/php/ch-1.php | 17 | ||||
| -rw-r--r-- | challenge-088/henry-wong/php/ch-2.php | 57 |
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)); |
