Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active May 19, 2020 13:47
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/9743f00c1be0369a10fa016ed9c4278a to your computer and use it in GitHub Desktop.
Save lbvf50mobile/9743f00c1be0369a10fa016ed9c4278a to your computer and use it in GitHub Desktop.
Just PHP Fun #001.
<?php
# https://www.codewars.com/kata/5d50e3914861a500121e1958 Alpabetical Addition.
function addLetters(...$letters) {
if(empty($letters)) return 'z';
$digits = array_map(
function($x){ return ord($x) - ord('a') + 1;},
$letters
);
$val = array_sum($digits)%26;
if(0 == $val) return 'z';
return chr(ord('a') - 1 + $val);
}

Just PHP FUN 001.

Started at 18:06 19.05.2020 Tuesday May. Finished at 20:46 19.05.2020 Tuesday May. 2hrs 40 mintes. (Total time with chating, tea, and so on.)

<?php
# https://www.codewars.com/kata/59f7fc109f0e86d705000043 By 3, or not by 3? That is the question
function divisibleByThree($str) {
$x = array_reduce(
str_split($str),
function($acc,$v){
return $acc + (int)$v;
},
0
);
return 0 == $x%3;
}
<?php
# https://www.codewars.com/kata/593c9175933500f33400003e Return the first M multiples of N.
function multiples(int $m, float $n): array {
$answer = [];
for($i = 1; $i<=$m; $i++) array_push($answer, $i*$n);
return $answer;
}
<?php
# https://www.codewars.com/kata/57a6633153ba33189e000074 Order count of characthers.
function orderedCount($text) {
if(empty($text)){
return [];
}
$text = str_split($text);
$h = [];
foreach($text as $c){
if(array_key_exists($c,$h)){
$h[$c] += 1;
}else{
$h[$c] = 1;
}
}
arsort($h);
$ans = [];
foreach($h as $k=>$v){
array_push($ans,[$k,$v]);
}
return $ans;
}
<?php
# https://www.codewars.com/kata/55fd2d567d94ac3bc9000064 Sum of odd numbers.
function rowSumOddNumbers($n) {
if(1 == $n) return 1;
$last = ((1 + $n)*$n) - 1;
$first = ((1 + $n-1) * ($n-1)) + 1;
return (($last + $first) * $n)/2;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment