# 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

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!

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

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

for item in main_list:

to_delete = []

for item in main_list:
to_delete.append(item)

for item in to_delete:
main_list.remove(item)

for item in main_list:

final_list = []

for item in main_list:
final_list.append(item[0])

final_list.sort()

for item in final_list:
print(item)
```

### Finding the percentage

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

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

```n = int(input())