티스토리 뷰

Codesignal

code signal - 11. isLucky

터보건 2020. 1. 29. 16:37

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.

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.

MY_ANSWER

def isLucky(n):
    n = [int(i) for i in str(n)]
    n_slice = len(n) // 2

    if sum(n[:n_slice]) == sum(n[n_slice : ]):
        return True
    else:
        return False

Best_ANSWER

def isLucky(n):
    s = str(n)
    pivot = len(s)//2
    left, right = s[:pivot], s[pivot:]
    return sum(map(int, left)) == sum(map(int, right))

 

'Codesignal' 카테고리의 다른 글

code signal - 13. reverseInParentheses  (0) 2020.02.09
code signal - 12. Sort by Height  (0) 2020.01.29
code signal - 10. commonCharacterCount  (0) 2020.01.29
code signal - 9. All Longest Strings  (0) 2020.01.29
code signal - 8. matrixElementsSum  (0) 2020.01.29
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/11   »
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
글 보관함