HackerRank Python Solution - Math Topic - Power - Mod Power

  • So far, we have only heard of Python's powers. Now, we will witness them!
  • Powers or exponents in Python can be calculated using the built-in power function. Call the power function ab as shown below:
>>> pow(a,b) 
or
 
>>> a**b
It's also possible to calculate ab mod m.
 
>>> pow(a,b,m)  

HackerRank Python Solution - Math Topic - Mod Divmod

One of the built-in functions of Python is divmod, which takes two arguments a and b, and returns a tuple containing the quotient of a/b first and then the remainder a.

For example:
 
>>> print divmod(177,10)
(17, 7)
Here, the integer division is 177/10 => 17 and the modulo operator is 177%10 => 7.

HackerRank Python Solution - Math Topic - Triangle Quest 2

  • You are given a positive integer N.
  • Your task is to print a palindromic triangle of size N.
  • For example, a palindromic triangle of size 5 is:
1
121
12321
1234321
123454321
  • You can't take more than two lines. The first line (a for-statement) is already written for you.
  • You have to complete the code using exactly one print statement. 

HackerRank Python Solution - Math Topic - Find Angle MBC

  • ABC is a right triangle, 90˙ at B. Therefore ∡ABC = 90˙
HackerRank Python Solution - Math Topic - Find Angle MBC
  • Point M is the midpoint of hypotenuse AC.
  • You are given the lengths AB and BC.
  • Your task is to find ∡MBC (angle Ө˙, as shown in the figure) in degrees.
Input Format:
  • The first line contains the length of side AB.
  • The second line contains the length of side BC.

HackerRank Python Solution - Math Topic - Polar Coordinates

  • Polar coordinates are an alternative way of representing Cartesian coordinates or Complex Numbers.
  • A complex number z = x + yj,  is completely determined by its real part x and imaginary part y. Here, j is the imaginary unit. 
  • A polar coordinate (r,φ) is completely determined by modulus r and phase angle φ.
  • If we convert complex number z to its polar coordinate, we find:
    • r: Distance from to origin, i.e., √ (x^2 + y^2)
    • φ: Counterclockwise angle measured from the positive x-axis to the line segment that joins z to the origin. 
  •  Python's cmath module provides access to the mathematical functions for complex numbers.

HackerRank Python Solution - Numpy Topic - Linear Algebra

The NumPy module also comes with a number of built-in routines for linear algebra calculations. These can be found in the sub-module linalg.

linalg.det:

The linalg.det tool computes the determinant of an array.

print numpy.linalg.det([[1 , 2], [2, 1]])       #Output : -3.0
linalg.eig:

The linalg.eig computes the eigenvalues and right eigenvectors of a square array.

HackerRank Python Solution - Numpy Topic - Polynomials

Poly:

The poly tool returns the coefficients of a polynomial with the given sequence of roots.

print numpy.poly([-1, 1, 1, 10])        #Output : [  1 -11   9  11 -10]
Roots:

The roots tool returns the roots of a polynomial with the given coefficients.

HackerRank Python Solution - Numpy Topic - Inner and Outer

Inner:

The inner tool returns the inner product of two arrays.

import numpy

A = numpy.array([0, 1])
B = numpy.array([3, 4])

print numpy.inner(A, B)     #Output : 4

HackerRank Python Solution - Numpy Topic - Dot and Cross

Dot:

The dot tool returns the dot product of two arrays.

import numpy

A = numpy.array([ 1, 2 ])
B = numpy.array([ 3, 4 ])

print numpy.dot(A, B)       #Output : 11

HackerRank Python Solution - Numpy Topic - Mean, Var, and Std

Mean:

The mean tool computes the arithmetic mean along the specified axis.

import numpy

my_array = numpy.array([ [1, 2], [3, 4] ])

print numpy.mean(my_array, axis = 0)        #Output : [ 2.  3.]
print numpy.mean(my_array, axis = 1)        #Output : [ 1.5  3.5]
print numpy.mean(my_array, axis = None)     #Output : 2.5
print numpy.mean(my_array)                  #Output : 2.5
By default, the axis is None. Therefore, it computes the mean of the flattened array.

HackerRank Python Solution - Numpy Topic - Min and Max

Min:

The tool min returns the minimum value along a given axis.

import numpy

my_array = numpy.array([[2, 5], 
                        [3, 7],
                        [1, 3],
                        [4, 0]])

print numpy.min(my_array, axis = 0)         #Output : [1 0]
print numpy.min(my_array, axis = 1)         #Output : [2 3 1 0]
print numpy.min(my_array, axis = None)      #Output : 0
print numpy.min(my_array)                   #Output : 0
By default, the axis value is None. Therefore, it finds the minimum over all the dimensions of the input array.

You might also like

Deploy your Django web app to Azure Web App using App Service - F1 free plan

In this post, we will look at how we can deploy our Django app using the Microsoft Azure app service - a free plan. You need an Azure accoun...