-
Notifications
You must be signed in to change notification settings - Fork 0
/
Challenge3.cs
156 lines (128 loc) · 4.76 KB
/
Challenge3.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Challenge 3 - Fortunata and Jacinta
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
class Challenge3
{
static string InputBookName = @"/Inputs/Challenge 3/pg17013.txt";
static string InputFileName = @"/Inputs/Challenge 3/submitInput.txt";
static string OutputFileName = @"/Outputs/Challenge 3/submitOutput.txt";
static Dictionary<string, int> Dictionary = new Dictionary<string, int>();
static IOrderedEnumerable<KeyValuePair<string, int>> DictionarySort;
static List<string> UnicodeOrder = new List<string>() { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "á", "é", "í", "ñ", "ó", "ú", "ü" };
static void Main()
{
GenerateDictionary();
string[] input = File.ReadAllLines(InputFileName, Encoding.UTF8);
int casesTotal = int.Parse(input[0]);
string[] output = new string[casesTotal];
for (int i = 0; i < casesTotal; i++)
{
string wordOrDigits = input[i + 1];
if (IsDigitsOnly(wordOrDigits))
{
int number = int.Parse(wordOrDigits);
var item = DictionarySort.ElementAt(number - 1);
output[i] = string.Format("Case #{0}: {1} {2}", i + 1, item.Key, item.Value);
}
else
{
var value = DictionarySort.First(x => x.Key == wordOrDigits).Value;
var key = wordOrDigits;
var index = DictionarySort.ToList().IndexOf(
new KeyValuePair<string, int>(key, value)) + 1;
output[i] = string.Format("Case #{0}: {1} #{2}", i + 1, value, index);
}
}
var utf8WithoutBOM = new UTF8Encoding(false); // Thanks to the Tuenti engineers :)
File.WriteAllLines(OutputFileName, output, utf8WithoutBOM);
}
static void GenerateDictionary()
{
string line;
using (StreamReader book = new StreamReader(InputBookName, Encoding.Default))
{
while ((line = book.ReadLine()) != null)
{
var words = GetWords(line);
// Save Dictionary
var wordsReplace = ReplaceCharsWords(words);
for (int i = 0; i < wordsReplace.Length; i++)
{
var word = wordsReplace[i];
if (Dictionary.ContainsKey(word))
{
int total = Dictionary[word] + 1;
Dictionary[word] = total;
}
else
{
Dictionary.Add(word, 1);
}
}
}
}
// Sort dictionary frecuency and order unicode
DictionarySort = Dictionary.OrderByDescending(item => item.Value)
.ThenBy(item => item, new UnicodeOrderCompare());
}
static string[] GetWords(string line)
{
line = line.ToLower();
return line.ToLower()
.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Where(x => x.Length > 2)
.ToArray();
}
static string[] ReplaceCharsWords(string[] words)
{
var wordsInLine = string.Join(" ", words);
var reg = new Regex(@"[^" + string.Join("", UnicodeOrder) + "]");
return reg.Replace(wordsInLine, " ")
.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
.Where(x => x.Length > 2)
.ToArray();
}
static bool IsDigitsOnly(string str)
{
foreach (char c in str)
{
if (c < '0' || c > '9')
return false;
}
return true;
}
public class UnicodeOrderCompare : IComparer<KeyValuePair<string, int>>
{
public int Compare(KeyValuePair<string, int> x, KeyValuePair<string, int> y)
{
var i = 0;
var j = 0;
while (i < x.Key.Length && j < y.Key.Length)
{
var aVal = UnicodeOrder.IndexOf(x.Key[i].ToString());
var bVal = UnicodeOrder.IndexOf(y.Key[j].ToString());
if (aVal < bVal)
{
return -1;
}
else if (aVal > bVal)
{
return 1;
}
i++;
j++;
}
if (x.Key.Length > i)
return 1;
else if (y.Key.Length > j)
return -1;
else
return 0;
// return string.Compare(x.Key, y.Key, StringComparison.CurrentCulture);
}
}
}