
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.
using System.Collections.Generic;
public class Student {
public int student_id;
public string name;
public int age;
public Student(int student_id, string name, int age) {
this.student_id = student_id;
this.name = name;
this.age = age;
}
}
public class SelectData {
public static void Main() {
List<Student> students = new List<Student> {
new Student(101, "Ulysses", 13),
new Student(53, "William", 10),
new Student(128, "Henry", 6),
new Student(3, "Henry", 11)
};
foreach (var student in students) {
if (student.student_id == 101) {
Console.WriteLine("+---------+-----+");
Console.WriteLine("| {0,7} | {1,3} |", student.name, student.age);
Console.WriteLine("+---------+-----+");
break;
}
}
}
}This approach leverages a List to hold student objects in C#. Using a simple foreach loop, it searches for the student with ID 101 and prints their 'name' and 'age' using formatted strings.
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.
1using System;
2using System.Collections.Generic;
3
4public class Student {
5 public string Name { get; set; }
6 public int Age { get; set; }
7}
8
9public class Program {
10 public static void Main() {
11 Dictionary<int, Student> studentsMap = new Dictionary<int, Student> {
12 {101, new Student {Name = "Ulysses", Age = 13}},
13 {53, new Student {Name = "William", Age = 10}},
14 {128, new Student {Name = "Henry", Age = 6}},
15 {3, new Student {Name = "Henry", Age = 11}}
16 };
17
18 if (studentsMap.TryGetValue(101, out Student student)) {
19 Console.WriteLine("+---------+-----+");
20 Console.WriteLine($"| {student.Name} | {student.Age} |");
21 Console.WriteLine("+---------+-----+");
22 }
23 }
24}In C#, a Dictionary maps the unique student_id to Student objects. The TryGetValue method helps in obtaining the student with ID 101 efficiently and securely.