Skip to content

Commit 0671d28

Browse files
committed
Adds 'Nested Lists' Python Challenge
1 parent aae222d commit 0671d28

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

Python/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@
1010
- Print function: [Challenge](https://www.hackerrank.com/challenges/python-print/problem) - [Solution](https://github.com/monoprosito/hackerrank_challenges/tree/master/Python/print_function)
1111
- Tuples: [Challenge](https://www.hackerrank.com/challenges/python-tuples/problem) - [Solution](https://github.com/monoprosito/hackerrank_challenges/tree/master/Python/tuples)
1212
- Find the Runner-Up Score!: [Challenge](https://www.hackerrank.com/challenges/find-second-maximum-number-in-a-list/problem) - [Solution](https://github.com/monoprosito/hackerrank_challenges/tree/master/Python/find_the_runner_up_score)
13+
- Nested Lists: [Challenge](https://www.hackerrank.com/challenges/nested-list/problem) - [Solution](https://github.com/monoprosito/hackerrank_challenges/tree/master/Python/nested_lists)

Python/nested_lists/README.md

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
### Problem
2+
3+
Given the names and grades for each student in a class of ![equation](https://latex.codecogs.com/gif.latex?N) students, store them in a nested list and print the name(s) of any student(s) having the second lowest grade.
4+
5+
**Note:** If there are multiple students with the second lowest grade, order their names alphabetically and print each name on a new line.
6+
7+
### Example
8+
9+
![equation](https://latex.codecogs.com/gif.latex?records%20%3D%20%5B%5B%22chi%22%2C%2020.0%5D%2C%20%5B%22beta%22%2C%2050.0%5D%2C%20%5B%22alpha%22%2C%2050.0%5D%5D)
10+
11+
The ordered list of scores is ![equation](https://latex.codecogs.com/gif.latex?%5B20.0%2C50.0%5D), so the second lowest score is ![equation](https://latex.codecogs.com/gif.latex?50.0). There are two students with that score: ![equation](https://latex.codecogs.com/gif.latex?%5B%22beta%22%2C%20%22alpha%22%5D). Ordered alphabetically, the names are printed as:
12+
13+
```
14+
alpha
15+
beta
16+
```
17+
18+
### Input Format
19+
20+
The first line contains an integer, ![equation](https://latex.codecogs.com/gif.latex?N), the number of students.
21+
22+
The ![equation](https://latex.codecogs.com/gif.latex?2N) subsequent lines describe each student over ![equation](https://latex.codecogs.com/gif.latex?2) lines.
23+
24+
* The first line contains a student's name.
25+
26+
* The second line contains their grade.
27+
28+
### Constraints
29+
30+
- ![equation](https://latex.codecogs.com/gif.latex?2%20%5Cleq%20N%20%5Cleq%205)
31+
32+
- There will always be one or more students having the second lowest grade.
33+
34+
### Output Format
35+
36+
Print the name(s) of any student(s) having the second lowest grade in. If there are multiple students, order their names alphabetically and print each one on a new line.
37+
38+
### Sample Input 0
39+
40+
```
41+
5
42+
Harry
43+
37.21
44+
Berry
45+
37.21
46+
Tina
47+
37.2
48+
Akriti
49+
41
50+
Harsh
51+
39
52+
```
53+
54+
### Sample Output 0
55+
56+
```
57+
Berry
58+
Harry
59+
```
60+
61+
### Explanation 0
62+
63+
There are ![equation](https://latex.codecogs.com/gif.latex?5) students in this class whose names and grades are assembled to build the following list:
64+
65+
*python students = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41], ['Harsh', 39]]*
66+
67+
The lowest grade of ![equation](https://latex.codecogs.com/gif.latex?37.2) belongs to Tina. The second lowest grade of ![equation](https://latex.codecogs.com/gif.latex?37.21) belongs to both Harry and Berry, so we order their names alphabetically and print each name on a new line.
68+
69+
**Author:** [harsh_beria93](https://www.hackerrank.com/profile/harsh_beria93)
70+
71+
**Difficulty:** Easy
72+
73+
**Max Score:** 10

Python/nested_lists/nested_lists.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
if __name__ == '__main__':
2+
# Create a students list
3+
students = list()
4+
5+
# Create the set to store the scores
6+
scores = set()
7+
8+
for _ in range(int(input())):
9+
name = input()
10+
score = float(input())
11+
12+
# Create a list of each student
13+
student = [name, score]
14+
15+
# Add each student score to a set
16+
scores.add(score)
17+
18+
# Add each student list to the general students list
19+
students.append(student)
20+
21+
# Order the set of students scores
22+
ordered_scores = sorted(scores)
23+
24+
# Get the second lowest from the student scores
25+
second_lowest_score = ordered_scores[1]
26+
27+
# Print the student names with the second lowest score
28+
for student in sorted(students):
29+
if student[1] == second_lowest_score:
30+
print(student[0])

Python/nested_lists/test_case_0.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Input (stdin)
2+
5
3+
Harry
4+
37.21
5+
Berry
6+
37.21
7+
Tina
8+
37.2
9+
Akriti
10+
41
11+
Harsh
12+
39
13+
14+
Expected Output
15+
Berry
16+
Harry

0 commit comments

Comments
 (0)