-
Notifications
You must be signed in to change notification settings - Fork 0
/
isLuckyTicketNum.py
34 lines (22 loc) · 934 Bytes
/
isLuckyTicketNum.py
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
# 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):
numstr = str(n)
half1 = numstr[:len(numstr)//2]
half2 = numstr[len(numstr)//2:]
sumhalf1 = sum([int(x) for x in half1])
sumhalf2 = sum([int(x) for x in half2])
return sumhalf1 == sumhalf2