-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatternProbCLS.cs
655 lines (597 loc) · 21.5 KB
/
patternProbCLS.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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization.Metadata;
using System.Threading.Tasks;
namespace CodePractice
{
public interface iSample
{
void printM(string val = "Interface Executed");
}
public class Sample : iSample
{
public void printM(string val = "Class Executed")
{
Console.WriteLine(val);
}
}
internal class patternProbCLS
{
public void pyramidPattern()
{
Console.WriteLine("Pyramid Pattern Problem : Using StringBuilder!");
StringBuilder strBuild = new StringBuilder();
Console.Write("Enter Number of Rows : ");
int row = Convert.ToInt32(Console.ReadLine());
//For R1 C1, R2 C3, R3 C5 in odd number increases with 2;
//by Using Linear Equation C=mR+b and Finding b (e.q. : -1) by putting C and R is 1&1 as first case,
//m is slope exponational who increases with 2, So our formula becames [fn : C=2R-1]
int column = 2 * row - 1;
Console.WriteLine("\n");
double center = Math.Round(((double)column / 2), MidpointRounding.AwayFromZero);
string space = " ";
string s = "*";
center = center - 1;
int numStar;
int rowL = row;
//Row Execution
for (int r = 0; r < row; r++)
{
numStar = (int)center;
//For Print Space
//upto 3-1 print space
for (int i = 0; i < center; i++)
{
strBuild.Append(space);
}
//print star here
for (int j = rowL; j > numStar; j--)
{
strBuild.Append(s);
}
center = center - 1;
rowL = rowL + 1;
strBuild.Append("\n");
}
Console.WriteLine(strBuild);
Console.WriteLine("Done!");
Console.ReadLine();
}
public void reverseString()
{
Console.WriteLine("Reverse String");
Console.WriteLine("Please, Enter Your String Here");
string s = Console.ReadLine();
char[] charStr = s.ToCharArray();
StringBuilder reverseStr = new StringBuilder();
for(int i=charStr.Length-1; i>=0; i--)
{
reverseStr.Append(charStr[i]);
};
Console.WriteLine(reverseStr.ToString());
}
public void rightAnglePattern()
{
string s = "*";
Console.WriteLine("Enter Number of Rows You Want!");
int row = Convert.ToInt32(Console.ReadLine());
StringBuilder strPattern = new StringBuilder();
for (int i = 0; i < row; i++)
{
strPattern.Append(s);
Console.WriteLine(strPattern);
}
}
public void isPrimeNumber()
{
Console.Write("Enter a Number : ");
int number = int.Parse(Console.ReadLine());
bool IsPrime = true;
for (int i = 2; i < number / 2; i++)
{
if (number % i == 0)
{
IsPrime = false;
break;
}
}
if (IsPrime)
{
Console.Write("Number is Prime.\n");
}
else
{
Console.Write("Number is not Prime.\n");
}
//Console.ReadKey();
}
public void interfaceWithClass()
{
Sample s = new Sample();
Console.WriteLine("Sample s = new Sample();");
s.printM();
iSample i = s;
Console.WriteLine("iSample i = s;");
i.printM();
Console.ReadKey();
}
public void additionOfArray()
{
//\CodePractice.csproj
string OutputPath = @"..\..\..\\DocGenerated\\FilePath\\GeneratedFile.txt";
string directory = Path.GetDirectoryName(OutputPath);
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory); // Create the directory if it doesn't exist
}
TextWriter textWriter = new StreamWriter(OutputPath, true);
try
{
Console.WriteLine("Enter Array Count");
int arCount = Convert.ToInt32(Console.ReadLine().Trim());
//List<int> ar = Console.ReadLine().TrimEnd().Split(' ').ToList().Select(arTemp => Convert.ToInt32(arTemp)).ToList();
Console.WriteLine("Please Enter Number :");
List<int> ar = new List<int>();
for (int i = 0; i < arCount; i++)
{
Console.WriteLine($"Enter {i+1} Number");
ar.Add(int.Parse(Console.ReadLine()));
}
int result = ArraySum(ar);
textWriter.WriteLine("Result \n{0} : Sum = {1}\n", DateTime.Now, result);
textWriter.Flush();
textWriter.Close();
}
catch (Exception ex)
{
textWriter.WriteLine("Exception \n{0} : Exception = {1}\n", DateTime.Now, ex);
textWriter.Flush();
textWriter.Close();
Console.WriteLine(ex);
throw;
}
}
public static int ArraySum(List<int> ar)
{
int sum = 0;
//foreach (var item in ar)
//{
// sum += item;
//}
for (int i = 0; i < ar.Count; i++)
{
sum += ar[i];
}
Console.WriteLine($"Result {sum}");
return sum;
}
public int[] getArray()
{
Console.WriteLine("Please Enter Your Size of Array");
int n = Convert.ToInt32(Console.ReadLine());
int[] arry = new int[n];
Console.WriteLine("Please! Enter Your Array Values Line by Line");
for (int i = 0; i < n; i++)
{
arry[i] = Convert.ToInt32(Console.ReadLine());
}
return arry;
}
public void starPyramid()
{
Console.WriteLine("Pyramid Pattern Problem : Using StringBuilder!");
StringBuilder strBuild = new StringBuilder();
Console.Write("Enter Number of Rows : ");
int row = Convert.ToInt32(Console.ReadLine());
//For R1 C1, R2 C3, R3 C5 in odd number increases with 2;
//by Using Linear Equation C=mR+b and Finding b (e.q. : -1) by putting C and R is 1&1 as first case,
//m is slope exponational who increases with 2, So our formula becames [fn : C=2R-1]
int column = 2 * row - 1;
Console.WriteLine("\n");
double center = Math.Round(((double)column / 2), MidpointRounding.AwayFromZero);
string space = " ";
string s = "*";
center = center - 1;
int numStar;
int rowL = row;
//Row Execution
for (int r = 0; r < row; r++)
{
numStar = (int)center;
//For Print Space
//upto 3-1 print space
for (int i = 0; i < center; i++)
{
strBuild.Append(space);
}
//print star here
for (int j = rowL; j > numStar; j--)
{
strBuild.Append(s);
}
center = center - 1;
rowL = rowL + 1;
strBuild.Append("\n");
}
Console.WriteLine(strBuild);
Console.WriteLine("Done!");
}
public void armStrongNum()
{
Console.WriteLine("Please Enter Your Number for Inspection"); //1-9, 153, 370, 407 some of armstrong number
string userNum = Console.ReadLine();
char[] userNumDigit = userNum.ToCharArray();
int n = userNumDigit.Length;
//Console.WriteLine(n)
;
int num = Convert.ToInt32(userNum);
int tempNum = num;
double sum = 0;
for (int i = 0; i < n; i++)
{
var rem = tempNum % 10;
sum = sum + Math.Pow(rem, n);
tempNum = tempNum / 10;
//Console.WriteLine(sum);
}
if (sum == num)
{
Console.WriteLine($"Your Number : {num} is an Armstrong Number");
}
else
{
Console.WriteLine($"Your Number : {num} is not matched with calculation {sum}, Hence it's not an Armstrong Number");
}
}
public void removeEvenArry()
{
int[] arry = getArray();
Console.WriteLine("We are now remove all even number from the Array");
List<int> oddNumbers = new List<int>();
for (int i = 0; i < arry.Length; i++)
{
if (arry[i] % 2 != 0)
{
oddNumbers.Add(arry[i]);
}
Console.WriteLine($"[Index : {i}, Value : {arry[i]} ]");
}
int[] tempArry = oddNumbers.ToArray();
Console.WriteLine("Its a new array");
for (int i = 0; i < tempArry.Length; i++)
{
Console.WriteLine($"[Index : {i}, Value : {tempArry[i]} ]");
}
}
public void textEditorFun()
{
var editor = new TextEditorCls();
Console.Write("Please Enter the Text 1: ");
string textInsertion = Console.ReadLine();
editor.ChangeText(textInsertion);
editor.DisplayText();
Console.Write("\nPlease Enter the Text 2: ");
textInsertion = Console.ReadLine();
editor.ChangeText(textInsertion);
editor.DisplayText();
Console.Write("\nPlease Enter the Text 3: ");
textInsertion = Console.ReadLine();
editor.ChangeText(textInsertion);
editor.DisplayText();
Console.WriteLine("\nNow Function are Executed");
editor.Undo();
editor.DisplayText();
editor.Undo();
editor.DisplayText(); // "Nothing to Undo."
}
public void BinarySearch_fn()
{
var objBinary = new BinarySearchCLS();
int[] arr = getArray();
int n = arr.Length;
Array.Sort(arr);
Console.WriteLine("Please Enter Your Key You Want To Search");
int x = Convert.ToInt32(Console.ReadLine());
int result = objBinary.binarySearch(arr, x);
if (result == -1)
Console.WriteLine(
"Element is not present in array");
else
Console.WriteLine("Element is present at "
+ "index " + result);
}
public void starLeftPattern()
{
Console.WriteLine("Left Star Pattern Problem");
Console.Write("Enter Number of Rows : ");
int row = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i <= row; i++)
{
for (int j = 0; j <= i; j++)
{
Console.Write("* ");
}
Console.WriteLine("\n");
}
}
public void starCenterPattern()
{
Console.WriteLine("Center Star Pattern Problem");
Console.Write("Enter Number of Rows : ");
int row = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i <= row; i++)
{
Console.WriteLine("");
for (int spc = 0; spc < row - i; spc++)
{
Console.Write(" ");
}
for (int j = 0; j <= i; j++)
{
Console.Write("* ");
}
}
}
public void reverseArray()
{
int[] arry = getArray();
Console.WriteLine("We are now Reverse these Collection!");
List<int> listCollection = new List<int>();
List<int> tempCollection = new List<int>();
tempCollection = arry.ToList();
int listLen = tempCollection.Count;
int len = listLen;
Console.Write("\n[ ");
for (int i = 1; i <= listLen; i++)
{
listCollection.Add(tempCollection[len - i]);
Console.Write($"{listCollection[i - 1]}, ");
//len--;
//Console.Write($"{listCollection[i]}, ");
}
Console.Write("]");
}
public void frogJump()
{
var objGreedy = new GreedyWay();
int[] arry = getArray();
Console.WriteLine("Using Greedy Way : Can Frog Reach to Destination");
bool ans = objGreedy.canJump(arry);
if (ans)
{
Console.WriteLine("It is possible to reach the last index.");
}
else
{
Console.WriteLine("It is not possible to reach the last index.");
}
}
public void scheduler_Setup()
{
Console.WriteLine("Please, Enter How Many Presentation are there.");
int n = Convert.ToInt32(Console.ReadLine().Trim());
List<int> scheduleStart = new List<int>();
List<int> scheduleEnd = new List<int>();
int start = 0; int End = 0;
for(int i=0; i < n; i++)
{
Console.Write($"Enter Start Time of Presentation {i+1} = ");
var startTime = Convert.ToInt32(Console.ReadLine().Trim());
Console.Write($"Enter End Time of Presentation {i+1} = ");
var EndTime = Convert.ToInt32(Console.ReadLine().Trim());
if(startTime > EndTime)
{
start = EndTime;
End = startTime;
}
else
{
start = startTime;
End = EndTime;
}
scheduleStart.Add(start);
scheduleEnd.Add(End);
}
int result = FunctionResult.number_Of_Presentation(scheduleStart, scheduleEnd);
Console.WriteLine($"{result} No of Presentation, we can attend.");
}
public void checkParentheseOrder()
{
Console.WriteLine("Enter Your Symbols From an Order [{<()>}]");
string inputPattern = Console.ReadLine();
var objParentheses = new Parentheses();
int resultP = objParentheses.checkParenthesesOrder(inputPattern);
Console.WriteLine("result will be "+resultP);
}
public void FindPythagoreanTriplets()
{
int[] arry = getArray();
HashSet<int> sqArray = new HashSet<int>();
foreach (var num in arry)
{
sqArray.Add(num * num);
}
int n=arry.Length;
bool isFound = false;
for (int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
int a = arry[i]; int b = arry[j];
int sumOfSq = a*a + b*b;
if(sqArray.Contains(sumOfSq))
{
int c = (int)Math.Sqrt(sumOfSq);
if(Array.Exists(arry,x=>x==c))
{
isFound= true;
Console.WriteLine($"As per Pythagorean Theoram a,b & c is {a}, {b} & {c}");
}
}
}
}
if(!isFound)
{
Console.WriteLine("As per Pythagorean Theoram a,b & c is not found");
}
}
}
public class TextEditorCls
{
private Stack<string> undoStack = new Stack<string>();
private string currentText = "";
// Adds a new change to the undo stack
public void ChangeText(string newText)
{
undoStack.Push(currentText); // Save the current state before the change
currentText = newText;
Console.WriteLine($"Text Changed: {currentText}");
}
// Undo the last change
public void Undo()
{
if (undoStack.Count > 0)
{
currentText = undoStack.Pop(); // Revert to the previous state
Console.WriteLine($"Undo Successful: {currentText}");
}
else
{
Console.WriteLine("Nothing to Undo.");
}
}
public void DisplayText() => Console.WriteLine($"Current Text: {currentText}\n");
}
public class BinarySearchCLS
{
public int binarySearch(int[] arr, int x)
{
int numIteration = 1;
int low = 0, high = arr.Length - 1;
while (low <= high)
{
int mid = low + (high - low) / 2;
// Check if x is present at mid
if (arr[mid] == x)
{
Console.WriteLine($"Found in {numIteration} Iteration");
return mid;
}
// If x greater, ignore left half
if (arr[mid] < x)
{
low = mid + 1;
}
// If x is smaller, ignore right half
else
{
high = mid - 1;
}
numIteration++;
}
// If we reach here, then element was
// not present
return -1;
}
}
public class GreedyWay
{
public bool canJump(int[] nums)
{
// Initialize the maximum
// index that can be reached
int maxIndex = 0;
int jumpIndex = 0;
// Iterate through each
// index of the array
for (int i = 0; i < nums.Length; i++)
{
if (jumpIndex < maxIndex)
{
jumpIndex = maxIndex;
Console.WriteLine("Frog Jump on Index : " + jumpIndex);
}
// If the current index is greater
// than the maximum reachable index
// it means we cannot move forward
// and should return false
if (i > maxIndex)
{
return false;
}
// Update the maximum index
// that can be reached by comparing
// the current maxIndex with the sum of
// the current index and the
// maximum jump from that index
maxIndex = Math.Max(maxIndex, i + nums[i]);
}
// If we complete the loop,
// it means we can reach the
// last index
return true;
}
}
public class FunctionResult
{
public static int number_Of_Presentation(List<int> scheduleStart, List<int> scheduleEnd)
{
List<(int start, int End)> presentations = new List<(int start, int End)>();
for(int i=0; i<scheduleStart.Count; i++)
{
presentations.Add((scheduleStart[i], scheduleEnd[i]));
}
presentations.Sort((a,b)=> a.End!=b.End ? a.End.CompareTo(b.End) : a.start.CompareTo(b.start));
int maxCount = 0;
int lastEndTime = 0;
foreach (var presentation in presentations)
{
if (presentation.start >= lastEndTime)
{
maxCount++;
Console.WriteLine($"Presentation {maxCount} : presentation Start at {presentation.start} and End Time will be {presentation.End}");
lastEndTime = presentation.End;
}
}
return maxCount;
}
}
public class Parentheses
{
public int checkParenthesesOrder(string patternInput)
{
Stack<char> stack_symbols = new Stack<char>();
Dictionary<char, char> matchParentheses = new Dictionary<char, char>()
{
{ '}','{' },
{ ']','[' },
{ ')','(' },
{ '>','<' }
};
foreach(char c in patternInput)
{
if(matchParentheses.ContainsValue(c))
{
Console.WriteLine($"Push Open {c} in stack ");
stack_symbols.Push(c);
}
else if (matchParentheses.ContainsKey(c))
{
if(stack_symbols.Count>0 && stack_symbols.Peek() == matchParentheses[c])
{
stack_symbols.Pop();
Console.WriteLine($"pop Close {c} from stack");
}
else
{
Console.WriteLine($"stack count return mismatched on {c}");
return 0;
}
}
}
return stack_symbols.Count==0 ? 1 : 0;
}
}
}