<?php
function getSuitValue($card) {
$result = 0;
switch($card[1]) {
case 'C' : $result *= 10; break;
case 'H' : $result *= 100; break;
case 'D' : $result *= 1000; break;
default: $result *= 1;
}
return $result;
}
function getValue($card) {
$result = 0;
switch($card[0]) {
case 'A' : $result += 1; break;
case 'J' : $result += 11; break;
case 'Q' : $result += 12; break;
case 'K' : $result += 13; break;
default: $result += (int)$card[0];
}
return $result;
}
function isGreat($card1, $card2) {
$val1 = getValue($card1);
$val2 = getValue($card2);
if ($val1 > $val2) {
return true;
} elseif ($val1 === $val2) {
if (getSuitValue($card1) > getSuitValue($card2)) {
return true;
}
}
return false;
}
function mySort($arr) {
for ($i = 1; $i < count($arr); $i++) {
$x = $arr[$i];
for ($j = $i - 1; $j >= 0 && isGreat($arr[$j], $x); $j--) {
$arr[$j + 1] = $arr[$j];
}
// на оставшееся после сдвига место, ставим $a[$i]
$arr[$j + 1] = $x;
}
return $arr;
}
function sort_cards($cards) {
$array = explode(', ', $cards);
$resultArr = mySort($array);
return implode(', ', $resultArr);
}
echo sort_cards('1C, 5S, 4D'), PHP_EOL;
- Output for 4.3.10 - 4.3.11, 4.4.0 - 4.4.9, 5.0.2 - 5.0.5, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29, 5.4.0 - 5.4.45, 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
- 1C, 4D, 5S
- Output for 4.3.0 - 4.3.9, 5.0.0 - 5.0.1
- 1C, 4D, 5S
Notice: Use of undefined constant PHP_EOL - assumed 'PHP_EOL' in /in/WOIKf on line 60
PHP_EOL
preferences:
286.94 ms | 402 KiB | 341 Q