/
P07_PopulationCounter.cs
69 lines (59 loc) · 2.53 KB
/
P07_PopulationCounter.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
using System;
using System.Collections.Generic;
using System.Linq;
namespace P07_PopulationCounter
{
class P07_PopulationCounter
{
static void Main(string[] args)
{
var countryCityPopulation =
new Dictionary<string, Dictionary<string, ulong>>();
var countryPopulation = new Dictionary<string, ulong>();
GetPopulation(countryCityPopulation, countryPopulation);
PrintReport(countryCityPopulation, countryPopulation);
}
static void GetPopulation(
Dictionary<string, Dictionary<string, ulong>> countryCityPopulation,
Dictionary<string, ulong> countryPopulation)
{
var command = Console.ReadLine();
while (command != "report")
{
var commandLineList = command.Split('|').ToList();
var country = commandLineList[1];
var city = commandLineList[0];
var currentPopulation = ulong.Parse(commandLineList[2]);
if (!countryCityPopulation.ContainsKey(country))
{
countryCityPopulation[country] = new Dictionary<string, ulong>();
countryPopulation[country] = 0;
}
if (!countryCityPopulation[country].ContainsKey(city))
{
countryCityPopulation[country][city] = 0;
}
countryPopulation[country] += currentPopulation;
countryCityPopulation[country][city] += currentPopulation;
countryCityPopulation[country] =
countryCityPopulation[country]
.OrderByDescending(x => x.Value)
.ToDictionary(k => k.Key, x => x.Value);
command = Console.ReadLine();
}
}
static void PrintReport(
Dictionary<string, Dictionary<string, ulong>> countryCityPopulation,
Dictionary<string, ulong> countryPopulation)
{
countryPopulation = countryPopulation.OrderByDescending(x => x.Value).ToDictionary(k => k.Key, x => x.Value);
foreach (var country in countryPopulation)
{
Console.WriteLine($"{country.Key} (total population: {country.Value})");
Console.WriteLine(string.Join("\n",
countryCityPopulation[country.Key]
.Select(cityPopulation => $"=>{cityPopulation.Key}: {cityPopulation.Value}")));
}
}
}
}