-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
103 lines (90 loc) · 3.64 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace BigDataHackDriver
{
class Program
{
static void Main(string[] args)
{
var reader = new StreamReader(File.OpenRead(@"C:\users\latra\desktop\usersProjects.csv"));
var data = readData(reader);
var links = countLinks(data);
var sortedLinks = sortLinks(links);
}
public static Dictionary<string, Dictionary<string, int>> sortLinks(Dictionary<string, Dictionary<string, int>> links)
{
Dictionary<string, Dictionary<string, int>> sortedLinks = new Dictionary<string, Dictionary<string, int>>();
foreach (var country in links.Keys)
{
Dictionary<string, int> linkCounts = new Dictionary<string, int>();
linkCounts = links[country];
var sorted = linkCounts.Select(d => d).OrderByDescending(d => d.Value).ToDictionary(k => k.Key, e => e.Value);
sortedLinks.Add(country,sorted);
}
return sortedLinks;
}
public static Dictionary<string, Dictionary<string, int>> countLinks(Dictionary<string, List<string>> data)
{
Dictionary<string, Dictionary<string, int>> links = new Dictionary<string, Dictionary<string, int>>();
foreach (var key in data.Keys)
{
var countryList = data[key]; //list of strings (countries)
var numCountries = countryList.Count;
for(int i=0; i < numCountries; i++)
{
var country1 = countryList[i];
for(int j=0; j < numCountries; j++)
{
var country2 = countryList[j];
if (!country1.Equals(country2))
{
Dictionary<string, int> linkCounts = new Dictionary<string, int>();
if (links.ContainsKey(country1))
{
linkCounts = links[country1];
if (linkCounts.ContainsKey(country2))
{
linkCounts[country2]++;
}
else
{
linkCounts.Add(country2, 1);
}
}
else
{
linkCounts.Add(country2, 1);
links.Add(country1, linkCounts);
}
}
}
}
}
return links;
}
public static Dictionary<string, List<string>> readData(StreamReader reader)
{
Dictionary<string, List<string>> data = new Dictionary<string, List<string>>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
var repoID = values[1];
var country = values[2];
country = country.Replace("\"", "");
List<string> countries;
if (!data.TryGetValue(repoID, out countries))
{
countries = new List<string>();
data[repoID] = countries;
}
countries.Add(country);
}
return data;
}
}
}