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
e660b58f
Commit
e660b58f
authored
Aug 08, 2019
by
Alexandr Sitchenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
the test was completed
parent
e50c958b
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
130 additions
and
39 deletions
+130
-39
index.php
index.php
+130
-39
No files found.
index.php
View file @
e660b58f
<?php
<?php
$start
=
microtime
(
true
);
$start
=
microtime
(
true
);
/**
/**
* "The Happy Tickets"
* "The Happy Tickets"
*
*
* host/?first=000001&end=124555
* PHP >=7.2
*
* host/?first=000001&last=124555
* @author Alexandr Sitchenko <alexsitch@outlook.com>
* 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.'
);
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.'
);
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!!'
);
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!'
);
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
;
$ticketsHappy
=
0
;
$currentLeft
=
intdiv
(
$current
,
1000
);
$message
=
' "The Happy Tickets" is in the range'
;
$currentRight
=
$current
%
1000
;
/* "The Happy Tickets" are not in sequence from 000001 to 001000 */
$sumNumber
=
static
function
(
$num
)
{
if
(
$lastNumber
<
1001
)
{
$sumCurrent
=
0
;
exit
(
$ticketsHappy
.
$message
);
while
(
$num
>
0
)
{
}
$sumCurrent
+=
$num
%
10
;
$num
=
intdiv
(
$num
,
10
);
/*
}
*****************************
if
(
$sumCurrent
>
9
)
{
* Block for set of function *
$sumCurrent
=
(
$sumCurrent
%
10
)
+
intdiv
(
$sumCurrent
,
10
);
*****************************
*/
/*
* 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
);
* "The Happy Tickets" is calculated into las ten
while
(
$currentRight
>=
0
)
{
*
$sumCurrentRight
=
$sumNumber
(
$currentRight
);
* @param int $range
if
(
$sumCurrentLeft
===
$sumCurrentRight
)
{
* @param int $lastNumber
++
$ticketsHappy
;
* @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
(
$sumNumberLeftPart
===
$sumCurrent
)
{
}
++
$coincidencesLast10
;
if
(
$currentLeft
>
0
)
{
}
$currentRight
=
999
;
++
$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
'<br /><br />'
;
echo
'Script execution time: '
.
round
(
microtime
(
true
)
-
$start
,
4
)
.
' s.'
;
echo
'Script execution time: '
.
round
(
microtime
(
true
)
-
$start
,
4
)
.
' s.'
;
\ No newline at end of file
/*
*********************
* 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
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