기억보다 기록을

[HackerRank] Compare the Tripliets, A Very Big Sum, Diagonal Difference 본문

Algorithm/Hacker Rank

[HackerRank] Compare the Tripliets, A Very Big Sum, Diagonal Difference

juyeong 2023. 4. 20. 16:39
반응형

[Compare the Tripliets]

Alice and Bob each created one problem for HackerRank. A reviewer rates the two challenges, awarding points on a scale from 1 to 100 for three categories: problem clarity, originality, and difficulty. ..

문제 상세 : https://www.hackerrank.com/challenges/compare-the-triplets/problem?isFullScreen=true 

 

Compare the Triplets | HackerRank

Compare the elements in two triplets.

www.hackerrank.com

답: 

def compareTriplets(a, b):
    answer = [0,0]
    for i in range(len(a)):
        if a[i] == b[i]:
            continue
        elif a[i] > b[i]:
            answer[0] +=1
        elif a[i] < b[i]:
            answer[1] +=1
            
    return answer

더 좋은 방법이 있을 것 같은데

리스트 한줄로 한다거나 음 음 음 ...

우선 웜업 단계니까 편하게 푸는 것에 의의를 두고 좀 더 나은 아이디어를 찾아 추가하는 방향으로 하겠습니다 :) 

 

 

[A Very Big Sum]

keep in mind that each elements will be larger than normal integer.. but 나에겐 파이썬 sum() 함수가 있는걸..

다른 언어는 언어가 클 때 빅오표기법 확인해봐야겠다 근데 파이썬은 .. 예.. 걍 sum 해버림..

문제: you are required to calculate and print the sum of the elements in an array, keeping in mind that some of those integers may be quite large.

문제 상세:  https://www.hackerrank.com/challenges/a-very-big-sum/problem?isFullScreen=true&h_r=next-challenge&h_v=zen 

 

A Very Big Sum | HackerRank

Calculate the sum of the values in an array that might exceed the range of int values.

www.hackerrank.com

답: 

def aVeryBigSum(ar):
    return sum(ar)

 

 

[Diagonal Difference]

squared matrix에서 diagonals의 (left to right and reversal of it) 차이의 절댓값을 구하는 문제였다

한줄로 abs(sum() - sum()) return 하고 싶었는데 그건 안됐고

답은 다음과 같다

문제 상세:

https://www.hackerrank.com/challenges/diagonal-difference/problem?isFullScreen=true&h_r=next-challenge&h_v=zen&h_r=next-challenge&h_v=zen 

 

답:

def diagonalDifference(arr):
    n = len(arr)
    sum_primary = 0
    sum_secondary = 0
    for i in range(n):
        sum_primary += arr[i][i]
        sum_secondary += arr[i][n-i-1]
    return abs(sum_primary - sum_secondary)
반응형