
Sponsored
Sponsored
Explanation: Iterator Filtering
This approach involves iterating through each record (or row) in the DataFrame and checking the 'student_id' against the specified value (101). Once a match is found, we can extract the 'name' and 'age' and return it as the result.
Time Complexity: O(n), where n is the number of students. We may have to check each student in the worst case.
Space Complexity: O(1), as no additional space beyond input storage is used.
The solution uses a list of dictionaries to represent the student data. By iterating over the list with a for loop and using Python's string formatting, we can output the desired 'name' and 'age' when the student ID is found.
Explanation: Lookup with Map/Dictionary
This approach involves using a hash table, such as a Dictionary in Python or HashMap in Java, to store each student record with their student_id as the key. This enables direct access to a record based on student_id with average O(1) complexity, extracting 'name' and 'age' immediately.
Time Complexity: O(1), as hash table operations (get) average constant time.
Space Complexity: O(n), space usage increases with input size.
1students = {
2 101: {'name': 'Ulysses', 'age': 13},
3 53: {'name': 'William', 'age': 10},
4 128: {'name': 'Henry', 'age': 6},
5 3: {'name': 'Henry', 'age': 11}
6}
7
8student = students.get(101)
9if student:
10 print('+---------+-----+')
11 print('| {name} | {age} |'.format(name=student['name'], age=student['age']))
12 print('+---------+-----+')
13The solution leverages Python's dict structure, which maps student IDs to their details. Retrieval using students.get(101) fetches the necessary record in constant time.