Commit 5a6b49ee authored by Alexandr Sitchenko's avatar Alexandr Sitchenko

first

parent 815e2a02
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ComposerJsonPluginSettings">
<unboundedVersionInspectionSettings>
<excludedPackages />
</unboundedVersionInspectionSettings>
<customRepositories />
<composerUpdateOptions />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/g5test.iml" filepath="$PROJECT_DIR$/g5test.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="PhpProjectSharedConfiguration" php_language_level="7.2" />
</project>
\ No newline at end of file
<?php
$start = microtime(true);
/**
* "Happy tickets"
*
......@@ -6,142 +7,45 @@
*
* @author Alexandr Sitchenko <alexsitch@outlook.com>
*/
define('LENGTH_NUMBER_TICKET', 6);
define('CHARACTERS_BY_HAPPY', 3);
checkStartParameters();
$data = [
'first' => (int)$_GET['first'],
'end' => (int)$_GET['end']
];
$current = $data['first'];
$ticketsHappy = 0;
/* 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);
if (!isset ($_GET['first'], $_GET['end'])) {
exit('You haven`t passed the parameters.');
}
/**
* @return bool
*/
function checkStartParameters(): bool
{
if (!isset ($_GET['first'], $_GET['end'])) {
exitScript('You haven`t passed the parameters!');
}
$first = $_GET['first'];
$end = $_GET['end'];
if (!ctype_digit($first) || !ctype_digit($end)) {
exitScript('You must enter the correct parameters!');
}
if (checkStartLength($first) || checkStartLength($end)) {
exitScript('You must enter parameters in format 000XXX!');
}
if (((int)$end - (int)$first) <= 0) {
exitScript('The first parameter must be less than the second!');
}
return true;
if (!ctype_digit($_GET['first']) || !ctype_digit($_GET['end'])) {
exit('You haven`t passed the parameters.');
}
/**
* 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);
}
if (strlen($_GET['first']) !== 6 || strlen($_GET['end']) !== 6) {
exit('You must enter parameters in format 000XXX!!');
/**
* 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);
if (((int)$_GET['end'] - (int)$_GET['first']) <= 0) {
exit('The first parameter must be less than the second!');
}
$current = (int)$_GET['first'];
$num = $current;
$ticketsHappy = 0;
do {
$sum1 = 0;
$sum2 = 0;
$current = $num;
while ($current > 999) {
$sum1 += $current % 10;
$current = (int)$current / 10;
}
while ($current > 0) {
$sum2 += $current % 10;
$current = (int)$current / 10;
}
if ($sum1 === $sum2) {
++$ticketsHappy;
}
//$happy($current);
$stopLoop = ((int)$_GET['end'] - $num);
++$num;
} while ($stopLoop);
/**
* 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;
echo '<br /><br />';
echo 'Время выполнения скрипта: ' . round(microtime(true) - $start, 4) . ' сек.';
\ 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