<?php
$hotels = [
['Cala', 3, 8.7, 6000],
['Ocean', 3, 8.2, 6150],
['Orchid', 3, 8.0, 7200],
['Ya', 4, 8.3, 6950],
['Chaba', 3, 8.2, 7120],
];
$top = [
'Ocean',
'Ya',
'Chaba',
];
for ( $i=0; $i<count($hotels); $i++ ) {
$hotels[$i][4] = 0;
}
var_dump(buildTop($hotels, 0.2, 2000));
function buildTop($hotels, $starCoef, $ratingCoef)
{
for ( $i=0; $i<count($hotels); $i++ ) {
for ( $j=$i+1; $j<count($hotels); $j++ ) {
$starDifference = $hotels[$i][1]-$hotels[$j][1];
$ratingToCompensate = $starDifference*$starCoef;
$rating = $hotels[$i][2]+$ratingToCompensate;
$ratingDifference = ($rating-$hotels[$j][2])*10;
$priceToCompensate = $ratingDifference*$ratingCoef;
$price = round($hotels[$i][3]-$priceToCompensate);
if ( $price < $hotels[$j][3] ) {
$hotels[$i][4]++;
} else {
$hotels[$j][4]++;
}
}
}
usort($hotels, function($hotelA, $hotelB){
return $hotelB[4]-$hotelA[4];
});
return array_map(function($hotel){
return $hotel[0];
}, $hotels);
}
preferences:
39.68 ms | 402 KiB | 5 Q