-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
246 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace QueueStack; | ||
|
||
public enum ShirtEnum | ||
{ | ||
Erigo = 1, | ||
Levis, | ||
Uniqlo | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace QueueStack; | ||
|
||
public enum TrousersEnum | ||
{ | ||
Levis = 1, | ||
Jeans, | ||
Wrangler | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |