-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathTriangle.php
More file actions
62 lines (49 loc) · 1.03 KB
/
Triangle.php
File metadata and controls
62 lines (49 loc) · 1.03 KB
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
<?php
/**
* Triangle
*
* Determine whether a triangle can be built from a given set of edges.
*/
include '../../Tests.class.php';
function solution($A) {
$sizeOfA = sizeof($A);
if($sizeOfA < 3)
return 0;
sort($A, SORT_NUMERIC);
$i = 0;
$p = null;
$q = $A[$i++];
$r = $A[$i++];
do{
$p = $q;
$q = $r;
$r = $A[$i++];
if($p + $q > $r)
return 1;
}while($i < $sizeOfA);
return 0;
}
$test = new Tests('Triangle');
// example
$A = array(10, 2, 5, 1, 8, 20);
$result = 1;
$test->run(array($A), $result);
// example1
$A = array(10, 50, 5, 1);
$result = 0;
$test->run(array($A), $result);
// example_grouped
$A = array(10, 50, 5, 1);
$result = 0;
$test->run(array($A), $result);
// extreme_single
// 1-element sequence + [5,3,3]
$A = array(5, 3, 3);
$result = 1;
$test->run(array($A), $result);
// extreme_arith_overflow1
// overflow test, 3 MAXINTs + [5,3,3]
$A = array(2147483647, 2147483647, 2147483647);
$result = 1;
$test->run(array($A), $result);
?>