Skip to content

Commit

Permalink
Add W3 D3 programs
Browse files Browse the repository at this point in the history
  • Loading branch information
cvpfus committed Sep 20, 2023
1 parent 73f7240 commit 6ec2c42
Show file tree
Hide file tree
Showing 11 changed files with 246 additions and 2 deletions.
27 changes: 25 additions & 2 deletions FormulatrixBootcamp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "List", "W3 D2\List\List.csp
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OperatorOverloading", "W2 D3\OperatorOverloading\OperatorOverloading.csproj", "{3108ABBF-67CB-4E65-80A7-6C1F2F3E30B7}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "W3 D3", "W3 D3", "{91FC4BEC-1535-4218-88E7-774E73FCFB89}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dictionary", "W3 D3\Dictionary\Dictionary.csproj", "{78E07498-23C7-4F38-895B-48EA5BCC8104}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HashTable", "W3 D3\HashTable\HashTable.csproj", "{2AD188A5-50C3-407C-BAE4-8EED634E2BE5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "QueueStack", "W3 D3\QueueStack\QueueStack.csproj", "{B48AD888-F0FA-4879-8F05-BB765527F212}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -319,13 +327,26 @@ Global
{3108ABBF-67CB-4E65-80A7-6C1F2F3E30B7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3108ABBF-67CB-4E65-80A7-6C1F2F3E30B7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3108ABBF-67CB-4E65-80A7-6C1F2F3E30B7}.Release|Any CPU.Build.0 = Release|Any CPU
{78E07498-23C7-4F38-895B-48EA5BCC8104}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{78E07498-23C7-4F38-895B-48EA5BCC8104}.Debug|Any CPU.Build.0 = Debug|Any CPU
{78E07498-23C7-4F38-895B-48EA5BCC8104}.Release|Any CPU.ActiveCfg = Release|Any CPU
{78E07498-23C7-4F38-895B-48EA5BCC8104}.Release|Any CPU.Build.0 = Release|Any CPU
{2AD188A5-50C3-407C-BAE4-8EED634E2BE5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2AD188A5-50C3-407C-BAE4-8EED634E2BE5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2AD188A5-50C3-407C-BAE4-8EED634E2BE5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2AD188A5-50C3-407C-BAE4-8EED634E2BE5}.Release|Any CPU.Build.0 = Release|Any CPU
{B48AD888-F0FA-4879-8F05-BB765527F212}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B48AD888-F0FA-4879-8F05-BB765527F212}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B48AD888-F0FA-4879-8F05-BB765527F212}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B48AD888-F0FA-4879-8F05-BB765527F212}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{3108ABBF-67CB-4E65-80A7-6C1F2F3E30B7} = {82AE1F6C-47FF-4067-89F3-87EBEFDA524C}
{A718291C-B218-4283-837F-43CF360568AC} = {A718291C-B218-4283-837F-43CF360568AC}
{B48AD888-F0FA-4879-8F05-BB765527F212} = {91FC4BEC-1535-4218-88E7-774E73FCFB89}
{2AD188A5-50C3-407C-BAE4-8EED634E2BE5} = {91FC4BEC-1535-4218-88E7-774E73FCFB89}
{78E07498-23C7-4F38-895B-48EA5BCC8104} = {91FC4BEC-1535-4218-88E7-774E73FCFB89}
{77D0C1C7-A06B-4E05-9458-873709BD4BFD} = {44C682DF-E1C9-4394-813D-0CB989CD9262}
{AA5CB577-831E-40F5-AABD-AFF7C56F8A57} = {44C682DF-E1C9-4394-813D-0CB989CD9262}
{1A0A5FD8-8FC2-4ECA-8EB0-BAC6DF70E7BF} = {0458C07F-2B31-4DB7-97E4-315800A0B2C0}
Expand All @@ -337,6 +358,7 @@ Global
{5275FE71-3F2C-449D-8A07-CE4D9D6FECAD} = {7892F519-CF95-407D-823E-C912E3E8BFAA}
{45977EB2-1F92-4A88-85CE-EED6715911C4} = {7892F519-CF95-407D-823E-C912E3E8BFAA}
{BD82992B-A5BB-46B3-8F43-86151B8D1101} = {7892F519-CF95-407D-823E-C912E3E8BFAA}
{A718291C-B218-4283-837F-43CF360568AC} = {A718291C-B218-4283-837F-43CF360568AC}
{AAF2C0DE-3EB1-4E94-B483-E34878CC2720} = {A718291C-B218-4283-837F-43CF360568AC}
{EB36E094-5E54-4F05-8EF2-2EDD48DE2628} = {7892F519-CF95-407D-823E-C912E3E8BFAA}
{466DCC52-00BD-45AD-A9AD-DAF1B2A8638E} = {A718291C-B218-4283-837F-43CF360568AC}
Expand Down Expand Up @@ -373,5 +395,6 @@ Global
{AC39434B-0FCF-4A5F-A86C-8892670D1AC8} = {24A9828C-37FB-41FF-AD85-C74FD61DFF50}
{2E8B2B36-98E9-4ACC-BE46-0332F53A1AE3} = {24A9828C-37FB-41FF-AD85-C74FD61DFF50}
{5F05DFBC-6EF9-4F4F-8B6D-4E1490171568} = {24A9828C-37FB-41FF-AD85-C74FD61DFF50}
{3108ABBF-67CB-4E65-80A7-6C1F2F3E30B7} = {82AE1F6C-47FF-4067-89F3-87EBEFDA524C}
EndGlobalSection
EndGlobal
10 changes: 10 additions & 0 deletions W3 D3/Dictionary/Dictionary.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
26 changes: 26 additions & 0 deletions W3 D3/Dictionary/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
public class Program
{
static void Main()
{
Dictionary<int, string> dict = new Dictionary<int, string>(){{3, "foo"}, {5, "bar"}};
dict.Add(7, "fizz");
dict.Add(9, "buzz");

PrintDictionary(dict);

dict.Remove(3);
PrintDictionary(dict);

bool status = dict.TryGetValue(3, out string? result);
Console.WriteLine($"{status} - {result}");
}

static void PrintDictionary(Dictionary<int, string> dict)
{
Console.WriteLine();
foreach (var kvp in dict)
{
Console.WriteLine($"{kvp.Key} - {kvp.Value}");
}
}
}
10 changes: 10 additions & 0 deletions W3 D3/HashTable/HashTable.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
26 changes: 26 additions & 0 deletions W3 D3/HashTable/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Collections;

