Advertisement
YavorGrancharov

Default_Values(lambda)

Jul 17th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Default_Values
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Dictionary<string, string> stock = new Dictionary<string, string>();
  12.             string[] input = Console.ReadLine()
  13.                 .Split(new string[] { " -> " }, StringSplitOptions.RemoveEmptyEntries); ;
  14.  
  15.             while (input[0] != "end")
  16.             {
  17.                 string leftEntry = input[0];
  18.                 string rightEntry = input[1];
  19.  
  20.                 if (!stock.ContainsKey(leftEntry))
  21.                 {
  22.                     stock[leftEntry] = rightEntry;
  23.                 }
  24.                 input = Console.ReadLine()
  25.                     .Split(new string[] { " -> " }, StringSplitOptions.RemoveEmptyEntries); ;
  26.             }
  27.             string defaultInput = Console.ReadLine();
  28.  
  29.             Dictionary<string, string> ordered = stock
  30.                 .Where(p => p.Value != "null")
  31.                 .OrderByDescending(p => p.Value.Length)
  32.                 .ToDictionary(p => p.Key, p => p.Value);
  33.  
  34.             Dictionary<string, string> nulls = stock
  35.                 .Where(v => v.Value == "null")
  36.                 .ToDictionary(v => v.Key, v => defaultInput);
  37.  
  38.             Dictionary<string, string> concat = ordered
  39.                 .Concat(nulls)
  40.                 .ToDictionary(p => p.Key, p => p.Value);
  41.  
  42.             foreach (var pair in concat)
  43.             {
  44.                 Console.WriteLine("{0} <-> {1}", pair.Key, pair.Value);
  45.             }
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement