andraskiss.hu

Home / Development / Software Development / Python / HackerRank Challenges

HackerRank Python Challenges

In this article I show how I solved HackerRank Python challenges. The main purpose of the content is to present my acquired experiences primarily for learning purposes.

Other categories:

Introduction
Basic Data Types
Strings
Sets
Itertools
Collections
Built-Ins
Numpy
Others

Numpy

Arrays

Task:

You are given a space separated list of numbers. Your task is to print a reversed NumPy array with the element type float.

Solution:

import numpy

def arrays(arr):
    return numpy.array(arr[::-1], float)

arr = input().strip().split(' ')
result = arrays(arr)
print(result)

Shape and Reshape

Task:

You are given a space separated list of nine integers. Your task is to convert this list into a 3x3 NumPy array.

Solution:

import numpy

print(numpy.reshape(numpy.array(list(map(int, numpy.array(input().split())))), (3, 3)))

Transpose and Flatten

Task:

You are given a NxM integer array matrix with space separated elements (N = rows and M = columns). Your task is to print the transpose and flatten results.

Solution:

import numpy

n, m = input().split()

lst = []
for i in range(int(n)):
    lst.append(list(map(int, input().split())))

arr = numpy.array(lst)

print(numpy.transpose(arr))
print(numpy.array(lst).flatten())

Concatenate

Task:

You are given two integer arrays of size NxP and MxP (N & M are rows, and P is the column). Your task is to concatenate the arrays along axis 0.

Solution:

import numpy

n, m, p = input().split()

lst_n = []
lst_m = []
for i in range(int(n)):
    lst_n.append(list(map(int, input().split())))

for i in range(int(m)):
    lst_m.append(list(map(int, input().split())))

print(numpy.concatenate((numpy.array(lst_n), numpy.array(lst_m))))

Zeros and Ones

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.

Solution:

import numpy

ints = tuple(map(int, input().split()))

print(numpy.zeros(ints, dtype = int))
print(numpy.ones(ints, dtype = int))

Array Mathematics

Task:

You are given two integer arrays, A and B of dimensions NxM. Your task is to perform the following operations:

  1. Add (A + B)
  2. Subtract (A - B)
  3. Multiply (A * B)
  4. Integer Division (A / B)
  5. Mod (A % B)
  6. Power (A ** B)


Solution:
import numpy

nm = list(map(int, input().split()))
a = []
b = []

for i in range(nm[0]):
    a.append(list(map(int, input().split())))

for i in range(nm[0]):
    b.append(list(map(int, input().split())))

print(numpy.add(a, b))
print(numpy.subtract(a, b))
print(numpy.multiply(a, b))
print(numpy.floor_divide(a, b))
print(numpy.mod(a, b))
print(numpy.power(a, b))

Polynomials

Task:

You are given the coefficients of a polynomial P. Your task is to find the value of P at point x.

Solution:

import numpy

p = list(map(float, input().split()))
x = int(input())

print(numpy.polyval(p, x))