Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active July 27, 2020 18:31
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/6369945fb5cd076ab0904a710cfb0b4f to your computer and use it in GitHub Desktop.
Save lbvf50mobile/6369945fb5cd076ab0904a710cfb0b4f to your computer and use it in GitHub Desktop.
Just PHP FUN 060.
<?php
# https://www.codewars.com/kata/576bb3c4b1abc497ec000065 Compare Strings by Sum of Chars.
function compare($s1, $s2) {
$size = function($x){
if(!is_string($x)) return 0;
if(preg_match('/[^a-z]/i',$x)) return 0;
return array_sum(array_map('ord',str_split(strtoupper($x))));
};
return $size($s1) == $size($s2);
}
<?php
# https://www.codewars.com/kata/5827bc50f524dd029d0005f2 Coding Meetup #4 - Higher-Order Functions Series - Find the first Python developer.
function get_first_python($a) {
$x = array_values(array_filter($a,function($x){ return "Python" == $x['language'];}));
if(empty($x)) return "There will be no Python developers";
return $x[0]["first_name"].", ".$x[0]["country"];
}
<?php
# https://www.codewars.com/kata/5810d8ce2956d78698000032 Find the K-th last element of a singly linked list.
/*
Predefined class for list nodes:
class Node {
public $data, $next;
public function __construct($data, $next = NULL) {
$this->data = $data;
$this->next = $next;
}
}
*/
function get_kth_last_element($head, $k) {
$size = 0; $tmp = $head; $i = 0;
while($tmp){$size += 1; $tmp = $tmp->next;}
$steps = $size - 1 - $k;
if(0 > $steps || $steps >= $size ) return false;
while($head && $i < $steps){ $i += 1; $head = $head->next;}
if($head) return $head;
throw new Exception('There is no head.');
}
<?php
# https://www.codewars.com/kata/582afcadac2d9baa0900054c Pokerhand, straight or not?
// return true/false if the given cards make up a straight
function isStraight($c){
$a = $c;
$b = $c;
for($i=0; $i < count($b); $i++) if(14 == $b[$i]) $b[$i] = 1;
sort($a); sort($b); $a = array_values(array_unique($a)); $b = array_values(array_unique($b));
return test($a) || test($b);
}
function test($x){
for($i=0; $i < count($x); $i++) if(five(array_slice($x,$i,5))) return true;
return false;
}
function five($x){
if(count($x) < 5) return false;
for($i=1; $i<5; $i++) if($x[$i-1]+1 != $x[$i]) return false;
return true;
}
#-------------------------------------------------------------------------------------------
function isStraight($cards){
echo "Import: ".implode(",",$cards)."\n";
for($i = 0; $i < count($cards); $i+=1) if(14 == $cards[$i]) $cards[$i] = 1;
sort($cards);
echo "Sorted: ".implode(",",$cards)."\n";
$valid = function($x){
if(5 != count($x)) return false;
echo "test: ".implode(",",$x)."\n";
for($i=1; $i < 5; $i++) if($x[$i-1] + 1 != $x[$i]) return false;
return true;
};
while(!empty($cards)){
if($valid(array_slice($cards,0,5))){
echo "REturn true\n";
return true;}
$cards = array_slice($cards,1);
}
echo "Return false\n";
return false;
}
# ---------------------------------------------------------------------
function isStraight($cards){
sort($cards);
echo implode(",",$cards)."\n";
$s = count($cards);
for($i = 1; $i < $s ; $i++){
for($j = $i, $cnt = 0; $j < $s; $j++, $cnt += 1) if($cards[$j-1] >= $cards[$j]) break;
echo $cnt."\n";
if(4 == $cnt) return true;
}
return false;
}

Just PHP FUN 060.

Started at 21:09 27.07.2020 Monday July.
Finished at 23:42 27.07.2020 Monday July. (23.07.2020 1:30 Tuesday) 4hrs 21 minutes.

<?php
# https://www.codewars.com/kata/57fb017d9610ce369a0006ac Exclamation marks series #9: Remove or add a exclamation mark at the end of words of the sentence.
function remove(string $s): string {
$x =array_map(function($x){
if(preg_match('/!+$/',$x,$mtch)){
if(1 == strlen($mtch[0])) return substr($x,0,strlen($x)-1);
return $x;
}else{
return $x."!";
}
},explode(" ",$s));
return implode(" ",$x);
}
@lbvf50mobile
Copy link
Author

Import: 10,7,12,11,13,4,14
Sorted: 1,4,7,10,11,12,13
test: 1,4,7,10,11
test: 4,7,10,11,12
test: 7,10,11,12,13
Return false

10, 7, 12 ,11, 13, 4, 14
Failed asserting that true matches expected false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment