Advertisement
simonradev

GroupNumbers

May 30th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.62 KB | None | 0 0
  1. namespace GroupNumbers
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.  
  8.     public class Startup
  9.     {
  10.         private static Dictionary<int, HashSet<int>> matrixImitation;
  11.         private static int currentIndex;
  12.  
  13.         public static void Main()
  14.         {
  15.             matrixImitation = new Dictionary<int, HashSet<int>>
  16.             {
  17.                 [0] = new HashSet<int>(),
  18.                 [1] = new HashSet<int>(),
  19.                 [2] = new HashSet<int>()
  20.             };
  21.  
  22.             currentIndex = 0;
  23.  
  24.             int[] allElements = Console.ReadLine()
  25.                                 .Split(new[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries)
  26.                                 .Select(ParseStringsAndPlaceThemWhereNeeded)
  27.                                 .ToArray();
  28.  
  29.             StringBuilder result = new StringBuilder();
  30.             foreach (KeyValuePair<int, HashSet<int>> kvp in matrixImitation)
  31.             {
  32.                 foreach (int indexOfElement in kvp.Value)
  33.                 {
  34.                     result.Append(allElements[indexOfElement]);
  35.                     result.Append(" ");
  36.                 }
  37.  
  38.                 result.AppendLine();
  39.             }
  40.  
  41.             Console.Write(result);
  42.         }
  43.  
  44.         private static int ParseStringsAndPlaceThemWhereNeeded(string toParse)
  45.         {
  46.             int number = int.Parse(toParse);
  47.  
  48.             int remainder = Math.Abs(number) % 3;
  49.  
  50.             matrixImitation[remainder].Add(currentIndex);
  51.  
  52.             currentIndex++;
  53.  
  54.             return number;
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement