Guest User

Untitled

a guest
Jan 12th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5. namespace MostConnected
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             //get full path to the CSV
  12.             string listPath = Environment.CurrentDirectory + @"\all.csv";
  13.             //just for convenience
  14.             int column = 2;
  15.             //but does it even exist?
  16.             if (!System.IO.File.Exists(listPath))
  17.             {
  18.                 Console.WriteLine("Execution can not continue: \"all.csv\" not found.");
  19.                 Console.ReadLine();
  20.                 return;
  21.             }
  22.             //nevermind, it exists, create a nice dictionary then
  23.             Dictionary<int, int> connections = new Dictionary<int, int>();
  24.             //and here stuff might start to fail
  25.             try
  26.             {
  27.                 //init a reader
  28.                 StreamReader read = System.IO.File.OpenText(listPath);
  29.                 //lets ensure we always close the stream
  30.                 using (read)
  31.                 {
  32.                     string line;
  33.                     int linei = 1;
  34.                     //read till end
  35.                     while (!read.EndOfStream)
  36.                     {
  37.                         line = read.ReadLine();
  38.                         //split the row in 3, by columns
  39.                         string[] values = line.Split(new string[] { "," }, 3, StringSplitOptions.None);
  40.                         //if columns are under 3, something has failed, return
  41.                         if (values.Length < 3)
  42.                         {
  43.                             Console.WriteLine("Execution can not continue: invalid entry at line {0}, column count under than 3.", linei.ToString());
  44.                             Console.ReadLine();
  45.                             return;
  46.                         }
  47.                         int personID;
  48.                         //if this isnt a number or something, we can't continue either
  49.                         try
  50.                         {
  51.                             personID = int.Parse(values[column - 1]);
  52.                         }
  53.                         catch (FormatException)
  54.                         {
  55.                             Console.WriteLine("Execution can not continue: invalid entry at line {0}, column {1}.", linei.ToString(), column.ToString());
  56.                             Console.ReadLine();
  57.                             return;
  58.                         }
  59.                         //if key doesnt exists, lets add it
  60.                         if (!connections.ContainsKey(personID))
  61.                         {
  62.                             connections.Add(personID, 0);
  63.                         }
  64.                         //increase value at the specified key
  65.                         connections[personID]++;
  66.                         linei++;
  67.                     }
  68.                 }
  69.  
  70.                 int max = -1;
  71.                 //get highest count
  72.                 foreach (int key in connections.Keys)
  73.                 {
  74.                     if (max == -1) max = key;
  75.                     if (connections[key] > connections[max])
  76.                     {
  77.                         max = key;
  78.                     }
  79.                 }
  80.                 //print out
  81.                 Console.WriteLine("Most connected person: {0} with {1} connections.", max.ToString(), connections[max].ToString());
  82.             }
  83.             catch (Exception ex)
  84.             {
  85.                 Console.WriteLine("An exception unfortunately got thrown while trying to access/read the Excell workbook. This exception info could prove useful: {0}{1}", Environment.NewLine, ex.ToString());
  86.  
  87.             }
  88.             Console.ReadLine();
  89.         }
  90.     }
  91. }
Add Comment
Please, Sign In to add comment