Commit e660b58f authored by Alexandr Sitchenko's avatar Alexandr Sitchenko

the test was completed

parent e50c958b
<?php
$start = microtime(true);
/**
* "The Happy Tickets"
*
* host/?first=000001&end=124555
*
* @author Alexandr Sitchenko <alexsitch@outlook.com>
* PHP >=7.2
* host/?first=000001&last=124555
* Statistical date: The sequence from 000 to 999 have 11*10 sums of number of each from 1 to 9
* The sequence from 00 to 99 have 11 sums of number of each from 1 to 9
* Example: There is number 3 is 37 sums in sequence from 566 to 222
* 566-222 = 344 = 3*100 + 4*10 + 4 -> 3*11 + 4*1 + 0 (4 !== 3) = 37
* @author Alexandr Sitchenko <alex.sit@outlook.com>
*/
/*
**************************
* Check input parameters *
**************************
*/
if (!isset ($_GET['first'], $_GET['end'])) {
if (!isset ($_GET['first'], $_GET['last'])) {
exit('You haven`t passed the parameters.');
}
if (!ctype_digit($_GET['first']) || !ctype_digit($_GET['end'])) {
if (!ctype_digit($_GET['first']) || !ctype_digit($_GET['last'])) {
exit('You haven`t passed the parameters.');
}
if (strlen($_GET['first']) !== 6 || strlen($_GET['end']) !== 6) {
if (strlen($_GET['first']) !== 6 || strlen($_GET['last']) !== 6) {
exit('You must enter parameters in format 000XXX!!');
}
if (((int)$_GET['end'] - (int)$_GET['first']) <= 0) {
/* the main parameters is set */
$firstNumber = (int)$_GET['first'] < 999 ? 999 : (int)$_GET['first'];
$lastNumber = (int)$_GET['last'];
$range = $lastNumber - $firstNumber;
if ($range <= 0) {
exit('The first parameter must be less than the second!');
}
/* "The Happy Tickets" are not in sequence from 0 to 10 */
if ((int)$_GET['end'] < 1001) {
exit('0 "The Happy Tickets" is in the indicated sequence ');
}
$current = (int)$_GET['end'];
$ticketsHappy = 0;
$currentLeft = intdiv($current, 1000);
$currentRight = $current % 1000;
$sumNumber = static function ($num) {
$sumCurrent = 0;
while ($num > 0) {
$sumCurrent += $num % 10;
$num = intdiv($num, 10);
}
if ($sumCurrent > 9) {
$sumCurrent = ($sumCurrent % 10) + intdiv($sumCurrent, 10);
$message = ' "The Happy Tickets" is in the range';
/* "The Happy Tickets" are not in sequence from 000001 to 001000 */
if ($lastNumber < 1001) {
exit($ticketsHappy . $message);
}
/*
*****************************
* Block for set of function *
*****************************
*/
/*
* Sum of numeric in three-digit number
*
* @param int $num - three-digit number
* @return int - sum of numeric in input number
*/
$sumNumberMax3Numeric = static function (int $num): int {
$sum = (int)($num / 100) + (int)(($num % 100) / 10) + $num % 10;
if ($sum > 9) {
$sum = (int)(($sum % 100) / 10) + $sum % 10;
}
return $sumCurrent;
return $sum;
};
/**
* "The Happy Tickets" is calculated into range without las ten
*
* number of Coincidences in 1000 = (int)($range / 1000 - 1) * 11 * 10;
* number of Coincidences in 100 = (int)(($range % 1000) / 100 ) * 11;
* number of Coincidences in 10 = (int)(($range % 100) / 10 );
*
* @param int $range
* @return int
*/
$numberCoincidences10 = static function (int $range): int {
return (int)($range / 1000 ) * 11 * 10 + (int)(($range % 1000) / 100) * 11 + (int)(($range % 100) / 10);
};
while ($currentLeft > 0) {
$sumCurrentLeft = $sumNumber($currentLeft);
while ($currentRight >= 0) {
$sumCurrentRight = $sumNumber($currentRight);
if ($sumCurrentLeft === $sumCurrentRight ) {
++$ticketsHappy;
/**
* "The Happy Tickets" is calculated into las ten
*
* @param int $range
* @param int $lastNumber
* @return int
*/
$numberCoincidencesLast10 = static function (int $range, int $lastNumber) use ($sumNumberMax3Numeric): int {
$lastTen = $range % 10;
$startForCheck = $lastNumber - $lastTen;
$checkThousand = (((int)($lastNumber / 1000) - (int)($startForCheck / 1000)) === 1);
/* example if tickets are have a numeric from 003999 to 004001 */
$sumNumberLeftPart = $sumNumberMax3Numeric((int)($startForCheck / 1000));
$coincidencesLast10 = 0;
while ($startForCheck <= $lastNumber) {
$sumCurrent = $sumNumberMax3Numeric($startForCheck % 100);
if ($checkThousand && $sumCurrent === 0) {
$sumNumberLeftPart = $sumNumberMax3Numeric((int)($startForCheck / 1000));
}
--$currentRight;
}
if ($currentLeft > 0) {
$currentRight = 999;
if ($sumNumberLeftPart === $sumCurrent) {
++$coincidencesLast10;
}
++$startForCheck;
}
--$currentLeft;
}
return $coincidencesLast10;
};
/*
***************************************
* The end a block for set of function *
***************************************
*/
echo $ticketsHappy;
/**
* The calculate for "The Happy Tickets"
*/
$ticketsHappy = $numberCoincidences10($range) + $numberCoincidencesLast10($range, $lastNumber);
echo $ticketsHappy . $message;
/* time of script */
echo '<br /><br />';
echo 'Script execution time: ' . round(microtime(true) - $start, 4) . ' s.';
\ No newline at end of file
echo 'Script execution time: ' . round(microtime(true) - $start, 4) . ' s.';
/*
*********************
* The block of test *
*********************
*/
echo '<h1>Test:</h1>';
/**
* @param int $firstNumberTest
* @param int $lastNumberTest
* @param int $happyTicketsMustBe
*/
$test = static function (int $firstNumberTest, int $lastNumberTest, int $happyTicketsMustBe)
use ($numberCoincidences10, $numberCoincidencesLast10): void {
$rangeTest = $lastNumberTest - $firstNumberTest;
$testResult = 'False';
$ticketTest = $numberCoincidences10($rangeTest) + $numberCoincidencesLast10($rangeTest, $lastNumberTest);
if ($ticketTest === $happyTicketsMustBe) {
$testResult = 'True';
}
echo 'There are(is) a ' . $happyTicketsMustBe . ' "The Happy Tickets"
must be in range from ' . $firstNumberTest . ' to ' . $lastNumberTest .
' - ' . $testResult;
echo '<br /><br />';
};
$test(999, 1001, 1);
$test(9999, 10001, 2);
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment