-
Notifications
You must be signed in to change notification settings - Fork 2
/
logic.php
43 lines (37 loc) · 1.27 KB
/
logic.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
<?php
echo "| ---------------------------------------------- | \r\n";
echo "| Technical Test Backend Enginer - DOT Indonesia | \r\n";
echo "| Struktur data & Logic | \r\n";
echo "| ---------------------------------------------- | \r\n";
echo "\r\n";
echo "Masukkan nilai array contoh '1,4,5,2,3' separated by comma : \r\n";
$handle = fopen ("php://stdin","r");
$line = fgets($handle);
$input = (!trim($line)) ? [ 2, 1, 6, 9, 9, 4, 3] : explode(',',trim($line));
$unique = [];
/**
* Selection for exist the same value in array
* to make unique value in array
*/
foreach($input as $result){
if(!in_array($result,$unique)){
$unique[] = $result;
}
}
$array = $unique;
for( $i=0; $i < count($array); $i++ ){
for( $j=0; $j < $i; $j++){
if( $array[$j] < $array[$i] ){
$temp = $array[$i]; //swapping values
$array[$i] = $array[$j];
$array[$j] = $temp;
}
}
}
echo "-------------------------------------------------- \r\n";
echo "Array yang di masukkan : ".json_encode($input)."\r\n";
echo "Hasil terbeser ke 2 : ".((!isset($array[1])) ? $array[0] : $array[1])."\r\n";
echo "-------------------------------------------------- \r\n";
fclose($handle);
echo "\n";
echo "Terima Kasih\n";