-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
161 lines (152 loc) · 5.15 KB
/
Program.cs
File metadata and controls
161 lines (152 loc) · 5.15 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
//Question 1: Write an funciton that finds the largest and smallest numbers in an array
using System.Text;
using System.Text.RegularExpressions;
static (int min, int max) FindMinMax(int[] numbers)
{
if (numbers == null || numbers.Length == 0)
throw new ArgumentException("Array cannot be null or empty.");
int min = numbers[0];
int max = numbers[0];
for (int i = 1; i < numbers.Length; i++)
{
if (numbers[i] < min)
min = numbers[i];
if (numbers[i] > max)
max = numbers[i];
}
return (min, max);
}
//Question 2: Write a function that removes duplicate characters from string. Provide at least 3 solutions.
//Which is best in your opinion? Why?
//2A
static string RemoveDuplicates_HashSet(string input)
{
if (string.IsNullOrEmpty(input)) return input;
var seen = new HashSet<char>();
var result = new StringBuilder();
foreach (char c in input)
{
// returns false if exists
if (seen.Add(c))
{
result.Append(c);
}
}
return result.ToString();
}
//2B
static string RemoveDuplicates_Linq(string input)
{
if (string.IsNullOrEmpty(input)) return input;
return new string(input.Distinct().ToArray());
}
//2C
static string RemoveDuplicates_NoExtraSpace(string input)
{
if (string.IsNullOrEmpty(input)) return input;
var result = new StringBuilder();
for (int i = 0; i < input.Length; i++)
{
bool isDuplicate = false;
for (int j = 0; j < i; j++)
{
if (input[i] == input[j])
{
isDuplicate = true;
break;
}
}
if (!isDuplicate)
{
result.Append(input[i]);
}
}
return result.ToString();
}
//2a is best becuase O(n) time, Keeps original order, Clean and efficient
//Question 3: Write a function that checks if 2 strings are anagrams.
static bool isAnagram(string s1, string s2)
{
//error handling
if (s1 == null || s2 == null) return false;
// If lengths are different, they cannot be anagrams
if (s1.Length != s2.Length) return false;
// Dictionary to store character frequency counts
var counts = new Dictionary<char, int>();
// Count each character in the first string
foreach (char c in s1.ToLower())
{
// If character not yet seen, initialize count
if (!counts.ContainsKey(c))
counts[c] = 0;
// Increment count for this character
counts[c]++;
}
// Subtract counts using the second string
foreach (char c in s2.ToLower())
{
// If character not found, strings are not anagrams
if (!counts.ContainsKey(c))
return false;
// Decrease count for this characte
counts[c]--;
// If count goes negative, s2 has extra occurrences
if (counts[c] < 0)
return false;
}
return true;
}
//Question 4: Write a RegEx to match an Australian mobile phone
static bool IsValidAustralianMobile(string phoneNumber)
{
if (string.IsNullOrWhiteSpace(phoneNumber))
return false;
// Matches:
// 04XXXXXXXX
// +614XXXXXXXX
// with optional spaces or hyphens
// ^ Start of string
// (?:\+61|0) Match either "+61" (international format) OR "0" (local format)
// 4 Mobile numbers must start with '4' after prefix
// (?:[\s-]?\d){8} Match 8 digits, each optionally preceded by a space or hyphen
// $ End of string
string pattern = @"^(?:\+61|0)4(?:[\s-]?\d){8}$";
return Regex.IsMatch(phoneNumber, pattern);
}
Console.WriteLine("Hello, World!");
//Question 1: Example
var result = FindMinMax(new[] { 123, 534, 3, 9, 275 });
Console.WriteLine($"Min: {result.min}, Max: {result.max}");
//Question 2: Example
//2A
Console.WriteLine($"Input String for removing duplicate characters is 'Hello'");
var inputString = "hello";
var string1 = RemoveDuplicates_HashSet(inputString);
Console.WriteLine($"RemoveDuplicates_HashSet");
Console.WriteLine($"{string1}");
//2B
var string2 = RemoveDuplicates_Linq(inputString);
Console.WriteLine($"RemoveDuplicates_Linq");
Console.WriteLine($"{string2}");
//2C
var string3 = RemoveDuplicates_NoExtraSpace(inputString);
Console.WriteLine($"RemoveDuplicates_NoExtraSpace");
Console.WriteLine($"{string3}");
//Question 3: Example
//true
var anagramString = isAnagram("note","tone");
Console.WriteLine($"Check Anagram for Strings 'note' and 'tone'");
Console.WriteLine($"{anagramString}");
//false
var nonAnagramString = isAnagram("hello","world");
Console.WriteLine($"Check Anagram for Strings 'hello' and 'world'");
Console.WriteLine($"{nonAnagramString}");
//Question 4: Example
Console.WriteLine($"Check if Australian phone: +61412 345 678");
Console.WriteLine(IsValidAustralianMobile("+61412 345 678"));
Console.WriteLine($"Check if Australian phone: 0412 345 678");
Console.WriteLine(IsValidAustralianMobile("0412 345 678"));
Console.WriteLine($"Check if Australian phone: +61312 345 678");
Console.WriteLine(IsValidAustralianMobile("+61312 345 678"));
Console.WriteLine($"Check if Australian phone: 0312 345 678");
Console.WriteLine(IsValidAustralianMobile("0312 345 678"));