Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active May 30, 2020 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbvf50mobile/f7eda412aca7cc3758cc52239ffb9650 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/f7eda412aca7cc3758cc52239ffb9650 to your computer and use it in GitHub Desktop.
Just PHP FUN 011.

Just PHP FUN 011.

Started at 30.05.2020 20:57 Saturday May. Finished at 30.05.2020 22:26 Saturday May. (1hr 29minutes)

Why do this kata could be solved just by returning true in PHP? Also I checked tests $this->assertEquals(2, true); will pass too. Could somebody explain why this happens?

UPD: This happens because assertEqual instead of assertSame used. In PHP true == 1 and 1 == true, it casts digit to the boolean. And this is the root why the porblem arrise. https://stackoverflow.com/a/10254238/8574922

<?php
# https://www.codewars.com/kata/5a7893ef0025e9eb50000013 Maximum Gap (Array Series #4).
function maxGap($nums) {
sort($nums); $max = $nums[1] - $nums[0];
for($i = 2; $i < count($nums); $i++)
if($max < $nums[$i] - $nums[$i-1]) $max = $nums[$i] - $nums[$i-1];
return $max;
}
<?php
# https://www.codewars.com/kata/5a4ea304b3bfa89a9900008e Form The Largest.
function maxNumber($n) {
$array = str_split($n);
rsort($array);
return intval(implode($array));
}
<?php
# https://www.codewars.com/kata/5a91a7c5fd8c061367000002 Minimum Steps (Array Series #6).
function minimumSteps($nums, $value) {
sort($nums); $sum = 0;
foreach($nums as $k=>$v){
$sum += $v;
if($value <= $sum) return $k;
}
throw new Exception('Unreachable value.');
}
<?php
# https://www.codewars.com/kata/5a512f6a80eba857280000fc Nth Smallest Element (Array Series #4).
function NthSmallest($arr, $pos)
{
sort($arr);
return $arr[$pos-1];
}
<?php
# https://www.codewars.com/kata/5a905c2157c562994900009d Product Array (Array Series #5).
function productArray($nums) {
$product = array_product($nums);
return array_map(function($x) use ($product){return $product/$x;},$nums);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment