Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active May 21, 2020 13:41
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/8a86ed1b9cbcd515106e2a9fbafdad21 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/8a86ed1b9cbcd515106e2a9fbafdad21 to your computer and use it in GitHub Desktop.
Just PHP FUN 003.
<?php
# https://www.codewars.com/kata/580a0347430590220e000091 Find the area of the rectangle!
function area($diagonal, $side) {
if($diagonal <= $side) return "Not a rectangle";
return round($side * sqrt(pow($diagonal,2) - pow($side,2)),2);
}
<?php
# https://www.codewars.com/kata/5ac54bcbb925d9b437000001 Find the Middle of the Product.
function findMiddle($str){
if(!is_string($str)){
echo "Got it!\n";
return -1;
}
echo "Hi: $str\n";
var_dump($str);
$str = preg_replace('/[^0-9]/',"",$str);
var_dump($str);
if(empty($str)) return -1;
$str = strval(array_reduce(
str_split($str),
function($acc,$v){ return $acc * $v;},
1
));
$size = strlen($str);
if(1 == $size%2) return substr($str, floor($size/2), 1);
$str = substr($str, $size/2-1, 2);
return preg_replace('/^0/',"",$str);
}
<?php
# https://www.codewars.com/kata/56b7f2f3f18876033f000307 Are the numbers in order?
function in_asc_order($arr) {
for($i = 1; $i < count($arr); $i += 1)
if($arr[$i-1] > $arr[$i]) return false;
return true;
}

Just PHP FUN 003.

Started at 21.05.2020 Thursday May 19:14.
Finished at 21.05.2020 Thursday May 20:40. Total 1hr 26min.

<?php
# https://www.codewars.com/kata/5413759479ba273f8100003d esreveR.
function reverse(array $a): array {
for($i=0,$j=count($a)-1; $i < $j; $i += 1, $j -=1){
list($a[$i],$a[$j]) = [$a[$j],$a[$i]];
}
return $a;
}
<?php
/*
206. Reverse Linked List.
https://leetcode.com/problems/reverse-linked-list/
Runtime: 8 ms, faster than 70.00% of PHP online submissions for Reverse Linked List.
Memory Usage: 16.6 MB, less than 50.00% of PHP online submissions for Reverse Linked Li
*/
/**
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val = 0, $next = null) {
* $this->val = $val;
* $this->next = $next;
* }
* }
*/
class Solution {
/**
* @param ListNode $head
* @return ListNode
*/
function reverseList($head) {
$tmp = null;
$prv = null;
while($head){
$tmp = $head->next;
$head->next = $prv;
$prv = $head;
$head = $tmp;
}
return $prv;
}
}
<?php
# https://www.codewars.com/kata/5d49c93d089c6e000ff8428c Computer problem series #1: Fill the Hard Disk Drive.
function save($sizes, $hd) {
$sum = 0; $count = 0;
foreach($sizes as $size){
$sum += $size;
if($hd < $sum) return $count;
$count += 1;
}
return $count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment