forked from jamil-said/code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisLucky.py
More file actions
executable file
·42 lines (29 loc) · 944 Bytes
/
isLucky.py
File metadata and controls
executable file
·42 lines (29 loc) · 944 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
""" isLucky -- 10 min
Ticket numbers usually consist of an even number of digits. A ticket
number is considered lucky if the sum of the first half of the digits is
equal to the sum of the second half.
Given a ticket number n, determine if it's lucky or not.
Example
For n = 1230, the output should be
isLucky(n) = true;
For n = 239017, the output should be
isLucky(n) = false.
Input/Output
[execution time limit] 4 seconds (py3)
[input] integer n
A ticket number represented as a positive integer with an even number of
digits.
Guaranteed constraints:
10 ≤ n < 106.
[output] boolean
true if n is a lucky ticket number, false otherwise.
"""
def isLucky(n):
lstN, sum1, sum2 = list(str(n)), 0, 0
if len(lstN) % 2 != 0: return False
for n in range(len(lstN)//2):
sum1 += int(lstN[n])
sum2 += int(lstN[len(lstN)-1-n])
return sum1 == sum2
print(isLucky(1230)) # True
print(isLucky(239017)) # False