Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active June 23, 2020 10:05
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/4e1c32927cbf94f254cd0c4c210ea138 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/4e1c32927cbf94f254cd0c4c210ea138 to your computer and use it in GitHub Desktop.
Just PHP FUN 031.
<?php
# https://www.codewars.com/kata/58712dfa5c538b6fc7000569 Simple beads count.
function count_red_beads(int $n): int {
$ans = ($n-1)*2;
return $ans >= 0 ? $ans : 0;
}
<?php
# https://www.codewars.com/kata/5a8c1b06fd5777d4c00000dd Principal Diagonal | VS | Secondary Diagonal.
function diagonal($matrix){
$s = count($matrix);
$pd = 0;
for($i = 0; $i < $s; $i +=1 ) $pd += $matrix[$i][$i];
$sd = 0;
for($i = 0; $i < $s; $i +=1 ) $sd += $matrix[$i][$s - 1 - $i];
if($pd == $sd) return 'Draw!';
if($pd > $sd) return 'Principal Diagonal win!';
return "Secondary Diagonal win!";
}
<?php
# https://www.codewars.com/kata/585a033e3a36cdc50a00011c Frequency sequence.
function freq_seq(string $s, string $sep): string {
$x = str_split($s);
$h = count_chars($s,1);
return implode($sep,array_map(function($v) use ($h){ return $h[ord($v)];},$x));
}

Just PHP FUN 031.

Started at 23.06.2020 15:59 Tuesday.
Finised at 23.06.2020 17:05 Tuesday. (1hr 6minutes)

<?php
# https://www.codewars.com/kata/5c784110bfe2ef660cb90369 Ranking position.
function ranking($people) {
if(empty($people)) return [];
$cmp = function($a,$b){
if($a['points'] == $b['points']) return $a['name'] <=> $b['name'];
return $b['points'] <=> $a['points'];
};
usort($people,$cmp);
$people[0]["position"] = 1;
for($i = 1; $i < count($people); $i += 1){
if($people[$i-1]["points"] == $people[$i]["points"]){
$people[$i]["position"] = $people[$i-1]["position"];
}else{
$people[$i]["position"] = $i + 1;
}
}
return $people;
}
<?php
# https://www.codewars.com/kata/585a1a227cb58d8d740001c3 Thinkful - String Drills: Repeater.
use function str_repeat as solution;
# ------------------------------------------------------------------------------------------
function solution($s, $n) {
return str_repeat($s,$n);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment