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

Basic Data Types

List Comprehensions

Task:

Let's learn about list comprehensions! You are given three integers x, y and z representing the dimensions of a cuboid along with an integer n. Print a list of all possible coordinates given by (i, j, k) on a 3D grid where the sum of i + j + k is not equal to n. Here, 0 <= i <= x; 0 <= j <= y; 0 <= k <= z. Please use list comprehensions rather than multiple loops, as a learning exercise.

Solution:

x = int(input())
y = int(input())
z = int(input())
n = int(input())

list1 = list([i, j, k] for i in range(x + 1) for j in range(y + 1) for k in range(z + 1) if i + j + k != n)
print(list1)

Find the Runner-Up Score!

Task:

Given the participants' score sheet for your University Sports Day, you are required to find the runner-up score. You are given n scores. Store them in a list and find the score of the runner-up.

Solution:

n = int(input())

list1 = list(set(map(int, input().split())))

list1.sort()

print(list1[-2])

Nested Lists

Task:

Given the names and grades for each student in a class of N students, store them in a nested list and print the name(s) of any student(s) having the second lowest grade.

Solution:

main_list = []

for i in range(int(input())):
    name = input()
    score = float(input())

    nested_list = []
    
    nested_list.append(name)
    nested_list.append(score)
    main_list.append(nested_list)
    
    del nested_list

#remove the lowest grade
lowest_grade = main_list[0][1]

for item in main_list:
    if item[1] < lowest_grade:
        lowest_grade = item[1]

to_delete = []
        
for item in main_list:
    if item[1] == lowest_grade:
        to_delete.append(item)

for item in to_delete:
    main_list.remove(item)

lowest_grade = main_list[0][1]

#find the second lowest grade

for item in main_list:
    if item[1] < lowest_grade:
        lowest_grade = item[1]
      
final_list = []

for item in main_list:
    if item[1] == lowest_grade:
        final_list.append(item[0])

final_list.sort()

for item in final_list:
    print(item)

Finding the percentage

Task:

The provided code stub will read in a dictionary containing key/value pairs of name:[marks] for a list of students. Print the average of the marks array for the student name provided, showing 2 places after the decimal.

Solution:

n = int(input())
student_marks = {}
for i in range(n):
    name, *line = input().split()
    scores = list(map(float, line))
    student_marks[name] = scores
query_name = input()

#print(student_marks[query_name])

sum_score = 0
amount = 0

for score in student_marks[query_name]:
    sum_score += score
    amount += 1

print("%.2f" % (sum_score / amount))

Lists

Task:

Consider a list (list = []). You can perform the following commands:

  1. insert i e: Insert integer e at position i.
  2. print: Print the list.
  3. remove e: Delete the first occurrence of integer e.
  4. append e: Insert integer at the end of the list.
  5. sort: Sort the list.
  6. pop: Pop the last element from the list.
  7. reverse: Reverse the list.
  8. Initialize your list and read in the value of n followed by n lines of commands where each command will be of the 7 types listed above. Iterate through each command in order and perform the corresponding operation on your list.


Solution:
n = int(input())
l = []

for i in range(n):
    command = input()
    
    if command.startswith("insert"):
        c = command.split()
        l.insert(int(c[1]), int(c[2]))
    elif command.startswith("print"):
        print(l)
    elif command.startswith("remove"):
        c = command.split()
        l.remove(int(c[1]))
    elif command.startswith("append"):
        c = command.split()
        l.append(int(c[1]))
    elif command.startswith("sort"):
        l.sort()
    elif command.startswith("pop"):
        l.pop()
    elif command.startswith("reverse"):
        l.reverse()

Tuples

Task:

Given an integer, n, and n space-separated integers as input, create a tuple, t, of those n integers. Then compute and print the result of hash(t).

Solution:

n = int(input())
l = input().split()

for i in range(n):
    l[i] = int(l[i])
 
print(hash(tuple(l)))