Skip to content

Commit f7ad029

Browse files
Done.
1 parent 234b802 commit f7ad029

File tree

2 files changed

+120
-56
lines changed

2 files changed

+120
-56
lines changed

Algorithms/Easy_Algorithms.Tests/UnitTest1.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,14 +366,14 @@ public static bool compare(List<string> arr1, string[] arr2)
366366
public void NodeDepthTest1()
367367
{
368368
var root = new NodeDepthsClass.BinaryTree(1);
369-
root.left = new NodeDepthsClass.BinaryTree(2);
370-
root.left.left = new NodeDepthsClass.BinaryTree(4);
371-
root.left.left.left = new NodeDepthsClass.BinaryTree(8);
372-
root.left.left.right = new NodeDepthsClass.BinaryTree(9);
373-
root.left.right = new NodeDepthsClass.BinaryTree(5);
374-
root.right = new NodeDepthsClass.BinaryTree(3);
375-
root.right.left = new NodeDepthsClass.BinaryTree(6);
376-
root.right.right = new NodeDepthsClass.BinaryTree(7);
369+
root.Left = new NodeDepthsClass.BinaryTree(2);
370+
root.Left.Left = new NodeDepthsClass.BinaryTree(4);
371+
root.Left.Left.Left = new NodeDepthsClass.BinaryTree(8);
372+
root.Left.Left.Right = new NodeDepthsClass.BinaryTree(9);
373+
root.Left.Right = new NodeDepthsClass.BinaryTree(5);
374+
root.Right = new NodeDepthsClass.BinaryTree(3);
375+
root.Right.Left = new NodeDepthsClass.BinaryTree(6);
376+
root.Right.Right = new NodeDepthsClass.BinaryTree(7);
377377
int actual = NodeDepthsClass.NodeDepths(root);
378378
Assert.Equal(16, actual);
379379
}

Algorithms/Easy_Algorithms/EasyAlgorithmsClass.cs

Lines changed: 112 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ public class TournamentWinnerClass
124124
/// team won.
125125
///
126126
/// it's guaranteed that exactly one team will win the tournament and thateach team will compete
127-
/// agaist all other teamns exactly once. It's also guaranteed that the tournament will always have
128-
/// at leat two teamns.
127+
/// against all other teams exactly once. It's also guaranteed that the tournament will always have
128+
/// at least two teams.
129129
///
130130
/// </summary>
131131
/// <returns></returns>
132132
/// <exception cref="NotImplementedException"></exception>
133-
public int HOME_TEAM_WON = 1;
133+
internal int HomeTeamWon = 1;
134134

