Cracking the Code: Understanding HackerRank's Python Nested Lists
HackerRank is a popular platform for developers to hone their coding skills, and its Python Nested Lists challenge is a classic example of how to manipulate data structures effectively. This article will dissect the problem, provide a clear solution, and offer insights into the underlying concepts.
The Challenge: Finding the Second Lowest Grade
Imagine you're a teacher tasked with creating a report on your students' grades. You have a list of student names and their corresponding grades. Your challenge is to find the second lowest grade and then print the names of all the students who achieved that grade, in alphabetical order.
Example Scenario:
Let's say you have the following student data:
students = [
['Harry', 37.21],
['Berry', 37.21],
['Tina', 37.2],
['Akriti', 41],
['Harsh', 39]
]
In this scenario, the second lowest grade is 37.21. The students who achieved this grade are Harry and Berry. So, your output should be:
Berry
Harry
The Solution: A Step-by-Step Approach
Let's break down the solution into manageable steps:
-
Extract the Scores: First, we need to extract the unique grades from the list of students. This can be done using a set comprehension:
grades = {score for _, score in students}
-
Find the Second Lowest Grade: We can use the
sorted()
function to sort the grades and access the second lowest score:second_lowest = sorted(grades)[1]
-
Filter for Second Lowest: Now, we filter the list of students to only include those with the second lowest grade:
second_lowest_students = [name for name, score in students if score == second_lowest]
-
Print the Result: Finally, we sort the filtered list alphabetically and print each name on a separate line:
second_lowest_students.sort() for name in second_lowest_students: print(name)
Complete Python Code:
students = [
['Harry', 37.21],
['Berry', 37.21],
['Tina', 37.2],
['Akriti', 41],
['Harsh', 39]
]
grades = {score for _, score in students}
second_lowest = sorted(grades)[1]
second_lowest_students = [name for name, score in students if score == second_lowest]
second_lowest_students.sort()
for name in second_lowest_students:
print(name)
Understanding the Code:
- Sets: The
grades = {score for _, score in students}
line uses a set comprehension to efficiently create a set of unique scores. Sets are unordered collections that ensure no duplicates. - List Comprehensions: The
second_lowest_students = [name for name, score in students if score == second_lowest]
line demonstrates a powerful list comprehension. This concise syntax allows us to filter the student list based on the second lowest grade. - Sorting: The
sorted(grades)
function sorts the grades, and indexing with[1]
retrieves the second lowest grade. Similarly, thesecond_lowest_students.sort()
line sorts the filtered student names alphabetically before printing.
Key Takeaways:
- This exercise demonstrates the elegance and efficiency of Python's data structures and comprehensions.
- By understanding sets, lists, and comprehensions, you can write concise and elegant code for handling various data manipulation tasks.
- HackerRank challenges like this one provide valuable practice in real-world programming scenarios.
Resources: