규도자 개발 블로그
[해커랭크(Hackerrank)/Problem Solving/파이썬3(python3)] Apple and Orange 본문
[해커랭크(Hackerrank)/Problem Solving/파이썬3(python3)] Apple and Orange
규도자 (gyudoza) 2019. 3. 14. 23:04Sam's house has an apple tree and an orange tree that yield an abundance of fruit. In the diagram below, the red region denotes his house, where is the start point, and is the endpoint. The apple tree is to the left of his house, and the orange tree is to its right. You can assume the trees are located on a single point, where the apple tree is at point , and the orange tree is at point .
When a fruit falls from its tree, it lands units of distance from its tree of origin along the -axis. A negative value of means the fruit fell units to the tree's left, and a positive value of means it falls units to the tree's right.
Given the value of for apples and oranges, determine how many apples and oranges will fall on Sam's house (i.e., in the inclusive range )?
For example, Sam's house is between and . The apple tree is located at and the orange at . There are apples and oranges. Apples are thrown units distance from , and units distance. Adding each apple distance to the position of the tree, they land at . Oranges land at . One apple and two oranges land in the inclusive range so we print
1
2
Function Description
Complete the countApplesAndOranges function in the editor below. It should print the number of apples and oranges that land on Sam's house, each on a separate line.
countApplesAndOranges has the following parameter(s):
- s: integer, starting point of Sam's house location.
- t: integer, ending location of Sam's house location.
- a: integer, location of the Apple tree.
- b: integer, location of the Orange tree.
- apples: integer array, distances at which each apple falls from the tree.
- oranges: integer array, distances at which each orange falls from the tree.
Input Format
The first line contains two space-separated integers denoting the respective values of and .
The second line contains two space-separated integers denoting the respective values of and .
The third line contains two space-separated integers denoting the respective values of and .
The fourth line contains space-separated integers denoting the respective distances that each apple falls from point .
The fifth line contains space-separated integers denoting the respective distances that each orange falls from point .
Constraints
Output Format
Print two integers on two different lines:
- The first integer: the number of apples that fall on Sam's house.
- The second integer: the number of oranges that fall on Sam's house.
Sample Input 0
7 11
5 15
3 2
-2 2 1
5 -6
Sample Output 0
1
1
Explanation 0
The first apple falls at position .
The second apple falls at position .
The third apple falls at position .
The first orange falls at position .
The second orange falls at position .
Only one fruit (the second apple) falls within the region between and , so we print as our first line of output.
Only the second orange falls within the region between and , so we print as our second line of output.
풀이
#!/bin/python3
import math
import os
import random
import re
import sys
# Complete the countApplesAndOranges function below.
def countApplesAndOranges(s, t, a, b, apples, oranges):
apple_count = 0
orange_count = 0
apple_tree_location = a
orange_tree_location = b
for apple_location in apples:
if s <= a + apple_location <= t:
apple_count = apple_count + 1
for orange_location in oranges:
if s <= b + orange_location <= t:
orange_count = orange_count + 1
print(apple_count)
print(orange_count)
if __name__ == '__main__':
st = input().split()
s = int(st[0])
t = int(st[1])
ab = input().split()
a = int(ab[0])
b = int(ab[1])
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()))
countApplesAndOranges(s, t, a, b, apples, oranges)
설명
문제의 개요를 간단하게 설명하자면 1차원 좌표의 특정 범위에 들어오는 숫자를 구하는 문제이다.
s는 Sam의 집이 시작되는 부분이고 t는 Sam의 집이 끝나는 부분이다. a는 사과나무의 위치이고 b는 오렌지나무의 위치이다. 각 배열에 들어가는 숫자가 각 나무에서 떨어진 과실의 위치를 의미하는데 1차원 좌표가 그러하듯이 숫자가 커질 수록 오른쪽으로, 작을 수록 왼쪽으로 향한다. 모든 입력이 끝났을 때 Sam의 집의 범위 안에 들어와있는 사과의 갯수와 오렌지의 갯수를 구하는 문제이다.
오히려 이상한 스토리를 끼워놔서 좀 더 어렵게 보이는 것 같다. 나도 그랬고. 예제에서 볼 수 있듯이 sample input의 2번째 라인, 5와 15가 각각 사과나무와 오렌지나무의 위치를 의미하는데 사과의 위치로 주어진 [-2, 2, 1]은 사과나무에서 시작하는 상대좌표이므로 현재 사과나무위치를 더하면 절대좌표를 얻을 수 있다. 고로 사과들의 절대좌표는 [3. 7. 6]이 되며 주어진 7~11이라는 Sam의 집 범위 안에 들어가는 사과는 1개가 되어 sample output의 위 숫자가 1이 되는 것이다.
이런식의 문제이다.
'알고리즘 > 풀이' 카테고리의 다른 글
[해커랭크(Hackerrank)/30 Days of Code/파이썬3(python3)] Day 0: Hello, World. (0) | 2019.03.19 |
---|---|
[해커랭크(Hackerrank)/Problem Solving/파이썬3(python3)] Grading Students (0) | 2019.03.19 |
[백준/10950/파이썬(python3)] A+B - 3 (0) | 2019.03.14 |
[해커랭크(Hackerrank)/Problem Solving/파이썬3(python3)] Birthday Cake Candles (0) | 2019.03.09 |
[해커랭크(Hackerrank)/Problem Solving/파이썬3(python3)] Mini-Max Sum (0) | 2019.03.09 |