level : easy
Question:
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
My answer:
def isLucky(n):
half_len=int(len(str(n))/2)
half1=list(str(n)[:half_len])
half1=list(map(lambda x:int(x), half1))
half2=list(str(n)[half_len:])
half2=list(map(lambda x:int(x), half2))
if(sum(half1)==sum(half2)):
return True
else:
return False
- string으로 변환후 반 잘라서 자릿수 확인한다.
- 앞에 부분의 합 ==뒷 부분의 합 : True
'Computer Science > 알고리즘 문제풀이' 카테고리의 다른 글
[프로그래머스][python] 12912. 두 정수 사이의 합 (0) | 2020.12.29 |
---|---|
[codesignal][python] 10. Sort by Height (0) | 2020.12.28 |
[codesignal][python] 8. commonCharacterCount (0) | 2020.12.27 |
[codesignal][python] 7. AllLongestStrings (0) | 2020.12.27 |
[프로그래머스][python] 68644. 두 개 뽑아서 더하기 (0) | 2020.12.26 |