Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.31 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace RegisteredUSers
  6. {
  7.     internal class Program
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             string input = Console.ReadLine();
  12.             Dictionary<string, string> dic = new Dictionary<string, string>();
  13.             Dictionary<string, string> dic2 = new Dictionary<string, string>();
  14.             while (input != "end")
  15.             {
  16.                 string[] inputTokens =
  17.                 input.Split(new string[] { " -> " }, StringSplitOptions.RemoveEmptyEntries);
  18.  
  19.                 string key = inputTokens[0];
  20.                 string value = inputTokens[1];// <= хубаво е да е коректния индекс :)
  21.  
  22.                 // според мен това е по добър вариянт за добавяне и променяне на стоиности в
  23.                 // в речник
  24.                 // проверяваш дали НЕсъществува и създаваш ключа и след проверката му
  25.                 // задаваш стоиност.
  26.                 if (!dic.ContainsKey(key))
  27.                 {
  28.                     dic[key] = string.Empty;
  29.                 }
  30.                 dic[key] = value;
  31.  
  32.  
  33.  
  34.                 input = Console.ReadLine();
  35.             }
  36.  
  37.             string defaultValue = Console.ReadLine();
  38.  
  39.             foreach (var item in dic)
  40.             {
  41.                 if (item.Value == "null")
  42.                 {
  43.                     dic2.Add(item.Key, defaultValue);
  44.                 }
  45.                 //dic.Remove(item.Value);
  46.             }
  47.             //когато го сортираш или променяш трябва да го зададеш като стоиност на себе си или
  48.             //или на друга колекция
  49.             dic = dic.Where(x => x.Value != "null")
  50.                 .OrderByDescending(x => x.Value.Length)
  51.                 .ToDictionary(x => x.Key, x => x.Value);
  52.  
  53.             foreach (var item in dic)
  54.             {
  55.                 Console.WriteLine($"{item.Key} <-> {item.Value}");
  56.             }
  57.  
  58.  
  59.             foreach (var item in dic2)
  60.             {
  61.                 Console.WriteLine($"{item.Key} <-> {item.Value}");
  62.             }
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement