Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active June 26, 2020 15:49
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/da2cc3fa8e5db028470bd29198aefc98 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/da2cc3fa8e5db028470bd29198aefc98 to your computer and use it in GitHub Desktop.
Just PHP FUN 034.
<?php
# https://www.codewars.com/kata/595519279be6c575b5000016 Battle of the characters (Easy).
function battle($x, $y) {
$tmp = weight($x) - weight($y);
return 0 == $tmp ? "Tie!" : ($tmp > 0 ? $x : $y);
}
function weight($x){
$sum = 0;
foreach(count_chars($x,1) as $k=>$v) $sum += ($k - ord('A') + 1) * $v;
return $sum;
}
# ----------------------------------------------------------------------------------------------
/**
If do not delete ord('A') from a character code, it is literally multiplication of the ord('A'),
Rest of a char code is too small for consideration.
*/
function battle($x, $y) {
$tmp = weight($x) - weight($y);
return 0 == $tmp ? "Tie!" : ($tmp > 0 ? $x : $y);
}
function weight($x){
$sum = 0; $sum1 = 0;
var_dump(count_chars($x,1));
foreach(count_chars($x,1) as $k=>$v) $sum += ($k - ord('A') + 1) * $v;
foreach(count_chars($x,1) as $k=>$v) $sum1 += ($k) * $v;
echo "$x $sum $sum1 \n";
return $sum;
}
<?php
# https://www.codewars.com/kata/5624e574ec6034c3a20000e6 Hamming Distance - Part 1: Binary codes.
function hamming_distance(string $r, string $s): int {
return array_sum(array_map(function($x,$y){ return (int)$x ^ (int)$y;},str_split($r),str_split($s)));
}

Just PHP FUN 034.

Started at 26.06.2020 at 21:05 Friday June.
Finished at 26.06.2020 at 22:48 Friday June. (1hr 45minutes)

<?php
# https://www.codewars.com/kata/5648b12ce68d9daa6b000099 Number of People in the Bus.
function number($bus_stops) {
return array_reduce($bus_stops,function($acc,$x){ return $acc + $x[0] - $x[1]; },0);
}
<?php
# https://www.codewars.com/kata/57cc981a58da9e302a000214 Small enough? - Beginner.
function smallEnough($a, $limit)
{
return count($a) == count(array_filter($a,function($x) use ($limit){ return $x <= $limit;}));
}
<?php
# https://www.codewars.com/kata/58844a13aa037ff143000072 Simple Fun #7: Will You?
function will_you(bool $y, bool $b, bool $l): bool {
return ($y && $b && !$l) || ($l && (!$b || !$y));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment