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

Built-Ins

Zipped!

Task:

The National University conducts an examination of N students in X subjects. Your task is to compute the average scores of each student.

Solution:

nx = input().split()
x = int(nx[1])

subjects = list()
for i in range(x):
    subject = [float(x) for x in input().split()]
    subjects.append(subject)

for i in zip(*subjects):
    sum_per_student = 0
    for j in i:
        sum_per_student += j
    print(sum_per_student / x)

Input()

Task:

You are given a polynomial P of a single indeterminate (or variable), x. You are also given the values of x and k. Your task is to verify if P(x) = k.

Solution:

first = input().split()
second = input()

new_str = second.replace('x', first[0])

print(eval(new_str) == int(first[1]))

Python Evaluation

Task:

You are given an expression in a line. Read that line as a string variable, such as var, and print the result using eval(var).

Solution:

eval(input())

Athlete Sort

Task:

You are given a spreadsheet that contains a list of N athletes and their details (such as age, height, weight and so on). You are required to sort the data based on the Kth attribute and print the final resulting table. Note that K is indexed from 0 to M - 1, where M is the number of attributes.

Solution:

nm = input().split()
arr = []

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

sort_col = int(input())
arr = sorted(arr, key=lambda l:l[sort_col], reverse=False)

for i in arr:
    row = ''
    for j in i:
        row += str(j) + ' '
    print(row)

Any or All

Task:

You are given a space separated list of integers. If all the integers are positive, then you need to check if any integer is a palindromic integer.

Solution:

num = input()
lst = input().split()
lst_int = list(map(int, lst))

ret = False
if all(x > 0 for x in lst_int):
    for i in lst:
        if i == i[::-1]:
            ret = True

print(ret)

ginortS

Task:

You are given a string S. S contains alphanumeric characters only. Your task is to sort the string in the following manner:

  • All sorted lowercase letters are ahead of uppercase letters.
  • All sorted uppercase letters are ahead of digits.
  • All sorted odd digits are ahead of sorted even digits.


Solution:
s = input()

lowercase = []
uppercase = []
odd_num = []
even_num = []

for i in s:
    if i.islower():
        lowercase.append(i)
    elif i.isupper():
        uppercase.append(i)
    elif i.isnumeric():
        if int(i) % 2 == 0:
            even_num.append(i)
        else:
            odd_num.append(i)

lowercase.sort()
uppercase.sort()
even_num.sort()
odd_num.sort()

lst = lowercase + uppercase + odd_num + even_num
ret = ''
for i in lst:
    ret += i

print(ret)