Ad
Mathematics
Code
Diff
  • 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
Code
Diff
  • 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
Code
Diff
  • 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;
    • }
Code
Diff
  • function rubegoldberg(): int {
      return __LINE__ >> 1;
    }
    • function rubegoldberg(){return __LINE__;}
    • function rubegoldberg(): int {
    • return __LINE__ >> 1;
    • }
Mathematics
Algorithms
Logic
Numbers
Code
Diff
  • 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);
    • }
Fundamentals
Logic
Code
Diff
  • // > 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
Code
Diff
  • 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;
    • }