Skip to content

Commit cb9d212

Browse files
.
1 parent 4fdec57 commit cb9d212

File tree

2 files changed

+184
-2
lines changed

2 files changed

+184
-2
lines changed

Algorithms/Medium_Algorithms.Tests/UnitTest1.cs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,13 +681,70 @@ public void MinNumberOfCoinsForChangeTestCase1()
681681
{
682682
int[] input = { 1, 5, 10 };
683683
Assert.True(MinNumberOfCoinsForChangeClass.MinNumberOfCoinsForChange(7, input) == 3);
684-
}
684+
}
685685
#endregion
686686

687+
#region LevenshteinDistance
688+
[Fact]
689+
public void LevenshteinDistanceTestCase1()
690+
{
691+
Assert.True(LevenshteinDistanceClass.LevenshteinDistance("abc", "yabd") == 2);
692+
}
693+
#endregion
687694

695+
#region NumberOfWaysToTraverseGraph
696+
[Fact]
697+
public void NumberOfWaysToTraverseGraphTestCase()
698+
{
699+
int width = 4;
700+
int height = 3;
701+
int expected = 10;
702+
var actual = new NumberOfWaysToTraverseGraphClass().NumberOfWaysToTraverseGraph(width, height);
703+
Assert.True(expected == actual);
704+
}
705+
#endregion
688706

707+
#region SingleCycleCheckClass
708+
[Fact]
709+
public void HasSingleCycleTestCase1()
710+
{
711+
Assert.True(SingleCycleCheckClass.HasSingleCycle(new int[] { 2, 3, 1, -4, -4, 2 }));
712+
}
713+
#endregion
689714

715+
#region BreadthFirstSearch
716+
[Fact]
717+
public void BreadthFirstSearchTestCase1()
718+
{
719+
BreadthFirstSearchClass.Node graph = new BreadthFirstSearchClass.Node("A");
720+
graph.AddChild("B").AddChild("C").AddChild("D");
721+
graph.children[0].AddChild("E").AddChild("F");
722+
graph.children[2].AddChild("G").AddChild("H");
723+
graph.children[0].children[1].AddChild("I").AddChild("J");
724+
graph.children[2].children[0].AddChild("K");
725+
string[] expected = {
726+
"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K"
727+
};
728+
List<string> inputArray = new List<string>();
729+
Assert.True(compare(graph.BreadthFirstSearch(inputArray), expected));
730+
}
690731

732+
public static bool compare(List<string> arr1, string[] arr2)
733+
{
734+
if (arr1.Count != arr2.Length)
735+
{
736+
return false;
737+
}
738+
for (int i = 0; i < arr1.Count; i++)
739+
{
740+
if (!arr1[i].Equals(arr2[i]))
741+
{
742+
return false;
743+
}
744+
}
745+
return true;
746+
}
747+
#endregion
691748

692749

693750

Algorithms/Medium_Algorithms/MediumAlgorithmsClass.cs

Lines changed: 126 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,14 +1394,139 @@ public static int MinNumberOfCoinsForChange(int n, int[] denoms)
13941394
}
13951395
return numOfCoins[n] != Int32.MaxValue ? numOfCoins[n] : -1;
13961396
}
1397-
}
1397+
}
13981398
#endregion
13991399

1400+
#region LevenshteinDistance
1401+
/// <summary>
1402+
///
1403+
/// </summary>
1404+
public class LevenshteinDistanceClass
1405+
{
1406+
public static int LevenshteinDistance(string str1, string str2)
1407+
{
1408+
int[,] edits = new int[str2.Length + 1, str1.Length + 1];
1409+
for (int i = 0; i < str2.Length + 1; i++)
1410+
{
1411+
for (int j = 0; j < str1.Length + 1; j++)
1412+
{
1413+
edits[i, j] = j;
1414+
}
1415+
edits[i, 0] = i;
1416+
}
1417+
for (int i = 1; i < str2.Length + 1; i++)
1418+
{
1419+
for (int j = 1; j < str1.Length + 1; j++)
1420+
{
1421+
if (str2[i - 1] == str1[j - 1])
1422+
{
1423+
edits[i, j] = edits[i - 1, j - 1];
1424+
}
1425+
else
1426+
{
1427+
edits[i,
1428+
j] = 1 +
1429+
Math.Min(edits[i - 1, j - 1],
1430+
Math.Min(edits[i - 1, j],
1431+
edits[i, j - 1]));
1432+
}
1433+
}
1434+
}
1435+
return edits[str2.Length, str1.Length];
1436+
}
1437+
}
1438+
#endregion
14001439

1440+
#region NumberOfWaysToTraverseGraph
1441+
/// <summary>
1442+
///
1443+
/// </summary>
1444+
public class NumberOfWaysToTraverseGraphClass
1445+
{
1446+
public int NumberOfWaysToTraverseGraph(int width, int height)
1447+
{
1448+
if (width ==1 || height ==1)
1449+
{
1450+
return 1;
1451+
}
14011452

1453+
return NumberOfWaysToTraverseGraph(width-1, height) +
1454+
NumberOfWaysToTraverseGraph(width, height - 1);
1455+
}
1456+
}
1457+
#endregion
14021458

1459+
#region SingleCycleCheck
1460+
/// <summary>
1461+
///
1462+
/// </summary>
1463+
public class SingleCycleCheckClass
1464+
{
1465+
public static bool HasSingleCycle(int[] array)
1466+
{
1467+
int numElementsVisited = 0;
1468+
int currentIdx = 0;
1469+
while (numElementsVisited < array.Length)
1470+
{
1471+
if (numElementsVisited > 0 && currentIdx == 0) return false;
1472+
numElementsVisited++;
1473+
currentIdx = getNextIdx(currentIdx, array);
1474+
}
1475+
return currentIdx == 0;
1476+
}
1477+
1478+
public static int getNextIdx(int currentIdx, int[] array)
1479+
{
1480+
int jump = array[currentIdx];
1481+
int nextIdx = (currentIdx + jump) % array.Length;
1482+
return nextIdx >= 0 ? nextIdx : nextIdx + array.Length;
1483+
}
1484+
}
1485+
#endregion
1486+
1487+
#region BreadthFirstSearch
1488+
/// <summary>
1489+
///
1490+
/// </summary>
1491+
public class BreadthFirstSearchClass
1492+
{
1493+
// Do not edit the class below except
1494+
// for the BreadthFirstSearch method.
1495+
// Feel free to add new properties
1496+
// and methods to the class.
1497+
public class Node
1498+
{
1499+
public string name;
1500+
public List<Node> children = new List<Node>();
1501+
1502+
public Node(string name)
1503+
{
1504+
this.name = name;
1505+
}
1506+
1507+
public List<string> BreadthFirstSearch(List<string> array)
1508+
{
1509+
Queue<Node> queue = new Queue<Node>();
1510+
queue.Enqueue(this);
14031511

1512+
while (queue.Count >0)
1513+
{
1514+
Node current = queue.Dequeue();
1515+
array.Add(current.name);
1516+
current.children.ForEach(child => { queue.Enqueue(child); });
1517+
}
1518+
return array;
1519+
}
14041520

1521+
public Node AddChild(string name)
1522+
{
1523+
Node child = new Node(name);
1524+
children.Add(child);
1525+
return this;
1526+
}
1527+
}
1528+
}
1529+
#endregion
14051530

14061531

14071532

0 commit comments

Comments
 (0)