Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active August 1, 2020 20:08
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/f015404acc6b204f3182abc3616797b3 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/f015404acc6b204f3182abc3616797b3 to your computer and use it in GitHub Desktop.
Just PHP FUN 065.
<?php
# https://www.codewars.com/kata/5b358a1e228d316283001892 Interview Question (easy).
function get_strings($city) {
$ans = [];
$x = count_chars(strtolower($city),1);
$city = preg_replace('/[^a-z]/i','',$city);
$chrs = array_values(array_unique(str_split(strtolower($city))));
foreach( $chrs as $v)
array_push($ans,chr(ord($v)).":".str_repeat('*',$x[ord($v)]));
return implode(',',$ans);
}
<?php
# https://www.codewars.com/kata/58aed2cafab8faca1d000e20 Nth power rules them all!
function modified_sum(array $a, int $n): int {
return array_sum(array_map(fn($x) => pow($x,$n),$a)) - array_sum($a);
}
<?php
# https://www.codewars.com/kata/52988f3f7edba9839c00037d The reject() function.
function reject($array, $predicate) {
return array_values(array_filter($array, fn($x) => ! $predicate($x)));
}
<?php
# https://www.codewars.com/kata/580777ee2e14accd9f000165 The Skiponacci Sequence.
function skiponacci($n) {
if(1 == $n) return "1";
if(2 == $n) return "1 skip";
$a = 1; $b = 1; $i = 2; $ans = "1 skip";
for( ; $i < $n; $i += 1){
$new_b = $a + $b; $a = $b; $b = $new_b;
if( 0 == $i%2){ $ans .= " $b";}
else{ $ans .= " skip";}
}
return $ans;
<?php
function valid_name($array) {
if(empty($array)) return "You must test at least one name.";
if(1 == count($array)) return "Congratulations, you can choose any name you like!";
$a = array_map(function($x){$a = str_split(strtolower($x)); return [$a[0],$a[count($a)-1]];},$array);
for($i=1; $i < count($a); $i += 1) if($a[$i-1][1] != $a[$i][0]) return "Back to the drawing board, your baby names are not compatible.";
return "Congratulations, your baby names are compatible!";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment