Advertisement
krasi1105

LINQuistics

May 11th, 2017
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. public class Program
  6. {
  7.     public static void Main()
  8.     {
  9.         string input;
  10.         var collections = new Dictionary<string, HashSet<string>>();
  11.         while (!(input = Console.ReadLine()).Equals("exit"))
  12.         {
  13.             var inputSplit = input.Split('.');
  14.  
  15.             if (inputSplit.Length == 1)
  16.             {
  17.                 int digit;
  18.                 bool isDigit = int.TryParse(inputSplit[0], out digit);
  19.                 if (isDigit)
  20.                 {
  21.                     if (collections.Count > 0)
  22.                     {
  23.                         var methodNames = collections.Values
  24.                             .First(m => m.Count == collections.Values.Max(c => c.Count))
  25.                             .OrderBy(m => m.Length)
  26.                             .Take(digit);
  27.                         foreach (var method in methodNames)
  28.                         {
  29.                             Console.WriteLine($"* {method}");
  30.                         }
  31.                     }
  32.                 }
  33.                 else
  34.                 {
  35.                     if (collections.ContainsKey(inputSplit[0]))
  36.                     {
  37.                         var methodNames = collections[inputSplit[0]]
  38.                             .OrderByDescending(m => m.Length)
  39.                             .ThenByDescending(m => m.Distinct().Count());
  40.                         foreach (var methodName in methodNames)
  41.                         {
  42.                             Console.WriteLine($"* {methodName}");
  43.                         }
  44.                     }
  45.                 }
  46.             }
  47.             else
  48.             {
  49.                 string collectionName = inputSplit[0];
  50.                 var methods = inputSplit.Skip(1);
  51.                 if (!collections.ContainsKey(collectionName))
  52.                 {
  53.                     collections[collectionName] = new HashSet<string>();
  54.                 }
  55.                 foreach (var method in methods)
  56.                 {
  57.                     var noBrackets = method.Trim('(', ')');
  58.                     collections[collectionName].Add(noBrackets);
  59.                 }
  60.             }
  61.         }
  62.  
  63.         string[] lastLine = Console.ReadLine().Split();
  64.         var searchedMethod = lastLine[0];
  65.         var selection = lastLine[1];
  66.  
  67.         var result = collections
  68.             .Where(c => c.Value.Contains(searchedMethod))
  69.             .OrderByDescending(c => c.Value.Count)
  70.             .ThenByDescending(c => c.Value.Min(m => m.Length));
  71.         foreach (var collection in result)
  72.         {
  73.             Console.WriteLine(collection.Key);
  74.             if (selection.Equals("all"))
  75.             {
  76.                 var methods = collection.Value
  77.                     .OrderByDescending(m => m.Length);
  78.                 foreach (var method in methods)
  79.                 {
  80.                     Console.WriteLine($"* {method}");
  81.                 }
  82.             }
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement