Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
tikets
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alexandr Sitchenko
tikets
Commits
e50c958b
Commit
e50c958b
authored
5 years ago
by
Alexandr Sitchenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
last version
parent
3d81210e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
135 deletions
+45
-135
index.php
index.php
+7
-5
ticket.php
ticket.php
+38
-130
No files found.
index.php
View file @
e50c958b
<?php
$start
=
microtime
(
true
);
/**
* "
Happy t
ickets"
* "
The Happy T
ickets"
*
* 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
;
...
...
This diff is collapsed.
Click to expand it.
ticket.php
View file @
e50c958b
<?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
;
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment