Skip to content

Commit

Permalink
graph and stack implementation updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
paramag authored and paramag committed Aug 29, 2018
1 parent bfcfa10 commit 6cd4865
Show file tree
Hide file tree
Showing 15 changed files with 694 additions and 43 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@
/Algorithm/LeetCode/obj/Debug
/Algorithm/Stacks/bin/Debug
/Algorithm/Stacks/obj/Debug
/Algorithm/InterviewCake/bin/Debug
/Algorithm/InterviewCake/obj/Debug
Binary file modified Algorithm/.vs/Algorithms/v14/.suo
Binary file not shown.
19 changes: 19 additions & 0 deletions Algorithm/Algorithms.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Algorithms.Problems.LeetCod
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Algorithms.Problem.InterviewCake", "InterviewCake\Algorithms.Problem.InterviewCake.csproj", "{9618162B-F6BE-4E79-A256-8B2D34AE7F46}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataStructures.Libraries.Graph", "..\DataStructures\Graph\DataStructures.Libraries.Graph.csproj", "{6EB3565B-2658-42A7-80CB-223B6E42013A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -179,11 +181,28 @@ Global
{9618162B-F6BE-4E79-A256-8B2D34AE7F46}.Release|x64.Build.0 = Release|Any CPU
{9618162B-F6BE-4E79-A256-8B2D34AE7F46}.Release|x86.ActiveCfg = Release|Any CPU
{9618162B-F6BE-4E79-A256-8B2D34AE7F46}.Release|x86.Build.0 = Release|Any CPU
{6EB3565B-2658-42A7-80CB-223B6E42013A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6EB3565B-2658-42A7-80CB-223B6E42013A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6EB3565B-2658-42A7-80CB-223B6E42013A}.Debug|ARM.ActiveCfg = Debug|Any CPU
{6EB3565B-2658-42A7-80CB-223B6E42013A}.Debug|ARM.Build.0 = Debug|Any CPU
{6EB3565B-2658-42A7-80CB-223B6E42013A}.Debug|x64.ActiveCfg = Debug|Any CPU
{6EB3565B-2658-42A7-80CB-223B6E42013A}.Debug|x64.Build.0 = Debug|Any CPU
{6EB3565B-2658-42A7-80CB-223B6E42013A}.Debug|x86.ActiveCfg = Debug|Any CPU
{6EB3565B-2658-42A7-80CB-223B6E42013A}.Debug|x86.Build.0 = Debug|Any CPU
{6EB3565B-2658-42A7-80CB-223B6E42013A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6EB3565B-2658-42A7-80CB-223B6E42013A}.Release|Any CPU.Build.0 = Release|Any CPU
{6EB3565B-2658-42A7-80CB-223B6E42013A}.Release|ARM.ActiveCfg = Release|Any CPU
{6EB3565B-2658-42A7-80CB-223B6E42013A}.Release|ARM.Build.0 = Release|Any CPU
{6EB3565B-2658-42A7-80CB-223B6E42013A}.Release|x64.ActiveCfg = Release|Any CPU
{6EB3565B-2658-42A7-80CB-223B6E42013A}.Release|x64.Build.0 = Release|Any CPU
{6EB3565B-2658-42A7-80CB-223B6E42013A}.Release|x86.ActiveCfg = Release|Any CPU
{6EB3565B-2658-42A7-80CB-223B6E42013A}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{845E0CA8-AC51-4BAE-95D7-60680CB4A44B} = {B2A3848C-3D95-4B7D-8247-227C7F9A2308}
{6EB3565B-2658-42A7-80CB-223B6E42013A} = {B2A3848C-3D95-4B7D-8247-227C7F9A2308}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,12 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AppStockProblem.cs" />
<Compile Include="DirtyTestCode.cs" />
<Compile Include="PlayGround.cs" />
<Compile Include="Meeting.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RepeatNumbers.cs" />
<Compile Include="RepeatNumbersBeastMode.cs" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Expand Down
21 changes: 21 additions & 0 deletions Algorithm/InterviewCake/DirtyTestCode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithms.Problem.InterviewCake
{
[TestClass]
public class DirtyTestCode
{
[TestMethod]
public void TestMethod1()
{
string str = "cats";

string subStr = str.Substring(0, str.Length - 1);
}
}
}
8 changes: 5 additions & 3 deletions Algorithm/InterviewCake/RepeatNumbers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ public static int FindRepeat(int[] items)
public void LongArrayTest()
{
// var numbers = new int[] { 4, 1, 4, 8, 3, 2, 7, 6, 5 };
var numbers = new int[] { 5, 8, 4, 1, 3, 2, 7, 6, 8 };
var expected = 8;
// var numbers = new int[] { 5, 8, 4, 1, 3, 2, 7, 6, 8 };

var numbers = new int[] { 1, 3, 9, 8, 4, 6, 1, 2, 5, 7 };
var expected = 1;
var actual = FindRepeat(numbers);
Assert.AreEqual(expected, actual);
}
}
}
}
12 changes: 12 additions & 0 deletions Algorithm/InterviewCake/RepeatNumbersBeastMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithms.Problem.InterviewCake
{
public class RepeatNumbersBeastMode
{
}
}
1 change: 1 addition & 0 deletions Algorithm/Stacks/Algorithms.Problem.Stacks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="GetMiddleElementConstantTimeProblem.cs" />
<Compile Include="MinValueInConstantTime.cs" />
<Compile Include="NextGreaterElementInArray.cs" />
<Compile Include="ParanthesisProblem.cs" />
Expand Down
136 changes: 136 additions & 0 deletions Algorithm/Stacks/GetMiddleElementConstantTimeProblem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithms.Problem.Stacks
{
public class Node
{
public int value;
public Node previous;
public Node next;

public Node(int value)
{
this.value = value;
}
}

[TestClass]
public class GetMiddleElementConstantTimeProblem
{
private Node doubleLinkedList;
private Node middleElement;
public int Counter = 0;

public void Push(int value)
{
Node node = new Node(value);

if (doubleLinkedList == null)
{
doubleLinkedList = node;
middleElement = doubleLinkedList;
}
else
{
doubleLinkedList.next = node;
doubleLinkedList.next.previous = doubleLinkedList;

doubleLinkedList = doubleLinkedList.next;
}

this.Counter += 1;

if (this.Counter > 1 && !(this.Counter % 2 == 0))
{
middleElement = middleElement.next;
}
}

public int Pop()
{
int value = doubleLinkedList.value;
doubleLinkedList = doubleLinkedList.previous;
doubleLinkedList.next = null;

this.Counter -= 1;

if (!(this.Counter % 2 == 0))
{
middleElement = middleElement.previous;
}

return value;
}

public int GetMiddleElement()
{
return middleElement.value;
}

[TestMethod]
public void TestMiddleElementConstantTime()
{
this.Push(1);
this.Push(2);
this.Push(3);

Assert.AreEqual(2, GetMiddleElement());
}

[TestMethod]
public void TestMiddleElementConstantTime2()
{
this.Push(1);
this.Push(2);
this.Push(3);
this.Push(4);

Assert.AreEqual(2, GetMiddleElement());
}

[TestMethod]
public void TestMiddleElementConstantTime3()
{
this.Push(1);
this.Push(2);
this.Push(3);
this.Push(4);
this.Push(5);

Assert.AreEqual(3, GetMiddleElement());
}

[TestMethod]
public void TestMiddleElementConstantTime4()
{
this.Push(1);
this.Push(2);
this.Push(3);
this.Push(4);
this.Push(5);
this.Pop();

Assert.AreEqual(3, GetMiddleElement());
}

[TestMethod]
public void TestMiddleElementConstantTime5()
{
this.Push(1);
this.Push(2);
this.Push(3);
this.Push(4);
this.Push(5);

this.Pop();
this.Pop();

Assert.AreEqual(2, GetMiddleElement());
}
}
}
60 changes: 59 additions & 1 deletion Algorithm/Stacks/MinValueInConstantTime.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,71 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Stacks
{
/// <summary>
/// Get minimum value from stack in constant time.
/// </summary>
[TestClass]
public class MinValueInConstantTime
{
private Stack<int> stack = new Stack<int>();
private Stack<int> auxillaryStack = new Stack<int>();

public int GetMinimumValue()
{
return this.auxillaryStack.Peek();
}

public void Push(int value)
{
this.stack.Push(value);

// Use the aux stack (2nd) to push the value if the top of the aux stack is less than the value.
// Also push to aux stack if the stack is empty.
if (this.auxillaryStack.Count == 0 || this.auxillaryStack.Peek() > value)
{
this.auxillaryStack.Push(value);
}
else
{
int auxValue = this.auxillaryStack.Peek();
this.auxillaryStack.Push(auxValue);
}
}

public int Pop()
{
int value = this.stack.Pop();

// remove from the aux stack if the value is equal to the top of the stack.
if (this.auxillaryStack.Peek() == value)
{
this.auxillaryStack.Pop();
}

return value;
}

[TestMethod]
public void TestGetMinimumInConstantTime()
{
this.Push(10);
this.Push(8);
this.Push(11);
this.Push(5);
this.Push(2);
this.Push(20);
this.Push(17);
this.Push(1);

int value = this.GetMinimumValue();

Assert.AreEqual(value, 1);
}
}
}
13 changes: 12 additions & 1 deletion DataStructures/Graph/DataStructures.Libraries.Graph.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\..\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.UnitTestFramework.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -41,9 +45,16 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Graph.cs" />
<Compile Include="Vertex.cs" />
<Compile Include="Graph`1.cs" />
<Compile Include="Vertex`1.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="graph_example.jpg" />
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Loading

0 comments on commit 6cd4865

Please sign in to comment.