Prepare > Python > Numpy > Zeros and Ones

2023. 8. 30. 20:40HackerRank-Python

 

Zeros and Ones | HackerRank

Print an array using the zeros and ones tools in the NumPy module.

www.hackerrank.com

 

문제


Task

You are given the shape of the array in the form of space-separated integers, each integer representing the size of different dimensions, your task is to print an array of the given shape and integer type using the tools numpy.zeros and numpy.ones.

Input Format

A single line containing the space-separated integers.

Output Format

First, print the array using the numpy.zeros tool and then print the array with the numpy.ones tool.

 

=> 숫자 3개를 인풋해줄건데, numpy.zeros()와 numpy.ones()를 이용해 인풋한 숫자 차원 만큼의 array를 생성하라

 

 

 

 

코드


import numpy as np

N = list(map(int, input().split()))

print(
    np.zeros(N, int)
)

print(
    np.ones(N, int)
)

 

 

 

 

노트


numpy.zeros()

import numpy

print(numpy.zeros((1,2)))                    #Default type is float
#Output : [[ 0.  0.]] 

print(numpy.zeros((1,2), dtype = numpy.int)) #Type changes to int
#Output : [[0 0]]
  • 모든 요소를 0으로 하는 array 생성
  • 첫번째 파라미터로 차원을 입력
  • 두 번째 파라미터로는 데이터 타입을 입력

 

 

numpy.ones()

import numpy

print(numpy.ones((1,2)))                    #Default type is float
#Output : [[ 1.  1.]] 

print(numpy.ones((1,2), dtype = numpy.int)) #Type changes to int
#Output : [[1 1]]
  • 모든 요소가 1인 array 생성
  • 첫번째 파라미터로 차원을 입력
  • 두 번째 파라미터로는 데이터 타입을 입력