Hacker Rank Python Challenges

Python Version 3

Problems

  1. Polar Coordinates
  2. Find Angle MBC
  3. Mod Divmod
  4. Power - Mod Power
  5. Integers Come In All Sizes
  6. Triangle Quest
  7. Triangle Quest 2

Polar Coordinates

Problem -> URL
Difficulty Level: Easy

Pseudocode:

  1. Read input from user.
  2. Convert the user input string to complex number.
  3. Use inbuilt function abs to calculate the distance.
  4. Use inbuilt function phase from cmath to calculate the angle.
  5. Print the results in the required order.

Solution:

# Enter your code here. Read input from STDIN. Print output to STDOUT
from cmath import phase

if __name__ == '__main__':
    complex_number = complex(input())
    distance = abs(complex_number)
    angle = phase(complex_number)
    print(distance)
    print(angle)

Find Angle MBC

Problem -> URL
Difficulty Level: Medium

Pseudocode:

  1. Read input from user.
  2. Validate user input, constraints 0 < AB, BC <= 100.
  3. Find angle BCA, We know tan(theta) = adjancent / opposite. We can calculate theta in degrees.
  4. We know sum of all the angles in a triangle is 180, MBC + BMC + BCM = 180. BCM is same as BCA and BMC is 90.
  5. Round the angle MBC and print the result in required format.

Solution:

# Enter your code here. Read input from STDIN. Print output to STDOUT
from math import sqrt
from math import degrees
from math import atan

def is_valid_input(AB, BC):
    if((AB > 0 and AB <= 100) and (BC > 0 and BC <= 100)):
        return True
    return False


def solution(AB, BC):
    if is_valid_input(AB, BC):
        # Find angle BCA - tan(theta)
        BCA = degrees(atan(BC/AB))
        
        # We know sum of all angles in a triangle is 180.
        MBC = 180.0 - 90.0 - BCA
        
        return u"{}\xb0".format(round(MBC))

if __name__ == '__main__':
    AB = int(input())
    BC = int(input())
    result =  solution(AB, BC)
    print(result)

Mod Divmod

Problem -> URL
Difficulty Level: Easy

Pseudocode:

  1. Read input from user.
  2. Use inbuilt function divmod().
  3. Print the results as required.

Solution:

# Enter your code here. Read input from STDIN. Print output to STDOUT
if __name__ == '__main__':
    a = int(input())
    b = int(input())
    result = divmod(a,b)
    print(result[0])
    print(result[1])
    print(result)

Power - Mod Power

Problem -> URL
Difficulty Level: Easy

Pseudocode:

  1. Read input from user.
  2. Validate user input. Constraints 1 <= a,b <= 10 and 2 <= m <= 1000.
  3. Calculate power and modulo of power.
  4. Print the result in required format.

Solution:

# Enter your code here. Read input from STDIN. Print output to STDOUT
def is_valid_input(a, b, m):
    if((a >= 1 and a <= 10) and (b >= 1 and b <= 10) and (m >= 2 and m <= 1000)):
        return True
    return False


def solution(a, b, m):
    if(is_valid_input(a, b, m)):
        return (pow(a,b), pow(a, b, m))


if __name__ == '__main__':
    a = int(input())
    b = int(input())
    m = int(input())
    result = solution(a, b, m)
    print(result[0])
    print(result[1])

Integers Come In All Sizes

Problem -> URL
Difficulty Level: Easy

Pseudocode:

  1. Read input from user.
  2. Validate user input. Constraints 1 <= a,b,c,d <= 1000.
  3. Calculate sum of power(a, b) and power(c, d).
  4. Print the result.

Solution:

# Enter your code here. Read input from STDIN. Print output to STDOUT
def is_in_range(num):
    if(num >= 1 and num <= 1000):
        return True
    return False


def is_valid_input(a,b,c,d):
    if(is_in_range(a) and is_in_range(b) and is_in_range(c) and is_in_range(d)):
        return True
    return False

def solution(a, b, c, d):
    if is_valid_input(a, b, c, d):
        result = pow(a, b) + pow(c, d)
        print(result)

if __name__ == '__main__':
    a = int(input())
    b = int(input())
    c = int(input())
    d = int(input())
    solution(a, b, c, d)

Triangle Quest

Problem -> URL
Difficulty Level: Medium

Pseudocode:

  1. Read input from user.
  2. For each number, we need calculate the sum of series.
  3. Instead of using for loop, we will use map and generate elements for the series that need to be summed up.
  4. Summing up all the elements will result in the required number.

Extracting Series :

From given e.g.
1
22
333
4444

Series will be,
For i = 1, 1 * 10 ^ 0
For i = 2, 2 * 10 ^ 1 + 2 * 10 ^ 0
For i = 3, 3 * 10 ^ 2 + 3 * 10 ^ 1 + 3 * 10 ^ 0
For i = 4, 4 * 10 ^ 3 + 4 * 10 ^ 2 + 4 * 10 ^ 1 + 4 * 10 ^ 0

From above we can understand that our series will be (10 ^ 0 + 10 ^ 1 + 10 ^ 2 + .... + 10 ^ i - 1) * i

Note: Our solution will not be accepted if we use additional for loop or string methods.

Solution:

# Using String Method - This will not be accepted
for i in range(1,int(input())):
    print(str(i) * i)

# Using List Comprehension - This will not be accepted
for i in range(1,int(input())):
    print(sum([i * 10 ** j for j in range(i)]))

# Using Map
for i in range(1,int(input())):
    print(sum(map(lambda x: 10 ** x, range(0, i))) * i)

Triangle Quest 2

Problem -> URL
Difficulty Level: Medium

Pseudocode:

  1. Read input from user.
  2. For each number, we need calculate the sum of series and square the sum value.
  3. Instead of using for loop, we will use map and generate elements for the series that need to be summed up.
  4. Summing up all the elements and squaring the sum value will result in the required palindrome number.

Extracting Series :

From given e.g.
1
121
12321
1234321
123454321

Series will be,
For i = 1, (10 ^ 0) ^ 2
For i = 2, (10 ^ 1 + 10 ^ 0) ^ 2
For i = 3, (10 ^ 2 + 10 ^ 1 + 10 ^ 0) ^ 2
For i = 4, (10 ^ 3 + 10 ^ 2 + 10 ^ 1 + 10 ^ 0) ^ 2
For i = 5, (10 ^ 4 + 10 ^ 3 + 10 ^ 2 + 10 ^ 1 + 10 ^ 0) ^ 2

From above we can understand that our series will be (10 ^ 0 + 10 ^ 1 + 10 ^ 2 + .... + 10 ^ i - 1) ^ 2

Note: Our solution will not be accepted if we use additional for loop or string methods.

Solution:

for i in range(1,int(input())):
    print(sum(map(lambda x: 10 ** x, range(i))) ** 2)