Mathematics
function calcTokenCost($price, $token) { return max($token, $price ? (int) $token * round($price / $token) : 0); }
- function calcTokenCost($price, $token) {
return max([$token, $token ? $token * round($price / $token) : $token]);- return max($token, $price ? (int) $token * round($price / $token) : 0);
- }
Date Time
Strings
function howManyTimesBetween(string $sentence): string { if (!preg_match('/^(?<date1>\d{4}-\d{2}-\d{2}) and (?<date2>\d{4}-\d{2}-\d{2})$/', $sentence, $dates)) { return 'Your question is strange'; } $dates = array_map('date_create', $dates); if (!($diff = $dates['date1']->diff($dates['date2']))->days) { return 'Dates are equals'; } $diffStrings = [$diff->format('%y year(s)'), $diff->format('%m month(s)'), $diff->format('%d day(s)')]; $diffStrings = preg_grep('/^0/', $diffStrings, PREG_GREP_INVERT); return 'There are '.implode(', ', $diffStrings)." between $sentence"; }
- function howManyTimesBetween(string $sentence): string
- {
- if (!preg_match('/^(?<date1>\d{4}-\d{2}-\d{2}) and (?<date2>\d{4}-\d{2}-\d{2})$/', $sentence, $dates)) {
- return 'Your question is strange';
- }
- $dates = array_map('date_create', $dates);
if ($dates['date1'] == $dates['date2']) {return 'Dates are equals';- if (!($diff = $dates['date1']->diff($dates['date2']))->days) {
- return 'Dates are equals';
- }
$diff = $dates['date1']->diff($dates['date2']);- $diffStrings = [$diff->format('%y year(s)'), $diff->format('%m month(s)'), $diff->format('%d day(s)')];
- $diffStrings = preg_grep('/^0/', $diffStrings, PREG_GREP_INVERT);
- return 'There are '.implode(', ', $diffStrings)." between $sentence";
- }
Date Time
Strings
function howManyTimesBetween(string $sentence): string { if (!preg_match('/^(?<date1>\d{4}-\d{2}-\d{2}) and (?<date2>\d{4}-\d{2}-\d{2})$/', $sentence, $dates)) { return 'Your question is strange'; } $dates = array_map('date_create', $dates); if ($dates['date1'] == $dates['date2']) { return 'Dates are equals'; } $diff = $dates['date1']->diff($dates['date2']); $diffStrings = [$diff->format('%y year(s)'), $diff->format('%m month(s)'), $diff->format('%d day(s)')]; $diffStrings = preg_grep('/^0/', $diffStrings, PREG_GREP_INVERT); return 'There are '.implode(', ', $diffStrings).' between '.$sentence; }
- function howManyTimesBetween(string $sentence): string
- {
- if (!preg_match('/^(?<date1>\d{4}-\d{2}-\d{2}) and (?<date2>\d{4}-\d{2}-\d{2})$/', $sentence, $dates)) {
- return 'Your question is strange';
- }
- $dates = array_map('date_create', $dates);
- if ($dates['date1'] == $dates['date2']) {
- return 'Dates are equals';
- }
- $diff = $dates['date1']->diff($dates['date2']);
$diffStrings[] = $diff->format('%y year(s)');$diffStrings[] = $diff->format('%m month(s)');$diffStrings[] = $diff->format('%d day(s)');- $diffStrings = [$diff->format('%y year(s)'), $diff->format('%m month(s)'), $diff->format('%d day(s)')];
- $diffStrings = preg_grep('/^0/', $diffStrings, PREG_GREP_INVERT);
- return 'There are '.implode(', ', $diffStrings).' between '.$sentence;
- }
Mathematics
Algorithms
Logic
Numbers
function calcTokenCost($price, $token) { return max($token, $price ? (int) $token * round($price / $token) : 0); }
- function calcTokenCost($price, $token) {
if ($price == 0 && $token == 0) {return 0;}if ($price < $token) {return $token;}$mod = fmod($price, $token);if ($mod < ($token / 2)) {return $price - $mod;}return $price + $token - $mod;- return max($token, $price ? (int) $token * round($price / $token) : 0);
- }
// PHPUnit Test Examples: // TODO: Replace examples and use TDD development by writing your own tests class MyTest extends TestCase { // test function names should start with "test" public function testThatSomethingShouldHappen() { $this->assertEquals(20, calcTokenCost(19.99, 5)); $this->assertEquals(15, calcTokenCost(17.30, 5)); $this->assertEquals(5, calcTokenCost(0.01, 5)); $this->assertEquals(5, calcTokenCost(0, 5)); $this->assertEquals(0, calcTokenCost(0, 0)); } }
- // PHPUnit Test Examples:
- // TODO: Replace examples and use TDD development by writing your own tests
class MyTestCases extends TestCase- class MyTest extends TestCase
- {
- // test function names should start with "test"
- public function testThatSomethingShouldHappen() {
- $this->assertEquals(20, calcTokenCost(19.99, 5));
- $this->assertEquals(15, calcTokenCost(17.30, 5));
- $this->assertEquals(5, calcTokenCost(0.01, 5));
- $this->assertEquals(5, calcTokenCost(0, 5));
- $this->assertEquals(0, calcTokenCost(0, 0));
- }
- }
Fundamentals
Logic
// > given a positive integer N // do not have to account for N of 0 function binaryGap($n) { return preg_match_all('/1(0+)1/', decbin($n), $matches) ? max(array_map('strlen', $matches[1])) : 0; }
- // > given a positive integer N
- // do not have to account for N of 0
- function binaryGap($n)
- {
while (!($n & 1)){$n >>= 1;}$n >>= 1;$max = 0;$current = 0;while ($n){if ($n & 1){$max = max($max, $current);$current = 0;}else{++$current;}$n >>= 1;}return $max;}- return preg_match_all('/1(0+)1/', decbin($n), $matches) ? max(array_map('strlen', $matches[1])) : 0;
- }
Dates/Time
Data Types
Strings
function howManyTimesBetween(string $sentence): string { $errorMessage = 'Your question is strange'; if (!preg_match('/^(?<date1>\d{4}-\d{2}-\d{2}) and (?<date2>\d{4}-\d{2}-\d{2})$/', $sentence, $dates)) { return $errorMessage; } $dates = array_map('date_create', $dates); if ($dates['date1'] == $dates['date2']) { return 'Dates are equals'; } $diff = $dates['date1']->diff($dates['date2']); $diffStrings[] = $diff->format('%y year(s)'); $diffStrings[] = $diff->format('%m month(s)'); $diffStrings[] = $diff->format('%d day(s)'); $diffStrings = preg_grep('/^0/', $diffStrings, PREG_GREP_INVERT); return 'There are '.implode(', ', $diffStrings).' between '.$sentence; }
- function howManyTimesBetween(string $sentence): string
- {
- $errorMessage = 'Your question is strange';
$dates = array_map(fn($str) => DateTime::createFromFormat('Y-m-d', $str),explode(' and ', $sentence));$dates = array_filter($dates);if (count($dates) !== 2) {return $errorMessage;- if (!preg_match('/^(?<date1>\d{4}-\d{2}-\d{2}) and (?<date2>\d{4}-\d{2}-\d{2})$/', $sentence, $dates)) {
- return $errorMessage;
- }
$diffStrings = [];if ($dates[0] == $dates[1]) {- $dates = array_map('date_create', $dates);
- if ($dates['date1'] == $dates['date2']) {
- return 'Dates are equals';
- }
$diff = $dates[0]->diff($dates[1]);$mapping = ['y' => 'year(s)','m' => 'month(s)','d' => 'day(s)',];foreach ($mapping as $p => $s) {if ($diff->$p) {$diffStrings[] = $diff->$p.' '.$s;}}- $diff = $dates['date1']->diff($dates['date2']);
- $diffStrings[] = $diff->format('%y year(s)');
- $diffStrings[] = $diff->format('%m month(s)');
- $diffStrings[] = $diff->format('%d day(s)');
- $diffStrings = preg_grep('/^0/', $diffStrings, PREG_GREP_INVERT);
- return 'There are '.implode(', ', $diffStrings).' between '.$sentence;
- }