Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active July 31, 2020 16:39
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/c6a0630d54efde75600063e2b19a5da6 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/c6a0630d54efde75600063e2b19a5da6 to your computer and use it in GitHub Desktop.
Just PHP FUN 064.
<?php
# https://www.codewars.com/kata/57b365f81fae8a0571000142 Measuring Average Speed.
function calculate_speed($distance, $time) {
$distance = convert_to_metters($distance);
$time = convert_to_sec($time);
$speed = $distance/$time;
return sprintf("%.0fmph",$speed*2.23694);
}
function convert_to_metters($dist){
if(preg_match('/^(\d+)m$/',$dist,$match)){ return intval($match[0]); }
if(preg_match('/^(\d+)km$/',$dist,$match)){ return intval($match[0])*1000; }
throw new Exception($dist.' incorect distance value.');
}
function convert_to_sec($time){
if(preg_match('/^(\d+)s$/',$time,$match)){ return intval($match[0]); }
if(preg_match('/^(\d+)min$/',$time,$match)){ return intval($match[0])*60; }
throw new Exception($time.' incorect time value.');
}
<?php
# https://www.codewars.com/kata/5787628de55533d8ce000b84 Correct the date-string.
function dateCorrect($datestring) {
if(!$datestring) return $datestring;
if(preg_match('/^(\d{2})\.(\d{2})\.(\d{4})$/',$datestring,$match)){
$day = intval($match[1]);
$month = intval($match[2]);
$year = intval($match[3]);
echo "$day, $month, $year \n";
$y = $year + floor($month/12);
$mnth = $month%12;
$day = $day - 1;
echo "$day, $mnth, $y \n";
$date = new DateTime();
$date->setDate($y, $mnth, 1);
echo "First ".$date->format('d.m.Y')."\n";
$date->add(new DateInterval("P".$day."D"));
echo "Second ".$date->format('d.m.Y')."\n";
return $date->format('d.m.Y');
}
return null;
}
<?php
# https://www.codewars.com/kata/57bf599f102a39bb1e000ae5 Fibonacci's FizzBuzz.
function fibs_fizz_buzz($n) {
if(1 == $n) return [1];
if(2 == $n) return [1,1];
$a = 1; $b = 1; $i = 2; $ans = [1,1]; $new_b = 0;
for( ; $i < $n; $i += 1){
$new_b = $a+$b; $a = $b; $b = $new_b;
if(0 == $new_b%15) {$new_b = 'FizzBuzz'; }
elseif ( 0 == $new_b%3) {$new_b = "Fizz";}
elseif (0 == $new_b%5) {$new_b = "Buzz";}
array_push($ans, $new_b);
}
return $ans;
}
<?php
# https://www.codewars.com/kata/58049aa58f5e65b6da0000ef The iccanobiF Sequence.
function iccanobif($n) {
if(1 == $n) return [1];
if(2 == $n) return [1,1];
$a = 1; $b = 1; $i = 2;
$answer = [1,1];
for( ; $i < $n; $i += 1){
$new_b = $a+$b;
$a = $b; $b = $new_b;
array_unshift($answer, $b);
}
return $answer;
}

Just PHP FUN 064.

Started at 21:44 31.07.2020 Friday July.
Finished at 23:38 31.07.2020 Friday July. (1hr 54minutes)

<?php
# https://www.codewars.com/kata/567b39b27d0a4606a5000057 Genetic Algorithm Series - #2 Mutation.
function mutate($chromosome, $p) {
$p = $p * 100;
return implode(array_map(fn($x) => rand(1,100) <= $p ? intval(!$x) : $x, str_split($chromosome)));
}
<?php
# https://www.codewars.com/kata/579775203467db17b500037b Object-Oriented PHP #2 - Class Constructors and $this.
class Person{
public $first_name, $last_name;
public function __construct($f,$l){
$this->first_name = $f; $this->last_name = $l;
}
public function get_full_name(){
return "$this->first_name $this->last_name";
}
}
<?php
# https://www.codewars.com/kata/5783d8f3202c0e486c001d23 Convert an array of strings to array of numbers.
function toNumberArray(array $stringArray) : array {
return array_map(function($x){return floatval($x);},$stringArray);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment