Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace _05._Filter_By_Age
- {
- using System;
- using System.Collections.Generic;
- using System.Linq;
- public class StartUp
- {
- public static void Main()
- {
- var n = int.Parse(Console.ReadLine());
- var studetns = new Dictionary<string, int>();
- for (int i = 0; i < n; i++)
- {
- var currentStudent = Console.ReadLine().Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries).ToArray();
- var name = currentStudent[0];
- var age = int.Parse(currentStudent[1]);
- if (!studetns.ContainsKey(name))
- {
- studetns[name] = age;
- }
- }
- var condition = Console.ReadLine();
- var ageFilter = int.Parse(Console.ReadLine());
- var format = Console.ReadLine();
- PrintResult(condition, studetns, ageFilter, format);
- }
- public static Dictionary<string, int> FilteredStudents(string condition, Dictionary<string, int> students, int age)
- {
- switch (condition)
- {
- case "older":
- return students.Where(p => p.Value >= age).ToDictionary(k => k.Key, v => v.Value);
- default:
- return students.Where(p => p.Value < age).ToDictionary(k => k.Key, v => v.Value);
- }
- }
- public static void PrintResult(string condition, Dictionary<string, int> students, int age, string format)
- {
- var filteredStudents = FilteredStudents(condition, students, age);
- switch (format)
- {
- case "name age":
- foreach (var student in filteredStudents)
- {
- Console.WriteLine($"{student.Key} - {student.Value}");
- }
- break;
- case "age":
- foreach (var student in filteredStudents)
- {
- Console.WriteLine(student.Value);
- }
- break;
- case "name":
- foreach (var student in filteredStudents)
- {
- Console.WriteLine(student.Key);
- }
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement