Computer Science/알고리즘 문제풀이

[codesignal][python] 9. isLucky

codesignal.com

 

Coding Tests and Assessments for Technical Hiring | CodeSignal

Learn how you can go beyond resumes in technical hiring with a state-of-the-art assessment platform and advanced coding tests

codesignal.com

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