Conditions : There is a square matrix filled with numbers from -10 to 10.

Task : Convert the original matrix so that within each column all negative numbers are always below positive numbers vertically, while respecting their original order in relation to each other.

Example

<?php function squareMatrixToString($squareMatrix) { $result = '<hr>'; $squareMatrixSize = count($squareMatrix); for($i = 0; $i < $squareMatrixSize; $i++) { for($j = 0; $j < $squareMatrixSize; $j++) { $result .= $squareMatrix[$i][$j] . ' '; } $result .= '<br>'; } return $result; } /*** Generate squareMatrix ***/ $squareMatrixSize = 10; $squareMatrix = array(); for($i = 0; $i < $squareMatrixSize; $i++) { for($j = 0; $j < $squareMatrixSize; $j++) { $squareMatrix[$i][$j] = 10 - rand(0, 20); } } echo squareMatrixToString($squareMatrix); // TO DO: echo squareMatrixToString($squareMatrix); ?> //на что хватило for($col = 0; $col < 10; $col++) usort($squareMatrix, $sort = function($a, $b) use ($col){ if($a[$col] === $b[$col]) return 0; return ($a[$col] < $b[$col]) ? 1 : -1; }); 

Closed due to the fact that the essence of the question is incomprehensible by the participants Vladimir Martianov , user194374, Abyx , LEQADA , Saidolim Jan 13 '16 at 17:41 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • That is, each column is sorted independently of others? The matrix has nothing to do with it. - Igor
  • Well, of course, but how? - mnbmnbmnbmmn
  • Where is your question? - Darth
  • @ garjo_099 - Well, you should try to do it yourself even for decency. - Igor
  • @ garjo_099 - Please add code by editing the question, not in the comments. Thank. What does not suit you in the given code? - Igor

2 answers 2

So far, I have come to such a simple and concise code.

Work code:

  /** Задача 3 */ function squareMatrixToString($squareMatrix) { $result = '<hr><table style="text-align: center">'; $squareMatrixSize = count($squareMatrix); for($i = 0; $i < $squareMatrixSize; $i++) { $result .= '<tr>'; for($j = 0; $j < $squareMatrixSize; $j++) { $result .= '<td>'.$squareMatrix[$i][$j] . '</td>'; } $result .= '</tr>'; } return $result .= '</table>'; } /*** Generate squareMatrix ***/ $squareMatrixSize = 10; $squareMatrix = array(); for($i = 0; $i < $squareMatrixSize; $i++) { for($j = 0; $j < $squareMatrixSize; $j++) { $rand = 10 - rand(0, 20); $squareMatrix[$i][$j] = $rand; } } echo squareMatrixToString($squareMatrix); // TO DO: $tmp = []; for($col = 0; $col < $squareMatrixSize; $col++) { $plus = []; $minus = []; for($row = 0; $row < $squareMatrixSize; $row++) { $num = $squareMatrix[$row][$col]; if($num >= 0) $plus[] = $num; else $minus[] = $num; } rsort($minus); $continueRow = count($plus); for($row = 0; $row < $continueRow; $row++) $squareMatrix[$row][$col] = array_shift($plus); $toEnd = count($minus); for($row = $continueRow; $row < $squareMatrixSize; $row++) $squareMatrix[$row][$col] = array_shift($minus); } echo squareMatrixToString($squareMatrix); 
  • Even a bit strange looks. I couldn’t remove the necessary lines from my code to make it work, but I wrote something of my own :-D - Alexey Shimansky
  • made it even easier - phpfiddle.org/main/code/18y6-d4n9 :-) - Alexey Shimansky
  • I would not do it myself until I did not calm down, and your code did not solve the task and, after deleting the lines you wrote, it didn’t do what was necessary. Thanks for the alternative. - mnbmnbmnbmmn
  • how is that? in each column, the positive numbers remain in the same order as before the sorting, and the negative ones line up in descending order immediately below the positive ones. Wasn’t that what the goal was? or just stupidly had to arrange the negative under the positive, everywhere observing the original order? right now, I can correct it ... after all, your code doesn’t meet your requirements either))) - Alexey Shimansky

The solution is straight to the forehead (non-optimized):

 function squareMatrixToString($squareMatrix) { $result = '<table>'; $squareMatrixSize = count($squareMatrix); for($i = 0; $i < $squareMatrixSize; $i++) { $result .= '<tr>'; for($j = 0; $j < $squareMatrixSize; $j++) { $result .= '<td>'; $result .= $squareMatrix[$i][$j] . ' '; $result .= '</td>'; } $result .= '</tr>'; } $result .= '</table>'; $result .= '<hr>'; return $result; } /*** Generate squareMatrix ***/ $squareMatrixSize = 10; $squareMatrix = array(); for($i = 0; $i < $squareMatrixSize; $i++) { for($j = 0; $j < $squareMatrixSize; $j++) { $squareMatrix[$i][$j] = 10 - rand(0, 20); } } echo squareMatrixToString($squareMatrix); $arrSorted = []; for ($i = 0; $i < $squareMatrixSize; ++$i) { $tmp = []; for($j = 0; $j < $squareMatrixSize; $j++) { array_push($tmp,$squareMatrix[$j][$i]); } $plus = array_filter($tmp, function($var){ return $var >= 0; }); $minus = array_filter($tmp, function($var){ return $var < 0; }); array_push($arrSorted, array_merge($plus, $minus)); } $squareMatrix = []; for ($i = 0; $i < $squareMatrixSize; ++$i) { $tmp = []; for($j = 0; $j < $squareMatrixSize; $j++) { array_push($tmp, $arrSorted[$j][$i]); } array_push($squareMatrix, $tmp); } echo squareMatrixToString($squareMatrix); 

http://phpfiddle.org/main/code/x0dr-jbzy


UPD: More concise solution:

 //******************************************** // TODO $result = []; for($i = 0; $i < $squareMatrixSize; $i++) { $col = array_column($squareMatrix, $i); $plus = array_filter($col, function($var){ return $var >= 0; }); $minus = array_filter($col, function($var){ return $var < 0; }); for($j = 0; $j < $squareMatrixSize; $j++) { $result[$j][$i] = $merge[$j]; } } echo squareMatrixToString($result); // //*********************************************** 

http://phpfiddle.org/main/code/vg6x-q9v5

  • Positive must maintain order, you do not have - mnbmnbmnbmmn
  • @ garjo_099 well, I thought I should also sort in ascending order .... well, then comment out / delete the sort($plus, SORT_NUMERIC); $plus = array_values($plus); lines sort($plus, SORT_NUMERIC); $plus = array_values($plus); sort($plus, SORT_NUMERIC); $plus = array_values($plus); and you will be happy))) - Alexey Shimansky
  • doesn't work like that - mnbmnbmnbmmn
  • @ garjo_099 wanna say this doesn't work: phpfiddle.org/main/code/g84f-z5vm ??? - Alexey Shimansky
  • @ garjo_099 I probably was inattentive at night when I read the question ..... I corrected the answer and links to the test. I think now everything is working as it should ..... because your published answer does not seem to correspond to your demand either) - Aleksey Shimansky