Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.IO;
- namespace MostConnected
- {
- class Program
- {
- static void Main(string[] args)
- {
- //get full path to the CSV
- string listPath = Environment.CurrentDirectory + @"\all.csv";
- //just for convenience
- int column = 2;
- //but does it even exist?
- if (!System.IO.File.Exists(listPath))
- {
- Console.WriteLine("Execution can not continue: \"all.csv\" not found.");
- Console.ReadLine();
- return;
- }
- //nevermind, it exists, create a nice dictionary then
- Dictionary<int, int> connections = new Dictionary<int, int>();
- //and here stuff might start to fail
- try
- {
- //init a reader
- StreamReader read = System.IO.File.OpenText(listPath);
- //lets ensure we always close the stream
- using (read)
- {
- string line;
- int linei = 1;
- //read till end
- while (!read.EndOfStream)
- {
- line = read.ReadLine();
- //split the row in 3, by columns
- string[] values = line.Split(new string[] { "," }, 3, StringSplitOptions.None);
- //if columns are under 3, something has failed, return
- if (values.Length < 3)
- {
- Console.WriteLine("Execution can not continue: invalid entry at line {0}, column count under than 3.", linei.ToString());
- Console.ReadLine();
- return;
- }
- int personID;
- //if this isnt a number or something, we can't continue either
- try
- {
- personID = int.Parse(values[column - 1]);
- }
- catch (FormatException)
- {
- Console.WriteLine("Execution can not continue: invalid entry at line {0}, column {1}.", linei.ToString(), column.ToString());
- Console.ReadLine();
- return;
- }
- //if key doesnt exists, lets add it
- if (!connections.ContainsKey(personID))
- {
- connections.Add(personID, 0);
- }
- //increase value at the specified key
- connections[personID]++;
- linei++;
- }
- }
- int max = -1;
- //get highest count
- foreach (int key in connections.Keys)
- {
- if (max == -1) max = key;
- if (connections[key] > connections[max])
- {
- max = key;
- }
- }
- //print out
- Console.WriteLine("Most connected person: {0} with {1} connections.", max.ToString(), connections[max].ToString());
- }
- catch (Exception ex)
- {
- 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());
- }
- Console.ReadLine();
- }
- }
- }
Add Comment
Please, Sign In to add comment