135135
public string TournamentWinner(List<List<string>> competitions, List<int> results)
136136
{
@@ -146,7 +146,7 @@ public string TournamentWinner(List<List<string>> competitions, List<int> result
146146
var homeTeam = competition[0];
147147
var awayTeam = competition[1];
148148

149-
string winingTeam = (result == HOME_TEAM_WON) ? homeTeam : awayTeam;
149+
string winingTeam = (result == HomeTeamWon) ? homeTeam : awayTeam;
150150

151151
updateScores(winingTeam, 3, scores);
152152

@@ -303,41 +303,41 @@ public class NodeDepthsClass
303303
// the Binary Tree and h is the height of the Binary Tree
304304
public class BinaryTree
305305
{
306-
private int value;
307-
public BinaryTree left;
308-
public BinaryTree right;
306+
private int _value;
307+
public BinaryTree? Left;
308+
public BinaryTree? Right;
309309
public BinaryTree(int value)
310310
{
311-
this.value = value;
312-
left = null;
313-
right = null;
311+
this._value = value;
312+
Left = null;
313+
Right = null;
314314
}
315315
}
316316

317-
public class Level
317+
private class Level
318318
{
319-
public BinaryTree root;
320-
public int depth;
321-
public Level(BinaryTree root, int depth)
319+
public readonly BinaryTree? Root;
320+
public readonly int Depth;
321+
public Level(BinaryTree? root, int depth)
322322
{
323-
this.root = root;
324-
this.depth = depth;
323+
this.Root = root;
324+
this.Depth = depth;
325325
}
326326
}
327-
public static int NodeDepths(BinaryTree root)
327+
public static int NodeDepths(BinaryTree? root)
328328
{
329329
int sumOfDepths = 0;
330-
Stack<Level> stack = new Stack<Level>();
330+
var stack = new Stack<Level>();
331331
stack.Push(new Level(root, 0));
332332
while (stack.Count > 0)
333333
{
334334
Level top = stack.Pop();
335-
BinaryTree node = top.root;
336-
int depth = top.depth;
335+
BinaryTree? node = top.Root;
336+
int depth = top.Depth;
337337
if (node == null) continue;
338338
sumOfDepths += depth;
339-
stack.Push(new Level(node.left, depth + 1));
340-
stack.Push(new Level(node.right, depth + 1));
339+
stack.Push(new Level(node.Left, depth + 1));
340+
stack.Push(new Level(node.Right, depth + 1));
341341
}
342342
return sumOfDepths;
343343
}
@@ -863,7 +863,15 @@ public static int ProductSumHelper(List<object> array, int multiplier)
863863

864864
#region BinarySeach
865865
/// <summary>
866-
///
866+
/// Write a function that takes in a sorted array of integers as well as a target integer.
867+
/// The function should use the Binary Seach algorithm to determine if the target integer
868+
/// is contained in the array and should return its index if it is, otherwise -1.
869+
///
870+
/// Sample input:
871+
/// array = [01,,21,33,45,45,61,71,72,73]
872+
///
873+
/// Sample Output:
874+
/// 3
867875
/// </summary>
868876
public class BinarySeachIterativelyClass
869877
{
@@ -906,7 +914,16 @@ public int BinarysearchRecursion(int[] array, int key, int l, int r)
906914

907915
#region FindThreeLargestNumbers
908916
/// <summary>
909-
///
917+
/// Write a function that takes in an array of at least three integers and, without sorting
918+
/// the input array, returns a sorted array of the three largest integers in the input array.
919+
///
920+
/// The function should return duplicated integers if necessary; for example, it should return
921+
/// [10,10.12] for an input array of [10,5,9,10,12].
922+
///
923+
/// Sample input:
924+
/// array - [141,1,17,-7,17,-27,18,518,7,7]
925+
/// Sample output:
926+
/// [181,41,541]
910927
/// </summary>
911928
public static class FindThreeLargestNumbersClass
912929
{
@@ -915,26 +932,28 @@ public static int[] FindThreeLargestNumbers(int[] array)
915932
int[] threeLargest = { Int32.MinValue, Int32.MinValue, Int32.MinValue };
916933
foreach (int num in array)
917934
{
918-
updateLargest(threeLargest, num);
935+
UpdateLargest(threeLargest, num);
919936
}
920937
return threeLargest;
921938
}
922-
public static void updateLargest(int[] threeLargest, int num)
939+
940+
private static void UpdateLargest(int[] threeLargest, int num)
923941
{
924942
if (num > threeLargest[2])
925943
{
926-
shiftAndUpdate(threeLargest, num, 2);
944+
ShiftAndUpdate(threeLargest, num, 2);
927945
}
928946
else if (num > threeLargest[1])
929947
{
930-
shiftAndUpdate(threeLargest, num, 1);
948+
ShiftAndUpdate(threeLargest, num, 1);
931949
}
932950
else if (num > threeLargest[0])
933951
{
934-
shiftAndUpdate(threeLargest, num, 0);
952+
ShiftAndUpdate(threeLargest, num, 0);
935953
}
936954
}
937-
public static void shiftAndUpdate(int[] array, int num, int idx)
955+
956+
private static void ShiftAndUpdate(int[] array, int num, int idx)
938957
{
939958
for (int i = 0; i <= idx; i++)
940959
{
@@ -953,7 +972,8 @@ public static void shiftAndUpdate(int[] array, int num, int idx)
953972

954973
#region BubbleSort
955974
/// <summary>
956-
///
975+
/// Write a function that takes in an array of integers and returns a sorted version of that
976+
/// array. Use the bubble sort to sort the array.
957977
/// </summary>
958978
public static class BubbleSortClass
959979
{
@@ -963,24 +983,25 @@ public static int[] BubbleSort(int[] array)
963983
{
964984
return new int[] { };
965985
}
966-
bool isSorted = false;
967-
int counter = 0;
986+
var isSorted = false;
987+
var counter = 0;
968988
while (!isSorted)
969989
{
970990
isSorted = true;
971991
for (int i = 0; i < array.Length - 1 - counter; i++)
972992
{
973993
if (array[i] > array[i + 1])
974994
{
975-
swap(i, i + 1, array);
995+
Swap(i, i + 1, array);
976996
isSorted = false;
977997
}
978998
}
979999
counter++;
9801000
}
9811001
return array;
9821002
}
983-
public static void swap(int i, int j, int[] array)
1003+
1004+
private static void Swap(int i, int j, int[] array)
9841005
{
9851006
int temp = array[j];
9861007
array[j] = array[i];
@@ -991,7 +1012,8 @@ public static void swap(int i, int j, int[] array)
9911012

9921013
#region InsertionSort
9931014
/// <summary>
994-
///
1015+
/// Write a function that takes in an array of integer and returns a sorted version of
1016+
/// that array. Use insertion sort.
9951017
/// </summary>
9961018
public class InsertionSortClass
9971019
{
@@ -1026,7 +1048,8 @@ public static void swap(int i, int j, int[] array)
10261048

10271049
#region SelectionSort
10281050
/// <summary>
1029-
///
1051+
/// Write a function that takes in an array of integer and returns a sorted version of
1052+
/// that array. Use selection sort algorithm.
10301053
/// </summary>
10311054
public class SelectionSortClass
10321055
{
@@ -1057,7 +1080,8 @@ public int[] SelectionSort(int[] array, int numberOfElements)
10571080

10581081
#region IsPalindrome
10591082
/// <summary>
1060-
///
1083+
/// Write d dunction that takes in an non-empty array string and that returns
1084+
/// a boolean representing whether the string is a palindrome.
10611085
/// </summary>
10621086
public class PalindromeCheckClass
10631087
{
@@ -1115,6 +1139,7 @@ public static char GetNewLetter(char letter, int key)
11151139
#endregion
11161140

11171141
#region RunLengthEncoding
1142+
11181143
/// <summary>
11191144
/// Write a function that takes in a non-empty
11201145
/// string and returns it's run-length encoding.
@@ -1131,12 +1156,11 @@ public static char GetNewLetter(char letter, int key)
11311156
/// And since encoded data must be decodable, this means that
11321157
/// we can't naively run-length-encode long runs. For example,
11331158
/// the run "AAAAAAAAAAAA" (12A)s, can naively be decoded as "12A"
1134-
/// sinc this string can be decoded as either "AAAAAAAAAAAA" or
1159+
/// since this string can be decoded as either "AAAAAAAAAAAA" or
11351160
/// "1AA". Thus, long runs(runs of 10 or more characters) should
11361161
/// be encoded in a split fashion; the aforementioned run should be
11371162
/// encoded as "9A3A".
11381163
/// </summary>
1139-
/// <param name="stringArray"></param>
11401164
/// <returns>String</returns>
11411165
public class RunLengthEncodingClass
11421166
{
@@ -1171,7 +1195,16 @@ public string RunLengthEncoding(string stringArray)
11711195

11721196
#region CommonCharacters
11731197
/// <summary>
1174-
///
1198+
/// Write a function that takes in a non-empty list of non-empty string and
1199+
/// returns a list of characters that are common to all strings in the list,
1200+
/// ignoring multiplicity.
1201+
///
1202+
/// Note that the strings are not guaranteed to only contain alphanumeric characters.
1203+
/// The list you return can be in any order.
1204+
/// Sample input:
1205+
/// strings = ["abc","bcd","cbaccd"]
1206+
/// Sample output:
1207+
/// ["b","c"]
11751208
/// </summary>
11761209
public class CommonCharactersClass
11771210
{
@@ -1219,7 +1252,16 @@ public string[] CommonCharacters(string[] strings)
12191252

12201253
#region GenerateDocument
12211254
/// <summary>
1222-
///
1255+
/// You're given a string of available characters and a string representing a document
1256+
/// that you need to generate. Write a function that determined if you can generate the
1257+
/// document using the available characters. If you can generate a document, your function
1258+
/// should return true:otherwise it should return false.
1259+
///
1260+
/// You're only able to generate the document if the frequency of unique characters in
1261+
/// string is greater than or equal to the frequency of unique characters in the document
1262+
/// string.
1263+
///
1264+
/// The document that your need to create may contain any characters, including special characters.
12231265
/// </summary>
12241266
public class GenerateDocumentClass
12251267
{
@@ -1267,25 +1309,33 @@ private int CountcharFrequency(char character, string target)
12671309

12681310
#region FirstNonRepeatingCharacter
12691311
/// <summary>
1270-
///
1312+
/// Write a function that takes in a string of lowercase English
1313+
/// alphabet letters and returns the index of the string's first
1314+
/// non-repeating characters.
1315+
///
1316+
/// The first non-repeating characters is the first character in
1317+
/// a string that occurs only once.
1318+
///
1319+
/// if the input string doesnt have any non-repeating characters your
1320+
/// function should return -1.
12711321
/// </summary>
12721322
public class FirstNonRepeatingCharacterClass
12731323
{
12741324
public int FirstNonRepeatingCharacter(string str)
12751325
{
1276-
Dictionary<Char, int> charaterFrequency = new Dictionary<Char, int>();
1326+
Dictionary<Char, int> characterFrequency = new Dictionary<Char, int>();
12771327

12781328
for (int i = 0; i < str.Length; i++)
12791329
{
12801330
char character = str[i];
1281-
charaterFrequency[character] =
1282-
charaterFrequency.GetValueOrDefault(character, 0) + 1;
1331+
characterFrequency[character] =
1332+
characterFrequency.GetValueOrDefault(character, 0) + 1;
12831333
}
12841334

12851335
for (int i = 0; i < str.Length; i++)
12861336
{
12871337
char character = str[i];
1288-
if (charaterFrequency[character] == 1)
1338+
if (characterFrequency[character] == 1)
12891339
{
12901340
return i;
12911341
}
@@ -1298,6 +1348,11 @@ public int FirstNonRepeatingCharacter(string str)
12981348

12991349
#region Semordnilap
13001350
/// <summary>
1351+
/// Write a function that takes in a list of unique strings and returns a list of Semordnilap pairs
1352+
///
1353+
/// A Semordnilap pair is defined as a set of different strings where the reverse of one word is the
1354+
/// same as the forward version of the other. For example the word "diaper and "repaid" are a
1355+
/// Semordnilap pair, as are the words "palindromes" and "Semordnilap"
13011356
///
13021357
/// </summary>
13031358
public class SemordnilapClass
@@ -1327,7 +1382,16 @@ public List<List<string> > Semordnilap(string[] input) {
13271382

13281383
#region ReverseWordsInString
13291384
/// <summary>
1330-
///
1385+
/// Write a function that takes in a string of words separated by one or more whitespaces
1386+
/// and returns a string that has these words in reverse order. For example, given the string
1387+
/// "tim is great", your function should return "great is tim.
1388+
///
1389+
/// For this problem, a word can contain special character. The words in the string will
1390+
/// be separated by one or more whitespaces, and the reversed string must contain the same
1391+
/// whitespaces , and the reversed string.
1392+
///
1393+
/// Node that you're not allowed to use any build-in split or reverse functions. but you
1394+
/// can use join function
13311395
/// </summary>
13321396
public class ReverseWordsInStringClass
13331397
{

0 commit comments

Comments
 (0)