-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
335 lines (286 loc) · 11.8 KB
/
Program.cs
File metadata and controls
335 lines (286 loc) · 11.8 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
335
/*
YOU ARE NOT ALLOWED TO MODIFY ANY FUNCTION DEFINATION's PROVIDED.
WRITE YOUR CODE IN THE RESPECTIVE FUNCTION BLOCK
*/
using System;
using System.Collections.Generic;
using System.Linq;
namespace DIS_Assignmnet1_SPRING_2022
{
class Program
{
static void Main(string[] args)
{
//Question 1
Console.WriteLine("Q1: Enter the string:");
string s = Console.ReadLine();
string final_string = RemoveVowels(s);
Console.WriteLine("Final string after removing the Vowels: {0}", final_string);
Console.WriteLine();
//Question 2:
string[] bulls_string1 = new string[] { "Marshall", "Student", "Center" };
string[] bulls_string2 = new string[] { "MarshallStudent", "Center" };
bool flag = ArrayStringsAreEqual(bulls_string1, bulls_string2);
Console.WriteLine("Q2");
if (flag)
{
Console.WriteLine("Yes, Both the array’s represent the same string");
}
else
{
Console.WriteLine("No, Both the array’s don’t represent the same string ");
}
Console.WriteLine();
//Question 3:
int[] bull_bucks = new int[] { 1, 2, 3, 2 };
int unique_sum = SumOfUnique(bull_bucks);
Console.WriteLine("Q3:");
Console.WriteLine("Sum of Unique Elements in the array are :{0}", unique_sum);
Console.WriteLine();
//Question 4:
int[,] bulls_grid = new int[,] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
Console.WriteLine("Q4:");
int diagSum = DiagonalSum(bulls_grid);
Console.WriteLine("The sum of diagonal elements in the bulls grid is :{0}", diagSum);
Console.WriteLine();
//Question 5:
Console.WriteLine("Q5:");
String bulls_string = "aiohn";
int[] indices = { 3, 1, 4, 2, 0 };
String rotated_string = RestoreString(bulls_string, indices);
Console.WriteLine("The Final string after rotation is {0} ", rotated_string);
Console.WriteLine();
//Quesiton 6:
string bulls_string6 = "mumacollegeofbusiness";
char ch = 'c';
string reversed_string = ReversePrefix(bulls_string6, ch);
Console.WriteLine("Q6:");
Console.WriteLine("Resultant string are reversing the prefix:{0}", reversed_string);
Console.WriteLine();
}
/*
<summary>
Given a string s, remove the vowels 'a', 'e', 'i', 'o', and 'u' from it, and return the new string.
Example 1:
Input: s = "MumaCollegeofBusiness"
Output: "MmCllgfBsnss"
Example 2:
Input: s = "aeiou"
Output: ""
Constraints:
• 0 <= s.length <= 10000
s consists of uppercase and lowercase letters
</summary>
*/
private static string RemoveVowels(string s)
{
try
{
//Declaring an array consists of vowels (considering only lower case, as given in the requirements)
string[] vowelsToRemove = new string[] { "a", "e", "i", "o", "u" };
//replacing the vowel from string with empty
foreach (var ch in vowelsToRemove)
{
s = s.Replace(ch, string.Empty);
}
//Assigning the updated string to final_string
String final_string = s;
return final_string;
}
catch (Exception)
{
throw;
}
}
/*
<summary>
Given two string arrays word1 and word2, return true if the two arrays represent the same string, and false otherwise.
A string is represented by an array if the array elements concatenated in order forms the string.
Example 1:
Input: : bulls_string1 = ["Marshall", "Student",”Center”], : bulls_string2 = ["MarshallStudent ", "Center"]
Output: true
Explanation:
word1 represents string "marshall" + "student" + “center” -> "MarshallStudentCenter "
word2 represents string "MarshallStudent" + "Center" -> "MarshallStudentCenter"
The strings are the same, so return true.
Example 2:
Input: word1 = ["Zimmerman", "School", ”of Advertising”, ”and Mass Communications”], word2 = ["Muma", “College”,"of”, “Business"]
Output: false
Example 3:
Input: word1 = ["University", "of", "SouthFlorida"], word2 = ["UniversityofSouthFlorida"]
Output: true
</summary>
*/
private static bool ArrayStringsAreEqual(string[] bulls_string1, string[] bulls_string2)
{
try
{
//Joined the array of same strings from each & compare against string1 & string2
return string.Join("", bulls_string1).Equals(string.Join("", bulls_string2));
}
catch (Exception)
{
throw;
}
}
/*
<summary>
You are given an integer array bull_bucks. The unique elements of an array are the elements that appear exactly once in the array.
Return the sum of all the unique elements of nums.
Example 1:
Input: bull_bucks = [1,2,3,2]
Output: 4
Explanation: The unique elements are [1,3], and the sum is 4.
Example 2:
Input: bull_bucks = [1,1,1,1,1]
Output: 0
Explanation: There are no unique elements, and the sum is 0.
Example 3:
Input: bull_bucks = [1,2,3,4,5]
Output: 15
Explanation: The unique elements are [1,2,3,4,5], and the sum is 15.
</summary>
*/
private static int SumOfUnique(int[] bull_bucks)
{
try
{
int sum = 0;
Dictionary<int, int> dict = new Dictionary<int, int>();
//Iterating through bull_bucks, to count each element's frequency
foreach (var num in bull_bucks)
{
if (!dict.ContainsKey(num))
dict.Add(num, 0);
dict[num] += 1;
}
//Considering only the elements, that have frequency = 1
foreach (var item in dict.Where(x => x.Value == 1).Select(x => x.Key).ToList())
sum += item;
return sum;
}
catch (Exception)
{
throw;
}
}
/*
<summary>
Given a square matrix bulls_grid, return the sum of the matrix diagonals.
Only include the sum of all the elements on the primary diagonal and all the elements on the secondary diagonal that are not part of the primary diagonal.
Example 1:
Input: bulls_grid = [[1,2,3],[4,5,6], [7,8,9]]
Output: 25
Explanation: Diagonals sum: 1 + 5 + 9 + 3 + 7 = 25
Notice that element mat[1][1] = 5 is counted only once.
Example 2:
Input: bulls_grid = [[1,1,1,1], [1,1,1,1],[1,1,1,1], [1,1,1,1]]
Output: 8
Example 3:
Input: bulls_grid = [[5]]
Output: 5
</summary>
*/
private static int DiagonalSum(int[,] bulls_grid)
{
try
{
//Initializations
int primarySum = 0, secondarySum = 0, n = bulls_grid.GetLength(0);
//calculating the sum of primary diagonal
for (int i = 0, j = 0; i < n; i++, j++)
{
primarySum += bulls_grid[i, j];
}
//calculating the sum of secondary diagonal
for (int i = 0, j = n - 1; i < n; i++, j--)
{
secondarySum += bulls_grid[i, j];
}
//In case of odd number of elements, subtracting the mid element, which would have been calculated with both primarySum & secondarySum
if (bulls_grid.Length % 2 == 1)
return primarySum + secondarySum - bulls_grid[n / 2, n / 2];
return primarySum + secondarySum;
// return sum;
}
catch (Exception e)
{
Console.WriteLine("An error occured: " + e.Message);
throw;
}
}
/*
<summary>
Given a string bulls_string and an integer array indices of the same length.
The string bulls_string will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.
Return the shuffled string.
Example 3:
Input: bulls_string = "aiohn", indices = [3,1,4,2,0]
Output: "nihao"
*/
private static string RestoreString(string bulls_string, int[] indices)
{
try
{
//declaring result array of the same size of given string
char[] res = new char[bulls_string.Length];
//building the result array as per the indices array's reference
for (int i = 0; i < bulls_string.Length; i++)
res[indices[i]] = bulls_string[i];
return new string(res);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
}
/*
<summary>
Given a 0-indexed string bulls_string and a character ch, reverse the segment of word that starts at index 0 and ends at the index of the first occurrence of ch (inclusive). If the character ch does not exist in word, do nothing.
For example, if word = "abcdefd" and ch = "d", then you should reverse the segment that starts at 0 and ends at 3 (inclusive). The resulting string will be "dcbaefd".
Return the resulting string.
Example 1:
Input: bulls_string = "mumacollegeofbusiness", ch = "c"
Output: "camumollegeofbusiness"
Explanation: The first occurrence of "c" is at index 4.
Reverse the part of word from 0 to 4 (inclusive), the resulting string is "camumollegeofbusiness".
Example 2:
Input: bulls_string = "xyxzxe", ch = "z"
Output: "zxyxxe"
Explanation: The first and only occurrence of "z" is at index 3.
Reverse the part of word from 0 to 3 (inclusive), the resulting string is "zxyxxe".
Example 3:
Input: bulls_string = "zimmermanschoolofadvertising", ch = "x"
Output: "zimmermanschoolofadvertising"
Explanation: "x" does not exist in word.
You should not do any reverse operation, the resulting string is "zimmermanschoolofadvertising".
*/
private static string ReversePrefix(string bulls_string6, char ch)
{
try
{
//String prefix_string ="";
//getting the first index of given character
int ind = bulls_string6.IndexOf(ch);
if (ind == -1)
return bulls_string6;
//declaring the result array & assigning the given string as charArray, to iterate
char[] res = bulls_string6.ToCharArray();
//iterating the result array & reversing the string till the first index of the given character
for (int i = 0; i <= ind / 2; i++)
{
//swapping the character from its index (i) to the new index (i-ind)
char tmp = res[i];
res[i] = res[Math.Abs(i - ind)];
res[Math.Abs(i - ind)] = tmp;
}
return new string(res);
}
catch (Exception)
{
throw;
}
}
}
}