public class Program
{
static void Main()
{
// Not type safety collection, and not recommended.
Hashtable ht = new Hashtable(){{3, "foo"}, {5, "bar"}};
ht.Add("foo", 3);
ht.Add("bar", 5);

PrintHashTable(ht);

ht.Remove(3);
PrintHashTable(ht);
}

static void PrintHashTable(Hashtable ht)
{
Console.WriteLine();
foreach (DictionaryEntry de in ht) // Cannot use var
{
Console.WriteLine($"{de.Key} - {de.Value}");
}
}
}
26 changes: 26 additions & 0 deletions W3 D3/QueueStack/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using QueueStack;
public class Program
{
static void Main()
{
Wardrobe wardrobe = new(2, 5);

wardrobe.TryPutCloth(0, ShirtEnum.Levis);
wardrobe.TryPutCloth(0, ShirtEnum.Levis);
wardrobe.TryPutCloth(0, ShirtEnum.Uniqlo);
wardrobe.TryPutCloth(0, ShirtEnum.Erigo);
wardrobe.TryPutCloth(0, ShirtEnum.Erigo);
wardrobe.TryPutCloth(0, ShirtEnum.Uniqlo); // false

wardrobe.TryPutCloth(1, TrousersEnum.Levis);
wardrobe.TryPutCloth(1, TrousersEnum.Wrangler);
wardrobe.TryPutCloth(1, TrousersEnum.Jeans);
wardrobe.TryPutCloth(1, TrousersEnum.Wrangler);
wardrobe.TryPutCloth(1, TrousersEnum.Jeans);
wardrobe.TryPutCloth(1, TrousersEnum.Jeans); // false

Console.WriteLine(wardrobe);

// todo: add queue feature
}
}
8 changes: 8 additions & 0 deletions W3 D3/QueueStack/QueueStack.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
8 changes: 8 additions & 0 deletions W3 D3/QueueStack/ShirtEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace QueueStack;

public enum ShirtEnum
{
Erigo = 1,
Levis,
Uniqlo
}
9 changes: 9 additions & 0 deletions W3 D3/QueueStack/StackUnderflowException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace QueueStack;

public class StackUnderflowException : Exception
{
public StackUnderflowException(string message) : base(message)
{

}
}
8 changes: 8 additions & 0 deletions W3 D3/QueueStack/TrousersEnum.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace QueueStack;

public enum TrousersEnum
{
Levis = 1,
Jeans,
Wrangler
}
90 changes: 90 additions & 0 deletions W3 D3/QueueStack/Wardrobe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using System.Text;

namespace QueueStack;

public class Wardrobe
{
private int _numOfShelves;
private int _maxShelfCapacity;
private Stack<Enum>[] _stackOfClothes;
private List<int> _currentShelfCapacity;

public Wardrobe(int numOfShelves, int maxShelfCapacity)
{
_numOfShelves = numOfShelves;
_maxShelfCapacity = maxShelfCapacity;
_stackOfClothes = new Stack<Enum>[numOfShelves];
for (int i = 0; i < _stackOfClothes.Length; i++)
{
_stackOfClothes[i] = new();
}
_currentShelfCapacity = new(new int[_numOfShelves]);
}

public void PutCloth(int numOfShelf, Enum cloth)
{
// todo
}

public bool TryPutCloth(int numOfShelf, Enum cloth)
{
if (numOfShelf < 0 || numOfShelf >= _numOfShelves) return false;
else
{
_currentShelfCapacity[numOfShelf] += 1;
if (_currentShelfCapacity[numOfShelf] > _maxShelfCapacity)
{
return false;
}
_stackOfClothes[numOfShelf].Push(cloth);
return true;
}
}

public bool TryTakeCloth(int numOfShelf)
{
if (numOfShelf < 0 || numOfShelf >= _numOfShelves) return false;
else
{
_currentShelfCapacity[numOfShelf] -= 1;
if (_currentShelfCapacity[numOfShelf] < _maxShelfCapacity)
{
return false;
}
_stackOfClothes[numOfShelf].Pop();
return true;
}
}

public bool TryPeekCloth(int numOfShelf, out Enum? result)
{
if (numOfShelf < 0 || numOfShelf >= _numOfShelves) {
result = null;
return false;
}
else
{
result = _stackOfClothes[numOfShelf].Peek();
return true;
}
}

public override string ToString()
{
StringBuilder sb = new();
sb.Append("Current wardrobe contains following clothes:");
sb.AppendLine();
foreach (var shelf in _stackOfClothes)
{
IEnumerable<Enum> enumerableStack = shelf;
sb.Append($"Shelf number: {Array.IndexOf(_stackOfClothes, shelf)} | Number of clothes: {shelf.Count}");
sb.AppendLine();
foreach (var item in enumerableStack)
{
sb.Append($"{item}");
sb.AppendLine();
}
}
return sb.ToString();
}
}

0 comments on commit 6ec2c42

Please sign in to comment.