-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.php
73 lines (66 loc) · 1.51 KB
/
lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
/**
* Return matrix diagonals
*
* @see Docs at https://github.com/phpmx/matrix-diagonals
* @author (at)richistron
* @license MIT
*/
namespace lib\tools;
function getMatrixDiagonals( $matrix = array() ){
// parse position
$parsePosition = function($r,$c){
return "[{$r}][{$c}]";
};
// checamos si la matríz es cuadra
$cols = count($matrix);
foreach($matrix as $key => $row) {
$rows = count($row);
if($rows != $cols){
echo "la raíz no es cuadrada"; exit;
}
}
// results array
$results = array();
// fisrt loop
$r = 0;
for ($c = 0; $c < $cols; $c++) {
$position = array();
$values = array();
$position[] = $parsePosition($r,$c);
$values[] = $matrix[$r][$c];
if($c > 0){
for($i = $c; $i > 0; $i--){
$x = $i - 1;
$y = $c - $i + 1;
$position[] = $parsePosition($y,$x);
$values[] = $matrix[$y][$x];
}
}
//
$results["position"][] = implode(" ", $position);
$results["values"][] = implode(" ", $values);
}
// Secod loop
$r = $r + 1;
$cc = $c;
for($i = $r; $i < $cols; $i++){
$position = array();
$values = array();
$y = $i;
$x = $c - 1;
$position[] = $parsePosition($y,$x);
$values[] = $matrix[$y][$x];
for($j = ($x - $y); $j > 0; $j--){
$y = $c - $j;
$x = $cc - $y;
$position[] = $parsePosition($y,$x);
$values[] = $matrix[$y][$x];
}
$results["position"][] = implode(" ", $position);
$results["values"][] = implode(" ", $values);
$cc++;
}
// print results
return $results;
}