본문 바로가기
HackerRank Algorithm

[HackerRank] 23. Apple and orange

by KIha_Jung 2020. 4. 7.

Implementation

 

# 방법1
# Complete the countApplesAndOranges function below.
def countApplesAndOranges(s, t, a, b, apples, oranges):
    # Computing distance
    apples = [a + apple for apple in apples]
    oranges = [b + orange for orange in oranges]
    
    # apples & oranges in the range
    in_apples = [apple for apple in apples if (apple >= s) and (apple <= t)]
    in_oranges = [orange for orange in oranges if (orange >= s) and (orange <= t)]
    
    # return the number of apples and oranges
    return len(in_apples), len(in_oranges)

if __name__ == '__main__':
    # starting, ending point
    st = input().split()

    s = int(st[0])
    t = int(st[1])
    
    # location of the Apple & Orange tree
    ab = input().split()

    a = int(ab[0])
    b = int(ab[1])
    
    # number of apples and oranges
    mn = input().split()

    m = int(mn[0])
    n = int(mn[1])

    apples = list(map(int, input().rstrip().split()))
    oranges = list(map(int, input().rstrip().split()))

    result = countApplesAndOranges(s, t, a, b, apples, oranges)
    print('\n'.join(map(str, result)))

댓글