HackerRank-Python
Prepare > Python > Numpy > Floor, Ceil and Rint
stem_sw
2023. 9. 2. 22:20
Floor, Ceil and Rint | HackerRank
Use the floor, ceil and rint tools of NumPy on the given array.
www.hackerrank.com
문제
Task
You are given a 1-D array, A. Your task is to print the floor, ceil and rint of all the elements of A.
Note
In order to get the correct output format, add the line np.set_printoptions(legacy='1.13') below the numpy import.
Input Format
A single line of input containing the space separated elements of array A.
Output Format
On the first line, print the floor of A.
On the second line, print the ceil of A.
On the third line, print the rint of A.
=> array A에 대해 올림, 내림, 반올림 한 A를 구하라
코드
import numpy as np
np.set_printoptions(legacy='1.13')
A = np.array(input().split(), float)
print(np.floor(A))
print(np.ceil(A))
print(np.rint(A))
노트
np.floor()
import numpy
my_array = numpy.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9])
print(numpy.floor(my_array)) #[ 1. 2. 3. 4. 5. 6. 7. 8. 9.]
- 파라미터로 array를 넘기면 array의 모든 요소들을 내림해 리턴
- 기존 arrray는 유지
np.ceil()
import numpy
my_array = numpy.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9])
print(numpy.ceil(my_array)) #[ 2. 3. 4. 5. 6. 7. 8. 9. 10.]
- 파라미터로 array를 넘기면 array의 모든 요소들을 올림해 리턴
- 기존 arrray는 유지
np.rint()
import numpy
my_array = numpy.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9])
print(numpy.rint(my_array)) #[ 1. 2. 3. 4. 6. 7. 8. 9. 10.]
- 파라미터로 array를 넘기면 array의 모든 요소들을 반올림해 리턴
- 기존 arrray는 유지
sep =
print()의 옵셔널파라미터로 출력값들의 구분방법을 지정
print(np.floor(A), np.ceil(A), np.rint(A), sep='\n')
- 백슬래시(\) + n 은 줄바꿈의 의미