Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.69 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace MatrixExercise
  10. {
  11.     class Program
  12.     {
  13.         static void Main(string[] args)
  14.         {
  15.             var sw = Stopwatch.StartNew();
  16.             var union = new HashSet<Int32>();
  17.             var result = new HashSet<Int32>();
  18.             foreach (var row in GetRowFromFile("Matrix.txt"))
  19.             {
  20.                 var copy = new int[row.Count];
  21.                 row.CopyTo(copy);
  22.                 row.IntersectWith(union);
  23.                 union.UnionWith(copy);
  24.                 result.UnionWith(row);
  25.             }
  26.  
  27.             sw.Stop();
  28.             Console.WriteLine("Time: " + sw.Elapsed);
  29.  
  30.             foreach (var i in result)
  31.             {
  32.                 Console.WriteLine(i);
  33.             }
  34.             Console.ReadKey();
  35.         }
  36.  
  37.  
  38.         static IEnumerable<HashSet<int>> GetRowFromFile(string path)
  39.         {
  40.             return ReadLines(() => File.OpenText(path), ParseLine);
  41.         }
  42.  
  43.         private static HashSet<Int32> ParseLine(string line)
  44.         {
  45.             var numbers = line.Split(' ');
  46.             var intList = new HashSet<int>(numbers.Select(Int32.Parse));
  47.             return intList;
  48.         }
  49.  
  50.         static IEnumerable<T> ReadLines<T>(Func<TextReader> provider, Func<string, T> morpher)
  51.         {
  52.             using (var reader = provider())
  53.             {
  54.                 string line;
  55.                 while ((line = reader.ReadLine()) != null)
  56.                 {
  57.                     yield return morpher(line);
  58.                 }
  59.             }
  60.         }
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement