Skip to content

Instantly share code, notes, and snippets.

@lbvf50mobile
Last active September 17, 2020 16:03
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/491193d622b59a77b1239e631d5f8ee2 to your computer and use it in GitHub Desktop.
Save lbvf50mobile/491193d622b59a77b1239e631d5f8ee2 to your computer and use it in GitHub Desktop.
Just PHP FUN 105.
<?php
# https://www.codewars.com/kata/565c9f5fcabeeaeb5a000052 Area of a regular polygon.
function area_of_regular_polygon(string $s): string {
$not = 'Invalid input';
echo "Input: $s\n";
if( ! preg_match('/(\d+.?\d*) sides of (\d+.?\d*) (\w+) each/',$s,$match)) return $not;
$n = (float) $match[1]; (float) $side = $match[2]; $units = $match[3];
echo "$n - sides of $side length \n";
if(2 >= $n || 0 >= $side || 0 != $n - floor($n)) return $not;
$a = ($side*$side)*$n;
$b = 4*tan(pi()/$n);
return sprintf("%.2f sq.$units", round($a/$b,2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment