Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active May 22, 2020 16:15
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/7b576ea7372018028641d629f0fc46aa to your computer and use it in GitHub Desktop.
Save lbvf50mobile/7b576ea7372018028641d629f0fc46aa to your computer and use it in GitHub Desktop.
Just PHP FUN 004.
<?php
# https://www.codewars.com/kata/59cfc000aeb2844d16000075 Alternate capitalization
function capitalize($s) {
$a = "";
$b = "";
$arr = str_split($s);
for($i = 0; $i <= count($arr); $i++){
if(0 == $i%2){
$a .= strtoupper($s[$i]);
$b .= strtolower($s[$i]);
}else{
$b .= strtoupper($s[$i]);
$a .= strtolower($s[$i]);
}
}
return [$a,$b];
}
<?php
# https://www.codewars.com/kata/567d71b93f8a50f461000019 `Genetic Algorithm Series - #3 Crossover`.
function crossover($a, $b, $index) {
var_dump([$a,$b,$index]); # <== interesting without this does not works.
if(empty($a)) return ['',''];
$a1 = substr($a,0,$index);
$b1 = substr($b,0,$index);
$a2 = substr($a,$index);
$b2 = substr($b,$index);
return [$a1.$b2, $b1.$a2];
}
# https://www.codewars.com/kata/567d71b93f8a50f461000019 `Genetic Algorithm Series - #3 Crossover`.
function crossover($a, $b, $index) {
$a1 = substr($a,0,$index);
$b1 = substr($b,0,$index);
$a2 = substr($a,$index);
$b2 = substr($b,$index);
return [$a1.$b2, $b1.$a2];
}
<?php
# Fails.
# https://www.codewars.com/kata/567d609f1c16d7369c000008/train/php
# Genetic Algorithm Series - #1 Generate
function generate($length) {
$x = str_pad(decbin(rand(0,intval(2**$length - 1))),$length,"0");
// var_dump($x);
return $x;
}
# Works
function generate($length) {
$ans = "";
for($i=0; $i < $length; $i++) $ans .= strval(rand(0,1));
return $ans;
}
<?php
# https://www.codewars.com/kata/58941fec8afa3618c9000184 Simple Fun #74: Growing Plan
function growingPlant($upSpeed, $downSpeed, $desiredHeight) {
$size = 0;
for($i = 1; $i < 50000; $i++){
$size += $upSpeed;
if($size >= $desiredHeight) return $i;
$size -= $downSpeed;
}
}

Just PHP FUN 004.

Started at 22.05.2020 21:27 Friday May.
Finished at 22.05.2020 22.15 1hr 12minutes.

<?php
# https://www.codewars.com/kata/567b468357ed7411be00004a `Genetic Algorithm Series - #4 Get population and fitnesses`.
function ($population, $fitness) {
$f = function($x) use ($fitness) {
return array("chromosome" => $x, "fitness" => $fitness($x));
};
return array_map(
$f,
$population
);
}
<?php
# Genetic Algorithm Series - #2 Mutation
# https://www.codewars.com/kata/567b39b27d0a4606a5000057/solutions/php/all/best_practice
function mutate($chromosome, $p) {
$chromosome = str_split($chromosome);
$funct = function($x) use ($p){
$rnd = rand(1,1000)/1000;
if($rnd <= $p) return "0" == $x ? "1" : "0" ;
return $x;
};
$ans = array_map($funct,$chromosome);
return implode($ans);
}
<?php
# https://www.codewars.com/kata/5d5ee4c35162d9001af7d699 Sum of Minimums!
function sum_of_minimums($numbers) {
return array_sum(array_map('min', $numbers));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment