-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram_DIS1.cs
More file actions
334 lines (269 loc) · 10.3 KB
/
Program_DIS1.cs
File metadata and controls
334 lines (269 loc) · 10.3 KB
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
using System;
using System.Collections.Generic;
using static System.Console;
namespace Assignment
{
class Program_DIS1
{
static void Main(string[] args)
{
Console.WriteLine(" Self Dividing Number - Output1:");
int a = 1, b = 22;
printSelfDividingNumbers(a, b);
Console.WriteLine("\n" + " PrintSeries - Output2 :");
int n2 = 5;
printSeries(n2);
Console.WriteLine("\n" + "Print Triangle - Output3 :");
int n3 = 5;
printTriangle(n3);
Console.WriteLine("\n" + " numJewelsInStones - Output4 :");
int[] J = new int[] { 1, 3 };
int[] S = new int[] { 1, 3, 3, 2, 2, 2, 2, 2 };
int r4 = numJewelsInStones(J, S);
Console.WriteLine(r4);
Console.WriteLine("\n" + " LargestCommonSubArray -Output5 :");
int[] arr1 = { 1, 2, 5, 6, 7, 8, 9 };
int[] arr2 = { 1, 2, 3, 4, 5, 7, 8, 9 };
int[] r5 = getLargestCommonSubArray(arr1, arr2);
foreach (int item in r5)
{
Console.Write(item + " ");
}
solvePuzzle();
}
// int[] arr = { 1, 2, 3 };
// object[] testArray = new object[] { "blah", "blah2" };
// List<int> testList = new List<int>();
/* foreach (var x in arr)
{
// Console.WriteLine("testlist: " +x);
testList.Add(x);
} */
// printSubArrays(arr, 0, 0);
// int[] X = { 1, 2, 3, 4, 5, 6 };
// int[] Y = { 1, 2, 5, 6, 7, 8 };
// int m = X.Length;
// int n = Y.Length;
// printLCSubStr(X, Y, m, n);
public static bool isSelfDividing(int num, int orig)
{
// we want to test each whether each digit is non - zero and divides the number. For example, with 21, we want to test d != 0 && 22 % d == 0 for d = 2,1.
// To do that, we need to iterate over each digit of the number.
// For each number in the given range, we will test if that number is self - dividing.
//perform the modulo operation
if ((num % 10) == 0 || orig % (num % 10) != 0)
return false;
if (num < 10)
return true;
return isSelfDividing(num / 10, orig);
}
//to print self dividing numbers for a given range from x to y
public static void printSelfDividingNumbers(int x, int y)
{
try
{
for (int i = x; i <= y; i++)
{
if (isSelfDividing(i, i))
{
Console.WriteLine(i);
}
}
}
//catch exception
catch
{
WriteLine("tion occured while computing printSelfDividingNumbers()");
}
}
public static void printSeries(int n)
{
try
{
int count = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= i; j++)
{
if (count <= n)
{
Console.Write(i + " ");
count = count + 1;
}
}
}
}
catch
{
Console.WriteLine("Exception occured while computing printSeries()");
}
}
public static void printTriangle(int n)
{
for (int i = n; i > 0; i--)
{
for (int k = 0; k < n - i; k++)
{
Console.Write(' ');
}
for (int j = i; j <= 2 * i - 1; j++)
{
Console.Write("* ");
}
for (int p = 0; p < i - 1; ++p)
Console.Write("* ");
Console.Write("\n");
}
}
public static int numJewelsInStones(int[] J, int[] S)
{
int c = 0;
for (int i = 0; i < S.Length; i++)
{
for (int j = 0; j < J.Length; j++)
{
if (J[j] == S[i])
{
c = c + 1;
}
}
}
return c;
}
public static int[] getLargestCommonSubArray(int[] X, int[] Y)
{
int res = 0;
int m = X.Length;
int n = Y.Length;
int[,] len = new int[2, n + m];
int Row = 0;
int checkEnd = 0;
//method finds the largest common contiguous subarray from two sorted arrays
//return the last array having the maximum length
// To store the index which contains the maximum value
try
{
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
{
len[Row, j] = 0;
}
else if (X[i - 1] == Y[j - 1])
{
len[Row, j] = len[1 - Row, j - 1] + 1;
if (len[Row, j] >= res)
{
res = len[Row, j];
checkEnd = i - 1;
}
}
else
{
len[Row, j] = 0;
}
}
Row = 1 - Row;
}
if (res == 0)
return null;
int[] @new = new int[res];
for (int i = checkEnd - res + 1, index = 0; index < res; i++, index++)
{
@new[index] = X[i];
}
return @new;
}
// Add catch statement if there is no largest common subarray found
catch
{
Console.WriteLine("Exception occured while computing getLargestCommonSubArray()");
}
return null;
}
/* static void printLCSubStr(int[] X, int[] Y, int m, int n)
{
// Create a table to store lengths of longest common
// suffixes of substrings. Note that LCSuff[i][j]
// contains length of longest common suffix of X[0..i-1]
// and Y[0..j-1]. The first row and first column entries
// have no logical meaning, they are used only for
// simplicity of program
int[,] LCSuff = new int[m + 1, n + 1];
// To store length of the longest common substring
int len = 0;
// To store the index of the cell which contains the
// maximum value. This cell's index helps in building
// up the longest common substring from right to left.
int row = 0, col = 0;
/* Following steps build LCSuff[m+1][n+1] in bottom
up fashion. */
/*for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= n; j++)
{
if (i == 0 || j == 0)
LCSuff[i, j] = 0;
else if (X[i - 1] == Y[j - 1])
{
LCSuff[i, j] = LCSuff[i - 1, j - 1] + 1;
if (len < LCSuff[i, j])
{
len = LCSuff[i, j];
row = i;
col = j;
}
}
else
LCSuff[i, j] = 0;
}
}
// if true, then no common substring exists
if (len == 0)
{
Console.Write("No Common Substring");
return;
}
// allocate space for the longest common substring
String resultStr = "";
// traverse up diagonally form the (row, col) cell
// until LCSuff[row][col] != 0
while (LCSuff[row, col] != 0)
{
resultStr = X[row - 1] + "," + resultStr ; // or Y[col-1]
--len;
// move diagonally up to previous cell
row--;
col--;
}
// required longest common substring
resultStr = resultStr.Remove(resultStr.Length - 1, 1);
Console.WriteLine(resultStr);
} */
public static void solvePuzzle()
{
try
{
// Console.WriteLine("Input1 :");
var input1 = Console.ReadLine();
// Console.WriteLine("Input2 :");
var input2 = Console.ReadLine();
}
catch
{
Console.WriteLine("Exception occured while computing solvePuzzle()");
}
}
}
}
/* write your self-reflection here as a comment :
1- Got handson experience on Visual Studio IDE.
2- Got exposure to basics of c# and coding in general.
3- Learned programming concepts and their fundamental application.
4- Learned how to present and prepare a proper flow of the program.
5- Learned how to develop and use methods throughout the program having independent functionality in c# programming.
6- Broke the program into simpler segments for efficient calculation.
7- Learned how to convert data types i.e. array to int.
8- Learned how to handle corner cases. */