Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active July 4, 2020 16: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/da5a6776c8ced93fc1025cb8a12d9129 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/da5a6776c8ced93fc1025cb8a12d9129 to your computer and use it in GitHub Desktop.
Just PHP FUN 041.
<?php
# https://www.codewars.com/kata/59727ff285281a44e3000011 Band name generator.
function band_name_generator(string $s): string {
$s = strtolower($s);
if($s[0] == $s[strlen($s)-1]) return substr(ucfirst($s),0,strlen($s)-1).$s;
return "The ".ucfirst($s);
}
<?php
# https://www.codewars.com/kata/57f75cc397d62fc93d000059 Char Code Calculation.
function calc($s) {
$x = str2str($s);
$y = str_replace(7,1,$x);
return array_sum(str_split($x)) - array_sum(str_split($y));
}
function str2str($s){
return implode(array_map(function($x){ return ord($x);},str_split($s)));
}
<?php
# https://www.codewars.com/kata/58989a079c70093f3e00008d Cartesian neighbors.
function cartesianNeighbor($x, $y){
$ans = [];
for($i = -1; $i <= 1; $i += 1)
for($j = -1; $j <= 1; $j += 1)
if($i != 0 || $j != 0)
array_push($ans,[$x+$i,$y+$j]);
return $ans;
}
<?php
# https://www.codewars.com/kata/582c42e0f000e54a7d000086 Dan's great power generator
function danspower(int $num, int $power): int {
$x = $num ** $power;
if(1 == $x%2) return (int) round($x/10) * 10;
return $x;
}

Just JUST PHP 041.

Started at 21:31 04.07.2020 Saturday July.
Finished at 23:31 04.07.2020 Saturday July. (2hrs 0minutes)

<?php
# https://www.codewars.com/kata/57873ab5e55533a2890000c7 Correct the time-string.
function timeCorrect($timestring) {
if(empty($timestring)) return $timestring;
if(!preg_match('/^\d{2}:\d{2}:\d{2}$/',$timestring)) return null;
$max = 60*60*24; list($hrs,$mnt,$sec) = explode(":",$timestring);
$total = (60 * 60* (int)$hrs + 60 * (int)$mnt + (int) $sec)%$max;
$hrs = (int) floor($total/(60*60));
$mnt = (int) floor(($total - $hrs*60*60)/60);
$sec = $total - $hrs*60*60 - $mnt*60;
return sprintf('%02d:%02d:%02d',$hrs,$mnt,$sec);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment