PL/Python

HackerRank Compare the Triplets

junmosdata 2022. 8. 5. 07:43

Quiz.

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.

The rating for Alice's challenge is the triplet a = (a[0], a[1], a[2]), and the rating for Bob's challenge is the triplet b = (b[0], b[1], b[2]).

The task is to find their comparison points by comparing a[0] with b[0], a[1] with b[1], and a[2] with b[2].

  • If a[i] > b[i], then Alice is awarded 1 point.
  • If a[i] < b[i], then Bob is awarded 1 point.
  • If a[i] = b[i], then neither person receives a point.

Comparison points is the total points a person earned.

Given a and b, determine their respective comparison points.

example)

a = [1, 2, 3]
b = [3, 2, 1]

  • For elements *0*, Bob is awarded a point because a[0] .

The return array is [1, 1] with Alice's score first and Bob's second.

 

#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'compareTriplets' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
#  1. INTEGER_ARRAY a
#  2. INTEGER_ARRAY b
#

def compareTriplets(a, b):
    # Write your code here

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    a = list(map(int, input().rstrip().split()))

    b = list(map(int, input().rstrip().split()))

    result = compareTriplets(a, b)

    fptr.write(' '.join(map(str, result)))
    fptr.write('\n')

    fptr.close()

 

위의 소스에서 compareTriplets 함수의 내용을 수정해서 문제를 해결해야 한다.

리턴되는 값이 리스트여야 한다는 것을 뒤늦게 알아서 문제를 해결하기 어려웠다.

리스트는 두 값으로 구성되는데, 첫 번째 값은 a가 높은 횟수, 두 번째 값을 b가 높은 횟수를 저장한다.

a와 b는 길이가 같도록 값이 입력된다. (a가 3개 입력되면 b도 3개)

 

 

my code)

def compareTriplets(a, b):
    # Write your code here
    ans = [0, 0]
    for i in range(len(a)):
        if a[i] > b[i]:
            ans[0] += 1
        elif a[i] < b[i]:
            ans[1] += 1
        else:
            continue
    return ans