Commit e50c958b authored by Alexandr Sitchenko's avatar Alexandr Sitchenko

last version

parent 3d81210e
<?php
$start = microtime(true);
/**
* "Happy tickets"
* "The Happy Tickets"
*
* host/?first=000001&end=124555
*
......@@ -14,20 +14,22 @@ if (!isset ($_GET['first'], $_GET['end'])) {
if (!ctype_digit($_GET['first']) || !ctype_digit($_GET['end'])) {
exit('You haven`t passed the parameters.');
}
if (strlen($_GET['first']) !== 6 || strlen($_GET['end']) !== 6) {
exit('You must enter parameters in format 000XXX!!');
}
if (((int)$_GET['end'] - (int)$_GET['first']) <= 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'];
$num = $current;
$ticketsHappy = 0;
$currentLeft = intdiv($current, 1000);
$currentRight = $current % 1000;
$sumNumber = static function ($num) {
$sumCurrent = 0;
while ($num > 0) {
......@@ -39,12 +41,12 @@ $sumNumber = static function ($num) {
}
return $sumCurrent;
};
while ($currentLeft > 0) {
$sumCurrentLeft = $sumNumber($currentLeft);
while ($currentRight >= 0) {
$sumCurrentRight = $sumNumber($currentRight);
if ($sumCurrentLeft === $sumCurrentRight ) {
//echo($current.'<br/>');
++$ticketsHappy;
}
--$currentRight;
......
<?php
/**
* "Happy tickets"
*
......@@ -6,142 +7,50 @@
*
* @author Alexandr Sitchenko <alexsitch@outlook.com>
*/
define('LENGTH_NUMBER_TICKET', 6);
define('CHARACTERS_BY_HAPPY', 3);
checkStartParameters();
if (!isset ($_GET['first'], $_GET['end'])) {
exit('You haven`t passed the parameters.');
}
if (!ctype_digit($_GET['first']) || !ctype_digit($_GET['end'])) {
exit('You haven`t passed the parameters.');
}
if (strlen($_GET['first']) !== 6 || strlen($_GET['end']) !== 6) {
exit('You must enter parameters in format 000XXX!!');
}
if (((int)$_GET['end'] - (int)$_GET['first']) <= 0) {
exit('The first parameter must be less than the second!');
}
$data = [
'first' => (int)$_GET['first'],
'end' => (int)$_GET['end']
];
$current = $data['first'];
$current = (int)$_GET['end'];
$ticketsHappy = 0;
$currentLeft = intdiv($current, 1000);
$currentRight = $current % 1000;
/* Numbers are not prepare in the range 100000 - 999999 */
$zeroBeginNumber = checkNumbersAtRange($current);
/* Main Block */
do {
if (!$zeroBeginNumber) {
$ticketsCurrentNumber = $current;
} else {
$zeroBeginNumber = checkNumbersAtRange($current);
$ticketsCurrentNumber = prepareNumberTicketsByHappy($current);
}
if (checkHappyTicket($ticketsCurrentNumber)) {
//echo $ticketsCurrentNumber . '<br>';
$ticketsHappy++;
}
$current++;
} while ((bool)($data['end'] - $current));
echo $ticketsHappy;
/*End Main Block */
/**
* @param string $message
*/
function exitScript(string $message): void
{
exit ($message);
}
/**
* @return bool
*/
function checkStartParameters(): bool
{
if (!isset ($_GET['first'], $_GET['end'])) {
exitScript('You haven`t passed the parameters!');
$sumNumber = static function ($num) {
$sumCurrent = 0;
while ($num > 0) {
$sumCurrent += $num % 10;
$num = intdiv($num, 10);
}
$first = $_GET['first'];
$end = $_GET['end'];
if (!ctype_digit($first) || !ctype_digit($end)) {
exitScript('You must enter the correct parameters!');
if ($sumCurrent > 9) {
$sumCurrent = ($sumCurrent % 10) + intdiv($sumCurrent, 10);
}
if (checkStartLength($first) || checkStartLength($end)) {
exitScript('You must enter parameters in format 000XXX!');
return $sumCurrent;
};
while ($currentLeft > 0) {
$sumCurrentLeft = $sumNumber($currentLeft);
while ($currentRight >= 0) {
$sumCurrentRight = $sumNumber($currentRight);
if ($sumCurrentLeft === $sumCurrentRight ) {
++$ticketsHappy;
}
--$currentRight;
}
if (((int)$end - (int)$first) <= 0) {
exitScript('The first parameter must be less than the second!');
if ($currentLeft > 0) {
$currentRight = 999;
}
return true;
--$currentLeft;
}
/**
* Numbers are checked by length at start
*
* @param string $number
*
* @return bool
*/
function checkStartLength(string $number) {
return (strlen($number) < LENGTH_NUMBER_TICKET);
}
/**
* Numbers are checked in the range 100000 - 999999
*
* @param int $number
*
* @return bool
*/
function checkNumbersAtRange(int $number): bool
{
$figureInNumber = strlen((string)$number);
return ($figureInNumber < LENGTH_NUMBER_TICKET);
}
/**
* Zeros are added to the beginning of the current number.
*
* @param int $number
*
* @return string
*/
function prepareNumberTicketsByHappy(int $number): string
{
return str_repeat('0', strlen((string)$number)) . $number;
}
/**
* The main function at this test :)
*
* @param string $ticketsNumber
*
* @return bool
*/
function checkHappyTicket(string $ticketsNumber): bool
{
$firstSum = sumCharacters(substr($ticketsNumber, 0, CHARACTERS_BY_HAPPY));
$endSum = sumCharacters(substr($ticketsNumber, CHARACTERS_BY_HAPPY * (-1), CHARACTERS_BY_HAPPY));
return ($firstSum === $endSum);
}
/**
* Summation of a number
*
* @param string $sequence
*
* @return int
*/
function sumCharacters(string $sequence): int
{
$num = (int)$sequence;
$sum = 0;
while ($num > 0) {
$modulo = $num % 10;
$sum += $modulo;
$num = (int)$num / 10;
}
return $sum;
}
\ No newline at end of file
echo $ticketsHappy;
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