Last active
June 9, 2023 14:09
-
-
Save AlexanderKomkov/bdf52bbafee0b424f82fb3ebb51c4fd0 to your computer and use it in GitHub Desktop.
Задачка сортировка массива
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function sect_sort($array, $start, $length = null) | |
{ | |
$ar_start = []; | |
$ar_sort = []; | |
$ar_end = []; | |
$sort_index = 0; | |
foreach($array as $i => $element) | |
{ | |
if ($i < $start) { | |
$ar_start[] = $element; | |
} elseif (is_null($length) || (is_int($length) && $sort_index < $length)) { | |
$ar_sort[] = $element; | |
$sort_index++; | |
} else { | |
$ar_end[] = $element; | |
} | |
} | |
sort($ar_sort); | |
return array_merge($ar_start, $ar_sort, $ar_end); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://www.codewars.com/kata/58ef87dc4db9b24c6c000092/train/php | |
In this kata you are given an array to sort but you're expected to start sorting from a specific position of the array (in ascending order) and optionally you're given the number of items to sort. | |
Examples: | |
sect_sort([1, 2, 5, 7, 4, 6, 3, 9, 8], 2) //=> [1, 2, 3, 4, 5, 6, 7, 8, 9] | |
sect_sort([9, 7, 4, 2, 5, 3, 1, 8, 6], 2, 5) //=> [9, 7, 1, 2, 3, 4, 5, 8, 6] | |
Documentation: | |
sect_sort($array, $start, $length); | |
array - array to sort | |
start - position to begin sorting | |
length - number of items to sort (optional) | |
if the length argument is not passed or is zero, you sort all items to the right of the start position in the array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment