HackerRank Python Solution - Itertools Topic - itertools.product()

itertools.product():
  • This tool computes the cartesian product of input iterables.
  • It is equivalent to nested for-loops.
  • For example, product(A, B) returns the same as ((x,y) for x in A for y in B).
Sample Code:

>>> from itertools import product
>>>
>>> print list(product([1,2,3],repeat = 2))
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (2, 3), (3, 1), (3, 2), (3, 3)]
>>>
>>> print list(product([1,2,3],[3,4]))
[(1, 3), (1, 4), (2, 3), (2, 4), (3, 3), (3, 4)]
>>>
>>> A = [[1,2,3],[3,4,5]]
>>> print list(product(*A))
[(1, 3), (1, 4), (1, 5), (2, 3), (2, 4), (2, 5), (3, 3), (3, 4), (3, 5)]
>>>
>>> B = [[1,2,3],[3,4,5],[7,8]]
>>> print list(product(*B))
[(1, 3, 7), (1, 3, 8), (1, 4, 7), (1, 4, 8), (1, 5, 7), (1, 5, 8), (2, 3, 7), (2, 3, 8), (2, 4, 7), (2, 4, 8), (2, 5, 7), (2, 5, 8), (3, 3, 7), (3, 3, 8), (3, 4, 7), (3, 4, 8), (3, 5, 7), (3, 5, 8)]

HackerRank Python Solution - Math Topic - Triangle Quest

  • You are given a positive integer N. Print a numerical triangle of height N-1 like the one below:
1
22
333
4444
55555
......
  • Can you do it using only arithmetic operations, a single for loop, and print statement?
  • Use no more than two lines. The first line (the for statement) is already written for you. You have to complete the print statement.
  • Note: Using anything related to strings will give a score of 0.

HackerRank Python Solution - Math Topic - Integer Come In All Sizes

  • Integers in Python can be as big as the bytes in your machine's memory. There is no limit in size as there is: 231-1 (c++ int) or 263-1 (C++ long long int).
  • As we know, the result of ab grows really fast with increasing b.
  • Let's do some calculations on very large integers.

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.

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...