-
Notifications
You must be signed in to change notification settings - Fork 0
/
Algorithms.cs
494 lines (430 loc) · 14.1 KB
/
Algorithms.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace Arrays
{
public class Algorithms
{
public Algorithms()
{
}
public static bool ContainsDuplicates(params int[] x)
{
var dictionary = new Dictionary<int, int>();
foreach (int i in x)
{
if (dictionary.ContainsKey(i))
{
Console.WriteLine($"Duplicate key found: {i}");
return true;
}
else
{
dictionary.Add(i, 1);
}
}
return false;
}
public static void QuestionOne()
{
}
public static String FilterBrackets(String str)
{
int len = str.Length;
char[] arr = str.ToCharArray();
String filter = "";
for (int i = 0; i < len; i++)
{
if ((arr[i] == '(') || (arr[i] == ')') || (arr[i] == '[') || (arr[i] == ']') || (arr[i] == '{') || (arr[i] == '}'))
{
filter += arr[i];
}
}
return filter;
}
/// <summary>
/// filter out any erroneous characters from the input string which are essentially space characters
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
public static String FilterErroneousChars(String input)
{
int len = input.Length;
char[] arr = input.ToCharArray();
String filter = "";
for (int i = 0; i < len; i++)
{
if (arr[i].Equals('(') || arr[i].Equals(')') || arr[i].Equals('[') || arr[i].Equals(']') || arr[i].Equals('{') || arr[i].Equals('}'))
{
filter += arr[i];
}
}
return filter;
}
private delegate List<int> myList(int[] arr);
public static bool QuestionTwo(int[] arr, int target, int num)
{
int sum = 0;
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length; j++)
{
for (int x = 0; x < arr.Length; x++)
{
if ((i != j) && (j != x))
{
//verify if the number is present as part of the additional constraint!
int cnt = NumberOfOccurances(arr, num);
if (cnt > 0)
{
//checking the required number i.e. 7 is present as output
if ((arr[i] == num) || (arr[j] == num) || (arr[x] == num))
{
sum = arr[i] + arr[j] + arr[x];
}
if (sum == target)
{
Console.WriteLine("The numbers are: {0},{1},{2}", arr[i], arr[j], arr[x]);
Console.ReadKey();
return true;
}
}
else
{
sum = arr[i] + arr[j];
if (sum == target)
{
Console.WriteLine("The numbers are: {0},{1}", arr[i], arr[j]);
Console.ReadKey();
return true;
}
}
}
}
}
}
return false;
}
public static int NumberOfOccurances(int[] arr, int isNumPresent)
{
int cnt = 0;
for (int i = 0; i < arr.Length; i++)
{
//check to see if the 'isNumPresent' is present; well, in this case the #7
if (arr[i] == isNumPresent)
{
cnt++;
}
}
return cnt;
}
public static void QuestionTwo()
{
int target = 14;
int sum = 0;
int[] arr = { 4, 3, 21, 6, 7 };
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length; j++)
{
for (int x = 0; x < arr.Length; x++)
{
if ((i != j) && (j != x))
{
sum = arr[i] + arr[j] + arr[x];
if (sum == target)
{
Console.WriteLine("Nailed it!!!");
Console.WriteLine("The numbers are: {0},{1},{2}", arr[i], arr[j], arr[x]);
}
}
}
}
}
Console.ReadLine();
}
/// <summary>
/// this will get the alternate number(s) in the array and add it to the list objects
/// to print alternate numbers, change i++ to i+=
/// </summary>
public static void QuestionsThree()
{
int[] arr = { 7, 2, 1, 3 };
List<int> list1 = new List<int>();
List<int> list2 = new List<int>();
List<int> listFinal = new List<int>();
for (int x = 0; x < arr.Length; x += 2)
{
list1.Add(arr[x]);
}
//this block of code will add those elements to the list that have an index that is even
for (int i = 0; i < arr.Length; i++)
{
if ((i + 1) % 2 == 0)
{
list2.Add(arr[i]);
}
}
int totCnt = (list1.Count + list2.Count) / 2;
for (int c = 0; c < totCnt; c++)
{
listFinal.Add(list1[c]);
listFinal.Add(list2[c]);
}
Console.WriteLine(listFinal.ToList());
Console.ReadKey();
}
internal static void FinalList(Func<int[], List<int>> firstHalfList, Func<int[], List<int>> secondHalfList)
{
throw new NotImplementedException();
}
// get every alternate item
public static List<int> FirstHalfList(int[] array)
{
List<int> list = new List<int>();
for (int x = 0; x < array.Length; x += 2)
{
list.Add(array[x]);
}
return list;
}
public static List<int> SecondHalfList(int[] array)
{
List<int> list = new List<int>();
for (int i = 0; i < array.Length; i++)
{
if ((i + 1) % 2 == 0)
{
list.Add(array[i]);
}
}
return list;
}
public static List<int> FinalList(List<int> list1, List<int> list2)
{
List<int> finalList = new List<int>();
int totCnt = (list1.Count + list2.Count) / 2;
for (int c = 0; c < totCnt; c++)
{
finalList.Add(list1[c]);
finalList.Add(list2[c]);
}
return finalList;
}
public static void QuestionsThreeOriginal()
{
int[] arr = { 7, 2, 1, 3 };
List<int> list1 = new List<int>();
List<int> list2 = new List<int>();
List<int> listFinal = new List<int>();
for (int x = 0; x < arr.Length; x += 2)
{
list1.Add(arr[x]);
}
//this block of code will add those elements to the list that have an index that is even
for (int i = 0; i < arr.Length; i++)
{
if ((i + 1) % 2 == 0)
{
list2.Add(arr[i]);
}
}
int totCnt = (list1.Count + list1.Count) / 2;
for (int c = 0; c < totCnt; c++)
{
listFinal.Add(list1[c]);
listFinal.Add(list2[c]);
}
Console.WriteLine(listFinal.ToList());
Console.ReadKey();
}
public static void Factorial()
{
int i, number, fact;
Console.WriteLine("Enter a number");
number = int.Parse(Console.ReadLine());
fact = number;
for (i = number - 1; i >= 1; i--)
{
fact *= i;
}
Console.WriteLine("\nFactorial of given number is: " + fact);
Console.ReadLine();
}
public static void TryIt()
{
var x = 1;
for (int i = 0; i < 3; i++)
{
x += 5 * i;
Console.WriteLine(x);
}
Console.ReadLine();
}
public static void GetTemperatureCloseToZero()
{
double temp = 50.0; //some arbitrary value
double diff;
int target = 0;
double[] arr = { 7, 7.9, 10.5, -4, 3, -6, -2, 1, -15, 11.3, 9, -1 };
for (int i = 0; i < arr.Length; i++)
{
diff = Math.Abs(Convert.ToDouble(arr[i])) - Convert.ToDouble(target);
if (diff <= temp)
{
temp = diff;
}
}
Console.WriteLine("The closest value to 0 is: {0}", temp);
Console.ReadLine();
}
public static void GetTempCloseToZeroSort()
{
double[] arr = { 7, 7.9, 10.5, -4, 3, -6, -2, 1, -15, 11.3, 9, -1 };
List<double> list = new List<double>();
foreach (var item in arr)
{
list.Add(Math.Abs(item));
}
list.Sort();
Console.WriteLine("The closest value to 0 is: {0}", list[0]);
Console.ReadLine();
}
public static void TEstMe()
{
var sample = new List<String>();
sample.InsertRange(collection: new List<String>(), index: 0);
sample.InsertRange(index: 0, collection: new List<string>());
}
//A palindrome is a word that reads the same backward or forward.
public static bool IsPalindrome(string word)
{
string original = word.ToLower();
string result = "";
for (int i = word.Length - 1; i >= 0; i--)
{
result += original[i];
}
if (original == result)
{
return true;
}
else
{
return false;
}
}
public static bool Exists(int[] ints, int k)
{
for (int i = 0; i < ints.Length; i++)
{
if (ints[i] == k)
{
return true;
}
}
return false;
}
public static string ReverseString(string s)
{
string result = string.Empty;
string[] temp = s.Split(' ');
Array.Reverse(temp);
for (int i = 0; i <= temp.Length - 1; i++)
{
result += temp[i] + ' ';
//Console.WriteLine(temp[i] + "" + ' ');
}
return result;
}
}
public abstract class TextInput
{
public abstract void Add(char c);
public abstract string GetValue();
}
public class NumericInput : TextInput
{
private string finalResult;
public override void Add(char c)
{
if (Char.IsDigit(c))
{
finalResult += Convert.ToString(c);
}
GetValue();
}
public override string GetValue()
{
return finalResult;
}
}
public class MathUtils
{
public static double Average(int a, int b)
{
return (a + b) / 2;
}
}
public class XmlTest
{
public static IEnumerable<string> FolderNames(string xml, char startingLetter)
{
XDocument xmlDoc = XDocument.Parse(xml);
var val = xmlDoc.Descendants("folder").ToList();
var result = val.Where(p => p.FirstAttribute.Value.StartsWith(startingLetter.ToString())).Select(p => p.FirstAttribute.Value).ToList();
return result;
}
}
public class McKeenson
{
public static void PrintNumAndAnimals()
{
for (int i = 1; i < 100; i++)
{
// a number 15 will print Elephant
if (i == 15)
{
Console.WriteLine("Elephant");
}
// multiples of 3 will print Cat
else if (i % 3 == 0)
{
Console.WriteLine("Cat");
}
// multiples of 5 will print Dog
else if (i % 5 == 0)
{
Console.WriteLine("Dog");
}
else
{
Console.WriteLine(i.ToString());
}
}
Console.ReadLine();
}
}
public class TomCat
{
//Find the maximum number of even numbers
public static void MaxNoOfEvenNumbers()
{
List<int> myList = new List<int>();
int cnt = 0;
int[] arr = { 9, 8, 3, 4, 5, 6 };
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] % 2 == 0)
{
myList.Add(cnt);
cnt++;
}
}
Console.WriteLine($"Total number of even numbers are: {cnt}");
Console.ReadLine();
}
}
}