-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmostViewedWriters.cs
218 lines (156 loc) · 5.22 KB
/
mostViewedWriters.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
int[][][] mostViewedWriters(int[][] topicIds, int[][] answerIds, int[][] views)
{
//1. Extract the unique topicIds
int[] uniqueTopicIds = abstractTopicIds(topicIds);
foreach (int id in uniqueTopicIds)
{
// Console.WriteLine("id: " + id);
}
//2. Create the return int[][][]
int[][][] mostViewedWritersArray = new int[uniqueTopicIds.Length][][];
//3. Loop through each mostViewedWritersArray adding the sorted data
for (var i = 0; i < mostViewedWritersArray.Length; i++)
{
//3A. Get an array of all the Questions linked to the specific Topic
var currentUniqueId = uniqueTopicIds[i];
int[] topicIdQuestionsAnswers = GetQuestionsLinkedWithId(currentUniqueId, topicIds);
foreach (int question in topicIdQuestionsAnswers)
{
// Console.WriteLine("question: " + question);
}
//3B. Get Answers ids from questions
int[] answerIdForTopic = GetAnswerIds(topicIdQuestionsAnswers, answerIds);
//3C. Get List of views for all answersFromViewIds. List so can edit them
var viewList = new List<int[]>();
viewList = ViewList(answerIdForTopic, views);
//3D. Sort and shorten list and make it into a int[][]
var jaggedArray = SortShorten(viewList);
mostViewedWritersArray[i] = jaggedArray;
}
//N. Retun the array
return mostViewedWritersArray;
}
public int[][] SortShorten(List<int[]> _viewList)
{
//1. combine like users in list
for (var i = 0; i < _viewList.Count; i++)
{
var userId = _viewList[i][0];
for (var j = i + 1; j < _viewList.Count; j++)
{
if (_viewList[j][0] == userId)
{
_viewList[i][1] += _viewList[j][1];//may be wrong...
_viewList.RemoveAt(j);
break;
}
}
}
//2. sort the list based on views then unser ids
//go one at a time finding highest then store it. if a tie lower userid breaks tie
for (var i = 0; i < _viewList.Count; i++)
{
var currentHighest = _viewList[i];//can swap with this later if needbe
for (var j = i + 1; j < _viewList.Count; j++)
{
if (_viewList[j][1] > currentHighest[1] || ((_viewList[j][1] == currentHighest[1] && _viewList[j][0] < currentHighest[0])))
{
var newHighest = _viewList[j];
_viewList[j] = currentHighest;
currentHighest = newHighest;
}
}
_viewList[i] = currentHighest;
}
//checks if more than 10 users
if (_viewList.Count > 10 && _viewList.Any())
{
for (var i = 0; i < (_viewList.Count - 10); i++)
{
_viewList.RemoveAt(_viewList.Count - 1);
}
}
//3. Turn into array
var sortShorten = new int[_viewList.Count][];
for (var i = 0; i < sortShorten.Length; i++)
{
sortShorten[i] = _viewList[i];
}
return sortShorten;
}
public int[] GetAnswerIds(int[] questions, int[][] _answerIds)
{
var listOfAnswerIds = new List<int>();
for (var i = 0; i < questions.Length; i++)
{
for (var j = 0; j < _answerIds[questions[i]].Length; j++)
{
listOfAnswerIds.Add(_answerIds[questions[i]][j]);
}
}
var returnArray = new int[listOfAnswerIds.Count];
for (var i = 0; i < returnArray.Length; i++)
{
returnArray[i] = listOfAnswerIds[i];
// Console.WriteLine("List of answer id: " + returnArray[i]);
}
return returnArray;
}
public List<int[]> ViewList(int[] _questions, int[][] _views)
{
var ListToReturn = new List<int[]>();
for (var i = 0; i < _questions.Length; i++)
{
for (var j = 0; j < _views.Length; j++)
{
if (_questions[i] == _views[j][0])
{
var userIdAnswer = new int[2];
userIdAnswer[0] = _views[j][1];//user id
userIdAnswer[1] = _views[j][2];//answer view count
ListToReturn.Add(userIdAnswer);
// Console.WriteLine("UserId: " + userIdAnswer[0] + " View Count: " + userIdAnswer[1]);
}
}
}
return ListToReturn;
}
public int[] GetQuestionsLinkedWithId(int _id, int[][] _topicIds)
{
var questions = new List<int>();
for (var i = 0; i < _topicIds.Length; i++)
{
if (Array.IndexOf(_topicIds[i], _id) != -1)
{
questions.Add(i);
}
}
var returnArray = new int[questions.Count];
for (var j = 0; j < returnArray.Length; j++)
{
returnArray[j] = questions[j];
}
return returnArray;
}
public int[] abstractTopicIds(int[][] _topicIds)
{
var uniqueIds = new List<int>();
for (var i = 0; i < _topicIds.Length; i++)
{
for (var j = 0; j < _topicIds[i].Length; j++)
{
var currentIdValue = _topicIds[i][j];
if (uniqueIds.IndexOf(currentIdValue) == -1)
{
uniqueIds.Add(currentIdValue);
}
}
}
uniqueIds.Sort();
var unigueIdArray = new int[uniqueIds.Count];
for (var i = 0; i < unigueIdArray.Length; i++)
{
unigueIdArray[i] = uniqueIds[i];
}
return unigueIdArray;
}